Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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

$array1[$i]['status'] = 'new value';
share|improve this answer
 
Although var_dump($x['status']); posts as: string(1) "0", $x['status'] = 'new value'; doesnt change anything –  jQuerybeast yesterday
 
I updated my answer, does that makes more sense to you? –  believe me yesterday
 
Bingo! thank you –  jQuerybeast yesterday
 
Your welcome :) –  believe me yesterday
add comment

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
        )
)
share|improve this answer
 
This doesnt work –  jQuerybeast yesterday
 
@jQuerybeast sorry I misunderstood at first. check the update. –  revo yesterday
 
Thank you. I will be using your method since I can avoid all those foreach methods for performance issues. –  jQuerybeast yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.