7

I have one array having multiple objects (say 3 Objects), each having 3 "Key-Value" pairs.

$PredefinedResult is something like this :

[
    {
        "EffectiveStatusId":0,
        "EffectiveStatus":"abc",
        "RecordCount":0
    },
    {
        "EffectiveStatusId":0,
        "EffectiveStatus":"def",
        "RecordCount":0
    },
    {
        "EffectiveStatusId":0,
        "EffectiveStatus":"ghi",
        "RecordCount":0
    }
]

I have another array of objects named $MainResult with values like :

[
    {
        "EffectiveStatusId":1,
        "EffectiveStatus":"abc",
        "RecordCount":7
    },
    {
        "EffectiveStatusId":6,
        "EffectiveStatus":"def",
        "RecordCount":91
    }
]

Expected Result :

I want to replace the similar objects inside $PredefinedResult with the objects from $MainResult and want result like this :

[
    {
        "EffectiveStatusId":1,
        "EffectiveStatus":"abc",
        "RecordCount":7
    },
    {
        "EffectiveStatusId":6,
        "EffectiveStatus":"def",
        "RecordCount":91
    },
    {
         "EffectiveStatusId":0,
         "EffectiveStatus":"ghi",
         "RecordCount":0
    }
]

What I tried :

I tried with this code but it's not giving me the desired result.

$FinalResult = array_replace($PredefineResult,$MainResult);

Can anyone help me on how to get the Expected Result as mentioned above ?

2
  • That's JSON. Are you actually decoding it before? Commented Sep 23, 2013 at 12:36
  • @AmalMurali : No, I have just formatted it so that everyone can see the result what i want. Actually it's an Array of Objects. Commented Sep 23, 2013 at 12:38

2 Answers 2

3

There's no "built-in" function for this. You're gonna have to loop and compare each manually. array_map seems like an OK choice here:

$PredefinedResult = array_map(function($a) use($MainResult){
    foreach($MainResult as $data){
        if($a->EffectiveStatus === $data->EffectiveStatus){
            return $data;
        }
    }
    return $a;
}, $PredefinedResult);

DEMO: http://codepad.viper-7.com/OHBQK8

Sign up to request clarification or add additional context in comments.

1 Comment

Demo no longer accessible.
2

Iterate through the array and manual compare the values as follows.

$res = array();
foreach ($PredefineResult as $result){
    foreach ($MainResult as $mresult){
        if(($result->EffectiveStatus == $mresult->EffectiveStatus) && $mresult->RecordCount!=0){
            $res[] = $mresult;
        }else $res[] = $result;
    }
}
print_r($res);

2 Comments

I don't think this is going to be work. Can you explain me how array_unique() will select only object with RecordCount != 0 ? See the result I want is that Object with RecordCount == 0 should be replaced with RecordCount != 0.
Seems your key is EffectiveStatus for each array. Then you have to go with foreach(); Let me update my answer accordingly.

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.