user6043723
user6043723

Reputation: 177

PHP - Check if values in one array exist in another array multi-dimensional array

I have the following two arrays.

This is a flat array (string: $second_names):

Array ( [0] => Cars [3] => Bikes [8] => Trucks ) //$second_names

I have this multidimensional array - (string: $premiumCatArraySets):

Array
(
    [0] => Array
        (
            [primary-category] => Automobiles
            [secondary-category] => Cars
            [tertiary-category] => Fiat Punto
        )
[1] => Array
    (
        [primary-category] => Automobiles
        [secondary-category] => Cars
        [tertiary-category] => BMW
    )

[2] => Array
    (
        [primary-category] => Automobiles
        [secondary-category] => Bikes
        [tertiary-category] => Honda
    )

[4] => Array
    (
        [primary-category] => Automobiles
        [secondary-category] => Trucks
        [tertiary-category] => Iveco
    )

[6] => Array
    (
        [primary-category] => Automobiles
        [secondary-category] => Cars
        [tertiary-category] => Mercedes
    )

[9] => Array
    (
        [primary-category] => Automobiles
        [secondary-category] => Cars
        [tertiary-category] => Toyota
    )

I am trying to use in_array to see whether the values in the flat array exist and output the brand of the car.

This is what I tried

foreach ($second_names as $second_name) {//Vechile type e.g. car, truck, bike
    if(in_array($second_name, $premiumCatArraySets)){
        echo '<h2>'.$second_name.'</h2>';
        foreach ($third_names as $third_name) {// e.g. Fiat, BMW, Toyota
            echo $third_name.'<br/>';
        }
    }
}

The line for if(in_array($second_name, $premiumCatArraySets)){ doesn't seem to be displaying anything.

Upvotes: 1

Views: 1851

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

The solution using call_user_func_array, array_merge_recursive, array_keys, array_flip, array_intersect_key and implode functions:

// grouping each category preserving the position of each item
$groups = call_user_func_array('array_merge_recursive', $premiumCatArraySets);
foreach ($second_names as $name) {
    $indexes = array_flip(array_keys($groups['secondary-category'], $name));
    echo '<h2>'.$name.'</h2>';
    echo implode(", ", array_intersect_key($groups['tertiary-category'], $indexes)) .'<br/>';
}

The output:

<h2>Cars</h2>Fiat Punto, BMW, Mercedes, Toyota<br/><h2>Bikes</h2>Honda<br/><h2>Trucks</h2>Iveco<br/>

Upvotes: 1

zakhefron
zakhefron

Reputation: 1443

Try

$output = [];
foreach($premiumCatArraySets as $key => $value){    
    if(in_array($value["secondary-category"],$second_names)){
        if(!isset($output[$value["secondary-category"]])){
            $output[$value["secondary-category"]]  = [];
        }        
        $output[$value["secondary-category"]][] = $value["tertiary-category"];
    }
}
foreach($output as $key => $value){ 
    echo '<h2>'.$key."</h2>";
        echo implode(", ",$value)."<br/>";
}

Output

Cars    
Fiat Punto, BMW, Mercedes, Toyota

Bikes    
Honda

Trucks    
Iveco

Refer : Demo

Upvotes: 1

Tibin Paul
Tibin Paul

Reputation: 846

If my understanding is correct, you have to get the vehicle brand from the second array for each vehicles in the first array. You could do something like below. This is a basic script.

<?php

$vehicles = ['Cars', 'Bikes', 'Trucks'];

$details = [
    [
        'primary-category' => 'Automobiles',
        'secondary-category' => 'Cars',
        'tertiary-category' => 'BMW'
    ],
    [
        'primary-category' => 'Automobiles',
        'secondary-category' => 'Trucks',
        'tertiary-category' => 'Benz'
    ]
];

foreach ($vehicles as $vehicle) {
    foreach ($details as $detail) {
        if ($vehicle == $detail['secondary-category']) {
            echo $detail['tertiary-category'];
            break;
        }
    }
}

?>

Upvotes: 2

Related Questions