Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given the following array

$a = array(
    'a' => '0',
    'b' => '1',
    'c' => '2',
    'push' => array(
         'd' => '0',
         'e' => '1',
         'here' => array()
    )
);

And the following set of loops:

// First level
foreach($a as $key=>$value):

     if($key=='push'):

        //Second level
        foreach($value as $key_=>$value_):

             if($key_=='here'):

              // If this key is found then do some stuff here and get another as a result array
              $thirdArray = array(12, 13, 15);

              // Then, I am looking to push this third array from within this loop 
              // Obviously, it should be placed inside this particular key of the array
              // I am trying something like below which doesn't work

              //array_push($value_, $thirdArray);

              endif;

         endforeach;

     endif;

 endforeach;

/* The output of printing my array should be

  'a' => 'A',
  'b' => 'B',
  'c' => 'C',
      'push' => array(
            'd' => '0',
            'e' => '1',
            'here' => array(

                     array(12, 13, 15)

             )


  */

This is giving me a big headache... and can't seem to find a solution.. Many thanks for your help in advance..

share|improve this question
1  
I'm not really sure what you're trying to achieve? Could you clarify some more? –  Oldskool Dec 29 '11 at 13:06
    
@user1099862 You need to tell us what your real goal is, not some abstracted version of it. –  jezmck Dec 29 '11 at 13:08
add comment

5 Answers

up vote 2 down vote accepted
foreach($value as $key_=>$value_):
  if($key_=='here'):
    $thirdArray = array(12, 13, 15);
    $a['push']['here'][] = $thirdArray;
  endif;
endforeach;

or

foreach($value as $key_=>$value_):
  if($key_=='here'):
    $thirdArray = array(12, 13, 15);
    $a[$key][$key_][] = $thirdArray;
  endif;
endforeach;
share|improve this answer
    
Fantastic, thanks.. I will keep second option as in the real example i don't know the name of the key.. –  user1099862 Dec 29 '11 at 13:36
add comment

Why don't you use something like that:

$a[$key][$key_] = array(12, 13, 15);

in stead of

$thirdArray = array(12, 13, 15);

or in case you know the place:

$a['push']['here'] = array(12, 13, 15);
share|improve this answer
add comment

Looks like

if( isset( $a['push'] ) )
 if( isset( $a['push']['here'] ) )
  $a['push']['here'][] = array(12, 13, 15);

would be the fastest way ô.O

share|improve this answer
add comment

You also could try to do replace $value and $value_ to references, so replace them in the lines 2 and 7 by &$value and &$value_ then you should be able to do what you wanted to (array_push)

EDIT: Notice it's not until PHP 5

share|improve this answer
add comment
if($key_=='here'):       
    $value[$key_] = array(12, 13, 15);            
endif;
share|improve this answer
    
Won't affect the original array, only the copy inside the foreach block. –  Matmarbon Dec 29 '11 at 13:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.