Reputation: 443
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
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>";
}
Upvotes: 0
Reputation: 639
$height = array_combine(array_keys($ar), array_column($ar,'height'));
$width = array_combine(array_keys($ar), array_column($ar,'width'));
Upvotes: 1
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
Reputation: 677
$array['thumbnail']['width']
will return 150
$array['thumbnail']['height']
will return 150
Upvotes: 0