user1991171
user1991171

Reputation: 23

How to display a list of arrays based on a value within array?

So I have an array like below:

Array
(
[0] => Array
    (
        [model] => COROLLA
        [trim] => L
        [msrp] => 16,800
        [type] => car
    )

[1] => Array
    (
        [model] => COROLLA
        [trim] => LE
        [msrp] => 18,300
        [type] => car
    )

I need to be able to call all unique values of type and iterate through them to return them as if the type is a category. So I need to make a list of all the vehicles with 'car' as a type and another list for all vehicles with 'van' as a type etc. I have this to get unique values:

$type = array();
  foreach($cars as $car){
    $type[] = $car['type'];
  }
$type = array_unique($type);

I am really running into a wall on how to search for their values and return the top level array. i know this is basic but I am struggling.

Upvotes: 0

Views: 50

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

If you have PHP >= 5.5.0 you can get the type associated with their keys like this:

$cars_types = array_column($cars, 'type');

Then you could sort on the type and loop it to access the $cars array:

asort($cars_types);

foreach($cars_types as $key => $type) {
    echo $type; // or $cars[$key]['type']
    echo $cars[$key]['model'];
    //etc
}

Upvotes: 0

Alejandro Arbiza
Alejandro Arbiza

Reputation: 766

The code below will result in an array that groups vehicles by type:

$vbt = array();
foreach ( $cars as $car )
{
    $vbt[ $car['type'] ][] = $car;
}

So now you have an array that looks like this:

/*
$vbt ==  Array
(
    [car] => Array
    (
        [0] => Array
        (
            [model] => COROLLA
            [trim] => L
            [msrp] => 16,800
            [type] => car
        )

        [1] => Array
        (
            [model] => COROLLA
            [trim] => LE
            [msrp] => 18,300
            [type] => car
        )
    )

    [van] => Array
    (
        [0] => Array
        (
            [model] => ODYSSEY
            [trim] => L
            [msrp] => 16,800
            [type] => van
        )
    )
)
*/

From that structure you can access any list of vehicles if you have the type, for instance, to get an array of all type == van you'll do:

$vans = $vbt['van'];

/*
$vans == Array
(
    [0] => Array
    (
        [model] => ODYSSEY
        [trim] => L
        [msrp] => 16,800
        [type] => van
    )
)
*/

If you know already the type and you just want to get the list of vehicles of that type:

$vehicles = array();
$searchType = 'car';
foreach ( $cars as $car )
{
    if ( $car['type'] == $searchType )
    {
        $vehicles[] = $car;
    }
}


/*
$vehicles == Array
    (
        [0] => Array
        (
            [model] => COROLLA
            [trim] => L
            [msrp] => 16,800
            [type] => car
        )

        [1] => Array
        (
            [model] => COROLLA
            [trim] => LE
            [msrp] => 18,300
            [type] => car
        )
    )
*/

Upvotes: 2

Related Questions