0

I pull one array from a data and one array from an API.

API Array (array1)

...
[71] => Array
        (
            [id] => integer
            [name] => example_name
            [logo] => url_of_image
            [lang] => en
        )
...

Database API (array2)

    ...
Array(
    [0] => integer
    [1] => integer
}
    ...

I want to use array2 to find any instances of the ID in array1, if they exist then unset the key.

Array diff doesn't work with multidimensional. There isn't a common key The values are dynamic so i can't hard code them Array filter won't take multi commands and doing a foreach results in an error

Notice: Array to string conversion in

PHP 5.6.

foreach($array2 as $key => $value) {
   foreach($array1 as $key1 => $value1) {
      if ($value1 == $value) {
         unset($arrat2[$key])
      }
   }
}

I had this buried somewhere to filter out languages i thought i could stick a foreach inside the function but that throws an array to string conversion.

$filtered = array();
                    $filtered = array_filter($array1, function($el) { return ($el['lang'] == "en"); });

Something like:

        $result = array();
            foreach($array2 as $value) {
                $result = array_filter($array1, function($ee) { return ($ee['id'] != $value); });
            }

Results in:

Notice: Undefined variable: value i

Var dumps array 2:

array(2) {
  [0]=>
  array(1) {
    ["id"]=>
    string(8) "random integer"
  }
  [1]=>
  array(1) {
    ["id"]=>
    string(9) "random integer"
  }
}

var dumps array 1:

array(151) {
  [0]=>
  array(4) {
    ["id"]=>
    int(integer 1)
    ["name"]=>
    string(7) "example name 1"
    ["logo"]=>
    string(97) "image1.png"
    ["lang"]=>
    string(2) "en"
  }
  [1]=>
  array(4) {
    ["id"]=>
    int(integer 2)
    ["name"]=>
    string(10) "example name 2"
    ["logo"]=>
    string(100) "image2.png"
    ["lang"]=>
    string(2) "en"
  }
  [2]=>
  array(4) {
    ["id"]=>
    int(integer 3)
    ["name"]=>
    string(9) "example name 3"
    ["logo"]=>
    string(99) "image3.png"
    ["lang"]=>
    string(2) "en"
  }

great answer, also had to make sure my two values were both integers being compared.

3
  • Show your foreach(), I don't see why this approach wouldn't work if you are doing it correctly. Commented Jan 18, 2017 at 1:04
  • Why wouldn't array_filter work? You supply the callback that decides if an element gets tossed or not. Commented Jan 18, 2017 at 1:16
  • @Rasclatt I know i am doing it wrong for sure, updated post. Commented Jan 18, 2017 at 1:23

2 Answers 2

1

Depending on what you want to do, this might help (you can provide more than 1 filter per line in $array2, lines are OR'ed while multiple conditions in one line are AND'ed):

<?php

$array1 = array(
    array(
        "id" => 1,
        "name" => "foo",
        "logo" => "foo-logo.png",
        "lang" => "es"
    ),
    array(
        "id" => 3,
        "name" => "bar",
        "logo" => "bar-logo.png",
        "lang" => "en"
    ),
    array(
        "id" => 7,
        "name" => "xyz",
        "logo" => "xyz-logo.png",
        "lang" => "it"
    ),
    array(
        "id" => 15,
        "name" => "aaa",
        "logo" => "aaa-logo.png",
        "lang" => "it"
    ),
    array(
        "id" => 23,
        "name" => "aaa",
        "logo" => "aaa-logo.png",
        "lang" => "es"
    ),
);

// Filter out entries with id #7 OR lang = en OR (name = "aaa" AND lang = "es")
$array2 = array(
    array("id" => 7),
    array("lang" => "en"),
    array("name" => "aaa", "lang" => "es")
);

echo "<pre>";

$filtered = array_filter($array1, function($e) use ($array2)
{
    foreach($array2 as $filters)
    {
        $doFilter = true;
        foreach($filters as $k => $v)
        {
            if (isset($e[$k]) && $e[$k] !== $v) {
                $doFilter = false;
                break;
            }
        }
        if ($doFilter)
        {
            return false;
        }
    }
    return true;
});

var_dump($filtered);

Output:

array(2) {
  [0]=>
  array(4) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(3) "foo"
    ["logo"]=>
    string(12) "foo-logo.png"
    ["lang"]=>
    string(2) "es"
  }
  [3]=>
  array(4) {
    ["id"]=>
    int(15)
    ["name"]=>
    string(3) "aaa"
    ["logo"]=>
    string(12) "aaa-logo.png"
    ["lang"]=>
    string(2) "it"
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I cant get this to work i think its because my IDs are strored like this: Array ( [0] => Array ( [id] => 32007412 ) [1] => Array ( [id] => 131877902 ) )
That's exactly the structure that my array2 has, what's the error?
Doesn't throw an error just doesn't filter anything
Can you var_dump your arrays? (input/$array1 and filter/$array2 in my case) Atleast a small portion would help.
updated main post the first result in both array 1 and array 2 is the same integer ID, is it because array 2 is a string not an integer?
|
0

I think you have one too many loops. Loop through the array you want to unset the key from, and test each value against the second array.

FOREACH ($array2){
$test_array[] = $id;
}
FOREACH ( $array1 AS $key => $array3){
  IF ( in_array($array3['id'],$test_array) ) {
    unset($array1[$key]);
  }
}

8 Comments

So i came across this on here somewhere else and this throws me a bracket error, even though all my brackets open and close correctly (not got 1 to many or 1 too short). [17-Jan-2017 20:31:01 America/Toronto] PHP Parse error: syntax error, unexpected '{' in //search.php on line 100 (line 100 being the unset line.)
@Keelan Did I understand correctly that you don't have array1['id'] but that your ids are variables themselves....for example, array1['id_this'] = 5; would match and unset array2['id_that'] = 5;?
array1 carries multidemensional arrays that each contain at least an id. I have some ids stored in a databased which i retrieve to an array. I want those ids searching the in array1 and make the key unset. ID key is common in the arrays.
@Keelan So, you want to unset($array1[$key]) when the value of $array1[$key]['id] is equal to any integer within $array2[]?
yes, however i have found out that the values inside array2 are strings not integers so this part of my problem ha.
|

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.