带有动态索引的PHP数组

时间:2018-01-16 03:56:47

标签: php arrays multidimensional-array

我试图从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。

任何人都可以给我解决方案吗?

2 个答案:

答案 0 :(得分:0)

这是您的解决方案

<强>输入

$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)
        )
    )
);

<强>解决方案

  1. 坐标位于第一个数组中。
  2. 姓名和地址位于第3位。
  3. 在下面的给定循环中,$row[0][0].','.$row[0][1];可以轻松选择坐标。 对于地址和名称,您需要选择第3个数组的键,为此我使用了$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
            )
    
    )
    

答案 1 :(得分:0)

解决了自己:

$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];