1

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..

2
  • 1
    I'm not really sure what you're trying to achieve? Could you clarify some more? Commented Dec 29, 2011 at 13:06
  • @user1099862 You need to tell us what your real goal is, not some abstracted version of it. Commented Dec 29, 2011 at 13:08

5 Answers 5

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

1 Comment

Fantastic, thanks.. I will keep second option as in the real example i don't know the name of the key..
0

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);

Comments

0

Looks like

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

would be the fastest way ô.O

Comments

0

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

Comments

0
if($key_=='here'):       
    $value[$key_] = array(12, 13, 15);            
endif;

1 Comment

Won't affect the original array, only the copy inside the foreach block.

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.