0

I need to search $array_2 for a json_encode()'d value from $array_1.

If item_id in $array_1 does not match the item_id in the some_field field in $array_2 then I need that array index so I can insert that into the database.

I've tried using in_array() but that won't work because the arrays have different keys. Also array_diff() will not work because the array indexes have no relation.

$array_1:

Array
(
    [0] => Array
        (
            [id] => 34
            [item_id] => sfg4et4
        )

    [1] => Array
        (
            [id] => 472
            [item_id] => adadgt4
        )
)

$array_2;

Array
(
    [0] => Array
        (
            [some_field] => {"item_id":"mlkjm980"}
        )

    [1] => Array
        (
            [some_field] => {"item_id":"fsff5ssa"}
        )
)
2
  • Do the numerical indexes in these arrays have any correlation? For example do you only want to compare item at index 0 in array 1 to item in index 0 in array 2? If so, are the arrays guaranteed to be the same size? Commented Dec 11, 2013 at 19:07
  • No, they don't. Sorry I did not include that. So array_diff() will not work either. Commented Dec 11, 2013 at 19:10

1 Answer 1

2

It seems to me that the first step would be to put $array_2 into a usable form. To do this, I might suggest using array_walk() to rewrite this array to be solely a numerically indexed array of item_id values.

$array_2 = [...];

array_walk($array_2, function(&$value, $index_not_used) {
    $some_field = json_decode($value['some_field']);
    // overwrite value
    $value = $some_field->item_id;
});

$array_2 is now an array like ['mlkjm980','fsff5ssa', ...]

Now you can apply array_filter() to $array_1 using this modified $array_2 array.

$filtered_array = array_filter($array_1, function ($value) use ($array_2) {
    $item_is_in_array_2 = in_array($value['item_id'], $array_2);
    return !$item_is_in_array_2; // return negation of value as we want to filter out cases where item_id is in $array_2
}); 
Sign up to request clarification or add additional context in comments.

6 Comments

I did something almost just like that )you can see my edit) but the problem is I'll still need the values from $array_1 so I can insert it into the database.
@Draven This solution I provided gives you that, as $filtered_array is just subset of $array_1 with matches removed.
Oh. I'll be back in an hour or so and I'll give it a shot. Thanks!
Any way I can get the id field after? I need it for later on in the script to UPDATE the database instead of INSERT.
@Draven The id field would still remain in the filtered array. Again, this approach does not modify the individual array elements in $array_1 at all, it simply removes the elements you want to filter out.
|

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.