0

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.

1
  • 1
    Could you please rephrase what exactly you want? (e.g. providing an example output would help a lot) Commented Sep 25, 2014 at 22:01

2 Answers 2

2

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
        )
    )
*/
Sign up to request clarification or add additional context in comments.

Comments

0

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
}

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.