从动态生成的数组中获取值

时间:2014-12-05 12:29:58

标签: php get key-value-store

我有一个数组

阵 (     [13802172] =>排列         (             [0] => 13802172             [1] =>回顾             [2] => ACCESS HIRE NSW             [3] => ACCESS HIRE NSW PTY LTD             [4] =>             [5] =>             [6] =>设备租赁&租赁服务,工具R             [7] => 5106206             [8] =>             [9] => 7394
        )

[13802174] => Array
    (
        [0] => 13802174
        [1] => reviewed
        [2] => ACRON ROAD OSHC
        [3] => ACRON ROAD OUT OF SCHOOL
        [4] => 
        [5] => 
        [6] => CHILD CARE SERVICES
        [7] => 5122005
        [8] => 
        [9] => 8351
    )

通过脚本动态生成13802174和13802172。

现在我需要[13802172] [6]的值作为EQUIP RENTALS& LEASING SERVICES,TOOL R动态,数组键可以很多,我需要显示第6个索引值的所有键。

你能告诉我一个答案怎么做?

2 个答案:

答案 0 :(得分:0)

您只需遍历整个数组,然后询问第7个值(索引6):

foreach($arr as $v) 
  printf("<pre>%s</pre>", $v[6]);

您还可以查看array_keys

答案 1 :(得分:0)

好的,所以你会有很多像这样的数组

[13802174] => Array
    (
        [0] => 13802174
        [1] => reviewed
        [2] => ACRON ROAD OSHC
        [3] => ACRON ROAD OUT OF SCHOOL
        [4] => 
        [5] => 
        [6] => CHILD CARE SERVICES
        [7] => 5122005
        [8] => 
        [9] => 8351
    )

我假设您已将每个分配给动态生成的数组。

你可以这样循环

foreach($dynamicallyGeneratedArrays as $key => $dynamicArray) {

    print $key.": ".$dynamicArray[6];
}

或者您可能想将其分配给新数组

$newArray = array();
foreach($dynamicallyGeneratedArrays as $key => $dynamicArray) {

    if(isset($dynamicArray[6]) {

        $newArray[$key] = $dynamicArray[6];
    }
}

如果您需要其他任何内容来实现您想要的结果,请发表评论,以便我尝试帮助编辑我的答案