1

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

Rahul Chokshi
  • 670
  • 4
  • 18
Steeve
  • 423
  • 2
  • 9
  • 23

4 Answers4

1
    $height = array_combine(array_keys($ar), array_column($ar,'height'));
    $width = array_combine(array_keys($ar), array_column($ar,'width'));
TsV
  • 629
  • 4
  • 7
0
$array['thumbnail']['width']

will return 150

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

will return 150

Dieter Kräutl
  • 657
  • 4
  • 8
0

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

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

The fourth bird
  • 154,723
  • 16
  • 55
  • 70