1

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.

3 Answers 3

2

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;
        }
    }
}

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

7 Comments

Thanks. Its telling me PHP Fatal error: Function name must be a string for if ($vehicle == $detail['secondary-category']) {
You tried executing the same script in my answer? Works charm for me!
Yeah, I did to try it on a small scale.$secondary_names = array('Cars', 'Bikes', 'Trucks'); $details = array( 0 => array( 'primary-category' => 'Automobiles', 'secondary-category' => 'Cars', 'tertiary-category' => 'BMW', ), 1 => array( 'primary-category' => 'Automobiles', 'secondary-category' => 'Trucks', 'tertiary-category' => 'Benz', ) );
Copy and paste the entire script causing error here. I will take a look.
Ok. I guess I managed. I used circular brackets instead of square brackets in my script. Because I used find and replace function :-)
|
1

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

Comments

1

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/>

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.