具有动态索引的 PHP 数组

Posted

技术标签:

【中文标题】具有动态索引的 PHP 数组【英文标题】:PHP array with dynamic index 【发布时间】:2018-06-24 17:26:41 【问题描述】:

我试图从 php 数组中创建表格。但是这里所有的键都是动态的,并且还有子数组

数组结构如下:

array (
  1371618448317 => 
  array (
    0 => 
    array (
      0 => '23.77311734',
      1 => '90.396355125',
      2 => '23.77313316',
      3 => '90.396411867187',
      4 => '23.77309048',
      5 => '90.396419484375',
      6 => '23.77307348',
      7 => '90.3963645',
    ),
    1 => 20911,
    2 => 
    array (
      1371618713208 => 
      array (
        0 => 1,
        1 => 'BRAC Delivery Centre',
        2 => '371/A  Shahinbag',
        3 => 25,
        4 => 91,
        5 => 221,
        6 => 1,
        7 => 11,
        8 => 1,
        9 => 99,
        10 => 1,
        11 => 99,
        12 => 99,
        13 => 99,
        14 => 99,
        15 => 99,
        16 => 1,
      ),
    ),
  ),
  1371619410448 => 
  array (
    0 => 
    array (
      0 => '23.77894566',
      1 => '90.39968559375',
      2 => '23.77916362',
      3 => '90.400307765625',
      4 => '23.77889887',
      5 => '90.400401515625',
      6 => '23.77870083',
      7 => '90.399780515625',
    ),
    1 => 24612,
    2 => 
    array (
      1371619950162 => 
      array (
        0 => 1,
        1 => 'EPI Centre (Govt.)',
        2 => ' Mohakhali Road Mohakhali',
        3 => 20,
        4 => 91,
        5 => 11,
        6 => 1,
        7 => 12,
        8 => 1,
        9 => 99,
        10 => 1,
        11 => 99,
        12 => 99,
        13 => 99,
        14 => 99,
        15 => 99,
        16 => 0,
      ),
    ),
  ),
  1371621080807 => 
  array (
    0 => 
    array (
      0 => '23.77746206',
      1 => '90.399232078125',
      2 => '23.77744917',
      3 => '90.399623390625',
      4 => '23.77712934',
      5 => '90.39958940625',
      6 => '23.77714809',
      7 => '90.399205125',
    ),
    1 => 24566,
    2 => 
    array (
      1371621897771 => 
      array (
        0 => 1,
        1 => 'Society for Assistance to Hearing Impaired Children (SAHIC)',
        2 => 'N/A Sattola Road Mohakhali',
        3 => 20,
        4 => 91,
        5 => 222,
        6 => 1,
        7 => 7,
        8 => 1,
        9 => 1,
        10 => 1,
        11 => 1,
        12 => 99,
        13 => 99,
        14 => 99,
        15 => 1,
        16 => 0,
      ),
    ),
  ),
  1371622305777 => 
  array (
    0 => 
    array (
      0 => '23.77357261',
      1 => '90.36189965625',
      2 => '23.77359605',
      3 => '90.36197925',
      4 => '23.77344028',
      5 => '90.36201675',
      6 => '23.77341802',
      7 => '90.361931296875',
    ),
    1 => 1325,
    2 => 
    array (
      1371622497359 => 
      array (
        0 => 1,
        1 => 'Natoinal Health Care Network',
        2 => '3/Ka Pisiculture Housing Society Shamoly',
        3 => 29,
        4 => 91,
        5 => 222,
        6 => 1,
        7 => 7,
        8 => 1,
        9 => 99,
        10 => 99,
        11 => 1,
        12 => 99,
        13 => 99,
        14 => 1,
        15 => 1,
        16 => 0,
      ),
    ),
  )

我的欲望故事会是这样的:

BRAC Delivery Centre      371/A  Shahinbag      23.77311734   90.396355125
EPI Centre (Govt.)        Mohakhali Road        23.77746206   90.399232078125

从 lat long 数组我总是尝试收集第一个 lat long。

谁能给我解决办法?

【问题讨论】:

*** 不是免费的编码服务。在发布问题之前进行研究并尝试。您的问题没有资格获得答案,因为它太宽泛了。 如果你想 从 lat long 数组中我总是尝试收集第一个 lat long,那么 90.399232078125 是错误的,因为它是下一行的 long Society for听力障碍儿童援助 (SAHIC) 请提供您尝试过的内容.. $assoc = true; $result = json_decode ($json, $assoc); foreach($result as $single_res) $lat = $single_res[0][0]; $long = $single_res[0][1]; $key = $single_res[2]; $name = $key[2]; print_r($key); print_r($name);我无法从第二个索引接收价值。因为运行时我不知道数组索引值。比如 1371618448317 是索引值 php.net/manual/en/function.array-values.php 【参考方案1】:

这是你的解决方案

输入

$array = array(
    1371618448317 => array (
        array ('23.77311734','90.396355125','23.77313316','90.396411867187','23.77309048','90.396419484375','23.77307348','90.3963645'),
        20911,
        array ('1371618713208' => array (1,'BRAC Delivery Centre','371/A  Shahinbag',25,91,221,1,11,1,99,1,99,99,99,99,99,1))
    ),
    1371619410448 => array (
        array ('23.77894566','90.39968559375','23.77916362','90.400307765625','23.77889887','90.400401515625','23.77870083','90.399780515625',),
        24612,
        array ('1371619950162' => array (1,'EPI Centre (Govt.)',' Mohakhali Road Mohakhali',20,91,11,1,12,1,99,1,99,99,99,99,99,0))
    ),
    1371621080807 => array(
        array ('23.77746206','90.399232078125','23.77744917','90.399623390625','23.77712934','90.39958940625','23.77714809','90.399205125'),
        24566,
        array ('1371621897771' => array (1,
            'Society for Assistance to Hearing Impaired Children (SAHIC)','N/A Sattola Road Mohakhali',20,91,222,1,7,1,1,1,1,99,99,99,1,0)
        )
    ),
    1371622305777 => array (
        array ('23.77357261','90.36189965625','23.77359605','90.36197925','23.77344028','90.36201675','23.77341802','90.361931296875'),
        1325,
        array ('1371622497359' => array (1,
            'Natoinal Health Care Network','3/Ka Pisiculture Housing Society Shamoly',29,91,222,1,7,1,99,99,1,99,99,1,1,0)
        )
    )
);

解决方案

    坐标在第一个数组中。 姓名和地址在第三位。

$row[0][0].','.$row[0][1]; 在下面的给定循环中轻松选择坐标。 对于地址和名称,您需要选择第三个数组的键,在这里我使用了$row[2][key($row[2]);

现在检查下面的代码。

$new_array = array();
$i=0;
foreach($array as $key => $row)
    //echo "<pre>";print_r(key($row[2]));
    $new[$i]['coordinates'] = $row[0][0].','.$row[0][1];
    $new[$i]['name'] = $row[2][key($row[2])][1];
    $new[$i]['address'] = $row[2][key($row[2])][2];
    $i++;

echo "<pre>";print_r($new);

输出

Array
(
    [0] => Array
        (
            [coordinates] => 23.77311734,90.396355125
            [name] => BRAC Delivery Centre
            [address] => 371/A  Shahinbag
        )

    [1] => Array
        (
            [coordinates] => 23.77894566,90.39968559375
            [name] => EPI Centre (Govt.)
            [address] =>  Mohakhali Road Mohakhali
        )

    [2] => Array
        (
            [coordinates] => 23.77746206,90.399232078125
            [name] => Society for Assistance to Hearing Impaired Children (SAHIC)
            [address] => N/A Sattola Road Mohakhali
        )

    [3] => Array
        (
            [coordinates] => 23.77357261,90.36189965625
            [name] => Natoinal Health Care Network
            [address] => 3/Ka Pisiculture Housing Society Shamoly
        )

)

【讨论】:

【参考方案2】:

自己解决了:

$assoc = true;
$result = json_decode ($json, $assoc);


echo "<table border='1'>";
echo "<tr><td>Hospital Name</td><td>Address</td><td>Lat1</td><td>Long1</td><td>Lat2</td><td>Long1</td><td>Lat3</td><td>Long3</td><td>Lat4</td><td>Long4</td></tr>";



foreach($result as $single_res)

$lat1 = $single_res[0][0];
$long1 = $single_res[0][1];


$lat2 = $single_res[0][2];
$long2 = $single_res[0][3];


$lat3 = $single_res[0][4];
$long3 = $single_res[0][5];


$lat4 = $single_res[0][6];
$long4 = $single_res[0][7];


$key = $single_res[2];

$key_val = array_keys($key);
$key_val = $key_val[0];


$name = $key[$key_val][1];
$address = $key[$key_val][2];

【讨论】:

以上是关于具有动态索引的 PHP 数组的主要内容,如果未能解决你的问题,请参考以下文章

遍历具有不同索引的表单元素名称数组

PHP如何处理整数索引的非连续键控数组?

MySQL PDO fetchAll 作为具有整数索引的数组

php 数组索引值如何重新从0开始递增

php把索引数组的第一个元素移除后索引不重置

JS/JQuery : 在生成新字段的同时继续表单数组的索引