0

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

8
  • en.wikipedia.org/wiki/Queue_%28data_structure%29 Commented Feb 10, 2012 at 17:53
  • 2
    @user975044. Any chance of the less prissy definition of $queue? Specifically which Php object is it? Commented Feb 10, 2012 at 18:36
  • Ok, specifically for my case i am using: api.drupal.org/api/drupal/modules--system--system.api.php/… terrible doc though.. Drupal i know.. but that shouldn't matter to the problem. A queue is basically a way of loading objects into a container or line to be processed. So if I want the number 1,2,3,4 to be processed in that order through the same function I can create a queue object for each number. so in the queue I have 4 objects which contain the numbers 1,2,3,4 in that order. Commented Feb 10, 2012 at 18:57
  • To start processing the queue the first object, which is number 1, gets pulled from the queue and processed. I shouldn't haven mentioned this as it really isn't causing the issue here. It is being able to access the array key and values in a function Commented Feb 10, 2012 at 18:57
  • @user975044: We all know what a queue is, we're asking you what your $queue object is, specifically. Commented Feb 10, 2012 at 19:11

1 Answer 1

1

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";
       }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.