tags:

views:

171

answers:

4

Here's a code example:

  $array = array();

  $array['master']['slave'] = "foo";

  foreach ($array as $key => $value) {

    foreach ($value as $key2 => $value2) {

      if (preg_match('/slave/',$key2)) {

        $value[$key2] = "bar";
        print "$value[$key2] => $key2 => $value2\n";
      }
    }
  }

  print_r($array);

Output:

bar => slave => foo

Array

(

    [master] => Array

        (
            [slave] => foo
        )

)

Rather i would like to have the following as the final array:

Array
(

    [master] => Array

        (
            [slave] => bar
        )

)

What wrong am i doing here?

Thank you!

Note: Example2:

enter code here $a= array('l1'=>array('l2'=>array('l3'=>array('l4'=>array('l5'=>'foo')))));

$a['l1']['l2']['l3']['l4']['l5'] = 'bar';

foreach ($a as $i => &$values) {

foreach ( $values as $key => &$value) {

if (is_array($value)){

  print_array($value,$key);

}

}

}

function print_array ($Array, $parent) {

foreach ($Array as $i1 => &$values1) {

if (is_array($values1)){

  foreach ($values1 as $key1 => &$value1) {

    if (is_array($value1)) {

      print_array($value1,$values1);

    }

    else {

      print "       $key1 => $value1\n";

    }

  }

}

else {

  if (preg_match('/l5/',$i1)) {

    $values1 = "foobar";

    print "       $i1 => $values1\n";

  }

}

}

}

print_r($a);

Output does not reflect 'foobar' in l5

Thank you!

A: 

Why not just:

foreach ($array as $key => &$value)
{
  if(isset($value['slave']))
    $value['slave'] = 'bar';
};
Ignacio Vazquez-Abrams
+1  A: 

Because foreach operates on a copy of the array. Read the documentation about foreach:

Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.

So you should do

foreach ($array as $key => &$value) {

    foreach ($value as $key2 => $value2) {
          //...
    }
}

Update:

Ok, I reviewed your code again:

  1. Your code was not working as your loops never reach the array with key l5.
  2. The code is highly inefficient. You make assumptions about the depth of the input array. E.g. your code cannot process the input array you provide (it should work if you omit one array).

Solution:

Make use of recursion. This code works:

$a =array('l1'=>array('l2'=>array('l3'=>array('l4'=>array('l5'=>'foo')))));

function process(&$array, $needle) {
    foreach($array as $k => &$v) {
        if ($k == $needle) {
             $v = "boooooooooo";
             print "$k => $v\n";
        }
        if (is_array($v)) {
             process($v, $needle);
        }
    }
}   

process($a, $needle);
print_r($a);

Hope that helps.

Oh and please use other keys next time. I thought the whole time that the key was 15 (fifteen) and was wondering why my example was not working ;) (15 looks not that different from l5 at a glance).

Felix Kling
Felix Kling
Felix, thanks this works with 2D. But can you take a look at my edited question? the one with $a as the array name and let me know what changes need to be made? Thanks a ton!
@Felix: I changed the code as you asked, but still it doesn't do the job for me! I need this working desperately as its choking up other things. So please bare with my impatience :) Can you know please have a look?
@amnesia-55: Updated my answer.
Felix Kling
@Felix: That helped a lot!! thanks a ton
A: 

Because $value is a new variable

Alexandru Luchian
+1  A: 

You have a few choices, but all stem from the same problem, which originates on this line

foreach ($array as $key => $value) {

At this point in the code, $value is not a reference to 2nd dimension of arrays in your data structure. To fix this, you have a couple options.

1) Force a reference

foreach ($array as $key => &$value) {

2) Use a "fully-qualified" expression to set the desired value

$array[$key][$key2] = 'bar';
Peter Bailey
Thanks!Can you help detect the error in foreach for the code below?<?php$a = array('l1'=>array('l2'=>array('l3'=>array('l4'=>array('l5'=>'foo')))));foreach ($a as $i => } }}function print_array ($Array, $parent) { foreach ($Array as $i1 => $values1) { if (is_array($values1)){ foreach ($values1 as $key1 => $value1) { if (preg_match('/l5/', $key1)) { $value1 = "boooooooooo"; continue; } } } }}print_r($a);?>
Sorry, the code looks messed up here! i'll edit my post itself..