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

2
  • $array['thumbnail']['width'] ? just foreach $array as $key => $data and var_dump $data['width']? Commented Aug 20, 2018 at 11:02
  • display your code Commented Aug 20, 2018 at 11:03

4 Answers 4

1
    $height = array_combine(array_keys($ar), array_column($ar,'height'));
    $width = array_combine(array_keys($ar), array_column($ar,'width'));
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

why you used { infront of $ i have never used it
This page has a nice explanation about the curly braces in the string.
You can also write it like this echo "key: " . $key . " width: " . $value['width'] . " height: " . $value['height'] . "<br>";
0
$array['thumbnail']['width']

will return 150

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

will return 150

Comments

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.

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.