简体   繁体   English

从PHP数组访问值

[英]accessing values from php array

I have been having an issue getting values out of an array that is formatted like so: 我一直在从格式化如下的数组中获取值时遇到问题:

array(
   [key]=>array(
            [0]=>value
            [1]=>value
            [2]=>value)
   [key]=>array(
            [0]=>value
            [1]=>value))

I am using a queue to run through each key as the queue item and process the information. 我正在使用一个队列来遍历每个键作为队列项并处理信息。 so to create the queue item I have tried this: 因此,为了创建队列项目,我尝试了以下方法:

while ($array = $result->fetchAssoc())
                {

                    $queue->createItem($array);

                }

this fails to create any items so I have used this method instead 这无法创建任何项目,所以我改用了这种方法

if ($array != 0 || $array != NULL) {
       foreach ($array as $row) { 
            $queue->createItem($row);
       }
}

Once the queue item is created the queue calls a function passing the queue $item and here is where I have problems as I can successfully get all the values of the second level array but I can not access the Key of the first level. 创建队列项目后,队列调用传递队列$ item的函数,这是我遇到的问题,因为我可以成功获取第二级数组的所有值,但无法访问第一级的Key。

function work_function($item){

   foreach($item as $row=>$job){
       //do something
   }
}

In my function I have tried: 在我的职能中,我尝试过:

 //1
    $arrayKEY= $item;

    //2
    foreach($item as $row){
     $arrayKEY= $row;
    }

I just cannot get the values I need. 我只是无法获得所需的价值。 What am I doing wrong/can I do to accomplish this? 我在做什么错/我该怎么做?

Thanks 谢谢

There's not much info here, but if the array is like you show, it's a multi-dimensional array, and thus needs 2 for loops. 这里没有太多信息,但是如果数组像您显示的那样,则它是一个多维数组,因此需要2个for循环。

function work_function($item){
   foreach($item as $row=>$job){
       echo "Row $row:\n";
       foreach($job as $value){
          echo $value."\n";
       }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM