简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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