0

I have a multidimensional array

Simple example array

[0]=>array(2) {
["slug"]=>string(12) "exampleslug1"
["tax"]=>string(11) "exampletax1"}
[1]=>array(2) {
["slug"]=>string(12) "exampleslug2"
["tax"]=>string(11) "exampletax2"}
[2]=>array(2) {
["slug"]=>string(12) "exampleslug2"
["tax"]=>string(11) "exampletax3"}

Now i want to check another multidimensional array for both values.

So for example. I want to check if the slug is "exampleslug2" and the tax = "exampletax2" So in this case it should return false/true/false.

Ive tried in_array but i can't seem to get that to work for multidimensional. Can anyone help me out with a solution for checking multiple values in the multidimensional array to be the same as in a second multidimensional array.

If it try something like

if (!in_array($termSlug, $externalTermSlugs['slug']) && !in_array($termTax, $externalTermSlugs['tax']) ) {}

It adds everything multiple times, what i want to have is that it adds everything once unique and doesn't overwrite if it already exists.

2
  • 2
    Use a simple foreach iteration. Commented Jul 5, 2018 at 9:51
  • How about in_array([ "slug" => $termSlug, "tax" => $termTax ], $externalTermSlugs)? Also array_unique($externalTermSlugs, SORT_REGULAR) will deduplicate without any checks needed Commented Jul 5, 2018 at 9:54

1 Answer 1

0

try this code

<?php
$arr = array( 0 => array("slug"=>"exampleslug1","tax"=>"exampletax1"),
             1 => array("slug"=>"exampleslug2","tax"=>"exampletax2"),
             2 => array("slug"=>"exampleslug2","tax"=>"exampletax3"));
foreach($arr as $key => $value){
    if($value['slug']=="exampleslug2" && $value['tax']=="exampletax2"){
        echo "true"."<br>";
    }else{
        echo "false"."<br>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

if($value == ['slug'=>"exampleslug2",'tax'=>"exampletax2"]) {
Thnx i ended up using something like your code. I'm doing a foreach to set the value if it exists, en then underneath if the value is not set to run my script ( i needed it to happen if the code didn't exist yet) foreach($arr as $key => $value) { if ($value === ['slug' => $termSlug, 'tax' => $termTax]) { $alreadySet = true; continue; } }
instead of thnx accept the answer so every one can see this answer @tic

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.