Sergio
Sergio

Reputation: 145

Compare multidimensional array with another array in PHP and get the values from the multidimensional array

I have an multidimensional array from an XML file which I want to compare with a normal array. I need to compare the [tag] name in the multidimensional array with the name in the other array and get value from the multidimensional array that belongs to the tag.

Array
(
    [0] => Array
        (
            [tag] => DOCUMENT
            [type] => open
            [level] => 1
        )

    [1] => Array
        (
            [tag] => SENDERID
            [type] => complete
            [level] => 2
            [value] => TEST
        )

    [2] => Array
        (
            [tag] => SENDERSHORTNAME
            [type] => complete
            [level] => 2
        )

    [3] => Array
        (
            [tag] => RECIPIENTID
            [type] => complete
            [level] => 2
            [value] => VGLEE
        )
)

Second array which I need to compare it with the multidimensional array:

$compare_array = array('DOCUMENT', 'SENDERID', 'SENDERSHORTNAME', 'RECIPIENTID');

Now I want to check if the key from $compare_array is matched in the multidimensional array. If so, I want to grab the value from the multidimensional array and make a variable with the name from the compare_array and append the value to the variable.

I made a for loop:

for($i = 0; $i < $count; $i++){
    if($values[$i]['tag'] == 'SENDERID'){
        $SENDER = $values[$i]['value'];
    }
    if($values[$i]['tag'] == 'RECIPIENTID'){
        $RECIPIENTID = $values[$i]['value'];
    }
    if($values[$i]['tag'] == 'IREF'){
        $IREF = $values[$i]['value'];
    }
    if($values[$i]['tag'] == 'DOCUMENTNUMBER'){
        $DOCUMENTNUMBER = $values[$i]['value'];
    }
}

Upvotes: 0

Views: 1253

Answers (3)

The fourth bird
The fourth bird

Reputation: 163207

For your example data, you could use array_reduce and use in_array to check if the tag is present in $compare_array

If you must make a variable with the name from the $compare_array and append the value to the variable you might use extract with a flag that suits your expectations.

The value is not present in all the example data so you might also check if that exists.

$compare_array = array('DOCUMENT', 'SENDERID', 'SENDERSHORTNAME', 'RECIPIENTID');
$result = array_reduce($arrays, function($carry, $item) use ($compare_array) {
    if(isset($item["value"]) && in_array($item["tag"], $compare_array, true)) {
        $carry[$item["tag"]] = $item["value"];
    }
    return $carry;
});
extract($result, EXTR_OVERWRITE);

echo $SENDERID;
echo $RECIPIENTID;

Demo

Upvotes: 1

Lovepreet Singh
Lovepreet Singh

Reputation: 4840

Try following code:

$array = []; //Complete array to be parsed.
$compare_array = array('DOCUMENT', 'SENDERID', 'SENDERSHORTNAME', 'RECIPIENTID');


foreach ($array as $value) {
    if (in_array($value["tag"], $compare_array)) {
        $$value["tag"] = $value["value"];
    }
}

It will traverse through array and if tag name matched to value $compare_array, then define a variable with tag name and initialize with value.

Upvotes: 1

mindaJalaj
mindaJalaj

Reputation: 448

You can try using array_in() in this case, below is the sample code

for($i = 0; $i < $count; $i++){
    if(in_array($values[$i]['tag'],$compare_array)){
        $value = $values[$i]['value'];
    }

}

For reference in_array()

Upvotes: 1

Related Questions