Steeve
Steeve

Reputation: 443

How to get inner array value using PHP?

I have this array

Array
(
    [thumbnail] => Array
        (
            [width] => 150
            [height] => 150
            [crop] => 1
        )

    [medium] => Array
        (
            [width] => 300
            [height] => 300
            [crop] => 
        )

    [medium_large] => Array
        (
            [width] => 768
            [height] => 0
            [crop] => 
        )

    [large] => Array
        (
            [width] => 1024
            [height] => 1024
            [crop] => 
        )

    [twentyseventeen-featured-image] => Array
        (
            [width] => 2000
            [height] => 1200
            [crop] => 1
        )

    [twentyseventeen-thumbnail-avatar] => Array
        (
            [width] => 100
            [height] => 100
            [crop] => 1
        )

)

If I use print_r(array_keys($arraydata, true)) I get the value of thumbnails name but I want to get the width and height and corresponding width and height value of all. I can use foreach tried with keys but it did not work

Upvotes: 1

Views: 3319

Answers (4)

The fourth bird
The fourth bird

Reputation: 163467

You could use 1 foreach and use the $key for the value of the thumbnails and for the value you could use the keys width and height:

foreach($arraydata as $key => $value) {
    echo "key: $key: width: ${value['width']} height: ${value['height']}<br>";
}

Demo

Upvotes: 0

TsV
TsV

Reputation: 639

    $height = array_combine(array_keys($ar), array_column($ar,'height'));
    $width = array_combine(array_keys($ar), array_column($ar,'width'));

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94672

You can of course use a foreach loop, 2 in fact as you have an array within an array.

foreach ($array as $size => $subarr) {
    echo $size. '<br>';

    foreach ( $subarr as $name => $val ) {
        // dont want the crop information
        if ( $name == 'crop' ) continue;

        echo $name . ' = '. $val . '<br>';
    }
}        

Output should be comething like

thumbnail
width = 150
height = 150

. . .

This may not be the format of output you really want but you can add that around this basic flow.

Upvotes: 0

Dieter Kr&#228;utl
Dieter Kr&#228;utl

Reputation: 677

$array['thumbnail']['width']

will return 150

$array['thumbnail']['height']

will return 150

Upvotes: 0

Related Questions