Parsing multidimensional array

I have a multidimensional array,

Array
(
    [0] => Array
        (
            [Title] => Icebound.
            [Id] => 1
            [Cooling_Area] => 800
        )

    [1] => Array
        (
            [Title] => Ocean Breeze
            [Id] => 2
            [Cooling_Area] => 750
        )

    [2] => Array
        (
            [Title] => Ice Tundra
            [Id] => 3
            [Cooling_Area] => 900
        )

    [3] => Array
        (
            [Title] => Cool Beans.
            [Id] => 4
            [Cooling_Area] => 300
        )

    [4] => Array
        (
            [Title] => Blue Steel.
            [Id] => 5
            [Cooling_Area] => 930
        )

    [5] => Array
        (
            [Title] => Sub-Zero
            [Id] => 6
            [Cooling_Area] => 500
        )

    [6] => Array
        (
            [Title] => Ice Ice Baby.
            [Id] => 7
            [Cooling_Area] => 670
        )

    [7] => Array
        (
            [Title] => The Frost
            [Id] => 8
            [Cooling_Area] => 850
        )

    [8] => Array
        (
            [Title] => Broken Dreams.
            [Id] => 9
            [Cooling_Area] => 650
        )

    [9] => Array
        (
            [Title] => Chill Out.
            [Id] => 10
            [Cooling_Area] => 600
        )

    [10] => Array
        (
            [Title] => Ice Giant.
            [Id] => 11
            [Cooling_Area] => 950
        )

    [11] => Array
        (
            [Title] => Chill Zone.
            [Id] => 12
            [Cooling_Area] => 650
        )


and am trying to figure how create an array like

["Icebound","Ocean Breeze","Ice Tundra",...]

given its a array of arrays, but would it be something like

foreach($dataArray as $unitArray) {
    foreach($unitArray as $key => $element) { {
    array_push($element);
    }
}

but only get the Title key in the second array?

If you are only going to need the title…

<?php

$movies = [
    [
        "title" => "Rear Window",
        "director" => "Alfred Hitchcock",
        "year" => 1954
    ],
    [
        "title" => "Full Metal Jacket",
        "director" => "Stanley Kubrick",
        "year" => 1987
    ],
    [
        "title" => "Mean Streets",
        "director" => "Martin Scorsese",
        "year" => 1973
    ]
];

$i = 0;
foreach ($movies as $movie) {
    echo $movies[$i]['title'] . '<br>';
    $i++;
}

PHP also has a built in function for this array_column(). Pass it your array and the desired column. it’s also additionally helpful because it has an optional argument which would lable another column in the data array as the key for the result array ( this can come in handy when you are dealing with indexed results from a DB.

hope that helps.

1 Like

array_column example per @dresden_phoenix

<?php

$movies = [
    [
        "title" => "Rear Window",
        "director" => "Alfred Hitchcock",
        "year" => 1954
    ],
    [
        "title" => "Full Metal Jacket",
        "director" => "Stanley Kubrick",
        "year" => 1987
    ],
    [
        "title" => "Mean Streets",
        "director" => "Martin Scorsese",
        "year" => 1973
    ]
];

$titles = array_column($movies, 'title');

foreach ($titles as $title)
echo $title . '<br>';

woaw, just like that?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.