0

I have two arrays $array1 and $array2 with data as show:

$array1:

Array 
  ( 
     [0] => Array 
         ( 
            [id] => 222
            [issubtask] => true 
         ) 
     [1] => Array 
         ( 
            [id] => 444
            [issubtask] => false 
         )
  )

$array2:

Array 
  ( 
   [0] => Array 
    ( 
        [id] => 111 
        [name] => Mark 
        [isselected] => false 
        [issubtask] => false 
    ) 
   [1] => Array 
    ( 
        [id] => 222 
        [name] => Tony 
        [isselected] => false 
        [issubtask] => false 
    ) 
   [2] => Array 
    ( 
        [id] => 333 
        [name] => Jack 
        [isselected] => false 
        [issubtask] => false 
    )
   [3] => Array 
    ( 
        [id] => 444 
        [name] => Nick 
        [isselected] => false 
        [issubtask] => false 
    )   
  )

All I want to do is check for the matching 'id' in $array2 from $array1 and then replace the 'issubtask' value of $array2 for that 'id' with the value of 'issubtask' in $array1.

I tried this out but the values of $array2 are not changed:

foreach ($array1 as $val1) {
    foreach ($array2 as $val2) {
        // Checking whether IDs match
        if ($val2['id'] == $val1['id']) {
            $val2['isselected'] = "true";
            $val2['issubtask'] = $value1['issubtask'];
        }
    }
}
print_r($array2);

I am expecting the below result but I get the $array2 values as it was before.

Result $array2:

Array 
( 
[0] => Array 
    ( 
        [id] => 111 
        [name] => Mark 
        [isselected] => false 
        [issubtask] => false 
    ) 
[1] => Array 
    ( 
        [id] => 222 
        [name] => Tony 
        [isselected] => true 
        [issubtask] => true 
    ) 
[2] => Array 
    ( 
        [id] => 333 
        [name] => Jack 
        [isselected] => false 
        [issubtask] => false 
    )
[3] => Array 
    ( 
        [id] => 444 
        [name] => Nick 
        [isselected] => true 
        [issubtask] => false 
    )   
)  

I am not sure whether I am doing the right way. Any help is much appreciated.
Thank you

2 Answers 2

2

You're changing the local value of the var, not the one really stored in your array. When you've got this :

    foreach ($array2 as $key2 => $val2) {

        // This is wrong : $val2 is a local value only in the foreach
        $val2['isselected'] = "true";

        // This is ok, you really change the value in the array
        $array2[$key2]['isselected'] = true ;
    }

Here is an answer for your problem. Be carefull to use true and not "true" also.

foreach ( $array1 as $field1 )
{
    foreach ( $array2 as $key2 => $field2 ) // Note that $field2 is just a local value in this loop
    {
        if ( $field2['id'] == $field1['id'] ) // Same ID for each field, and field1 has issubtask
        {
            // You have to change the value contained in $array2[1][222], not the value stored in $field2[issubtask] because this is a local value
            $array2[$key2]['issubtask'] = $field1['issubtask'] ; // Here you have to call the "full path" of array2[$key2]
            $array2[$key2]['isselected'] = true ;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Replace directly $array2 not $val2.

foreach ($array1 as $val1) {
    foreach ($array2 as $k => $v) {
        // Checking whether IDs match
        if ($v['id'] == $val1['id']) {
            $array2[$k]['isselected'] = "true";
            $array2[$k]['issubtask'] = $val1['issubtask'];
        }
    }
}
print_r($array2);

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.