开发者

In PHP how do i update values in an asssociative array and store the entire array?

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:

$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


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


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


Because $value is a new variable


Why not just:

foreach ($array as $key => &$value)
{
  if(isset($value['slave']))
    $value['slave'] = 'bar';
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜