0

I have 2 arrays. Sometimes a key/value from array1 may equals to key/value of array2. If that is true, change 'status' from the specific key/value from array2, to a new value. Does that make sense?

Here is where I am at:

foreach($array1 as $i=>$x){
          foreach($array2 as $k=>$y){
            if($x['url'] == $y['url']){

              // Up to here works

              foreach($i as &$value) {
                $value['status'] = 'new value';
            }

              break;
            }
          }
        }

This are my arrays:

array(1) { 
    [0]=> array(1) {
        ["url"]=> string(104) "aHR0cDovL3lvdXR1YmUuY29t"
        ["date"]=> string(19) "2014-01-06 21:44:39"
        ["status"]=> string(1) "0"
     }

    [1]=> array(1) { 
        ["url"]=> string(28) "d3d3LnR3aXR0ZXIuY29t"
        ["date"]=> string(19) "2014-01-06 14:28:32"
        ["status"]=> string(1) "0"
     }
 } 

and array2:

array(2) { 
    [0]=> array(2) {
        ["url"]=> string(104) "aHR0cDovL3lvdXR1YmUuY29t"
        ["date"]=> string(19) "2014-01-06 21:44:39" 
     }

    [1]=> array(2) { 
        ["url"]=> string(28) "aHR0cDovL3d3dy5nb29nbGUuY29t"
        ["date"]=> string(19) "2014-01-06 14:28:32"
     }
 }

Up to the comment it works. From there after, how can I change that specific key/value to a new value? My current example changes all key 'status' to 'new value'.

2 Answers 2

1

You don't have to loop again through array1 just change the key of it

$array1[$i]['status'] = 'new value';
Sign up to request clarification or add additional context in comments.

2 Comments

Although var_dump($x['status']); posts as: string(1) "0", $x['status'] = 'new value'; doesnt change anything
I updated my answer, does that makes more sense to you?
1

How about this:

<?php
    $array1 = array(
        array(
            "url"   =>  "aHR0cDovL3lvdXR1YmUuY29t",
            "date"  =>  "2014-01-06 21:44:39",
            "status"    =>  "0"
            )
        );
    $array2 = array(
        array(
            "url"   =>  "aHR0cDovL3lvdXR1YmUuY29t",
            "date"  =>  "2014-01-06 21:44:39",
            )
        );

    array_walk($array2, function($arr2) use (&$array1)
    {
        foreach($array1 as &$arr1)
        {
            if($arr2['url'] == $arr1['url'])
                $arr1['status'] = "something";
        }
    });

    print_r($array1);

Output:

Array
(
    [0] => Array
        (
            [url] => aHR0cDovL3lvdXR1YmUuY29t
            [date] => 2014-01-06 21:44:39
            [status] => something
        )
)

2 Comments

@jQuerybeast sorry I misunderstood at first. check the update.
Thank you. I will be using your method since I can avoid all those foreach methods for performance issues.

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.