0

I have an array and want to get the values from it sorted and modified a bit to an other array, so I can make some comparison with yet another array

This is my array to begin with

Array ( 
[0] => WP_Term Object ( 
[term_id] => 10 
...
[cat_name] => Klantenartikelen 
[category_nicename] => klantenartikelen 
[category_parent] => 0 ) 

[1] => WP_Term Object ( 
[term_id] => 11 
...
[cat_name] => Overwin je Overgang blog 
[category_nicename] => overwin-je-overgang-blog 
[category_parent] => 10 ) 
)

I want to sort out the values of [cat_name] in to the new Array, remove one string from it ( blog) and then compare it with another Array, which looks like this

Array ( 
[1426787445] => Overwin je Overgang
[1426787487] => Overgangstransformatie 
)

The problem with the second Array is that the key changes for every Array (membership secret code), but the values are always the same. (if it is too problematic to get the values from an Array like this, it can be hard-coded)

Then at the end I want to make a conditional logic with the comparison: if the value pairs of the two Array match, to echo a graphic element. There might be cases when one Array contains let's say 2 key/values pairs, and the other only 1, but it would need to pass true if one of the 2 is matching.

Any idea please, how this can be solved?

0

2 Answers 2

1

Given the following variables:

$data = Array ( 
    (object) Array( 
        "term_id" => 10, 
        "cat_name" => "Klantenartikelen", 
        "category_nicename" => "klantenartikelen", 
        "category_parent" => 0
    ),
    (object) Array( 
        "term_id" => 11, 
        "cat_name" => "Overwin je Overgang blog", 
        "category_nicename" => "overwin-je-overgang-blog", 
        "category_parent" => 10
    )
);

$ref = Array ( 
    1426787445 => "Overwin je Overgang",
    1426787487 => "Overgangstransformatie" 
);

You could then do this:

foreach ($data as $obj) {
    // remove "blog"
    $catname = str_replace(" blog", "", $obj->cat_name);
    // find this name in the $ref array
    $matchKey = array_search($catname, $ref);
    if ($matchKey !== false) {
        echo "Found '$catname' at key $matchKey", '<br>';
    }
}

This produces this output:

Found 'Overwin je Overgang' at key 1426787445

At the place of the echo you could then output the image, etc...

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

1 Comment

Thank you, great answer! Cheers!
0

Try this

$catname=$array_column($array, "cat_name");

$count=count($catname)

for($i=0; $i<$count; $i++){

   foreach($anotherarray as $key => $value){

     if($catname[$i]==$anotherarray[$key]){
        echo "graphic";         
     }

   }

}

find any error? post back

Comments

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.