0

I have the following array called $thumbs_meta and I want to get all of the filenames from the sizes subarray so that I can count them and glue them together into a comma-separated string.

Array
(
    [width] => 1024
    [height] => 768
    [file] => 2018/08/Penguins.jpg
    [sizes] => Array
        (
            [thumbnail] => Array
                (
                    [file] => Penguins-150x150.jpg
                    [width] => 150
                    [height] => 150
                    [mime-type] => image/jpeg
                )

            [medium] => Array
                (
                    [file] => Penguins-300x225.jpg
                    [width] => 300
                    [height] => 225
                    [mime-type] => image/jpeg
                )

            [medium_large] => Array
                (
                    [file] => Penguins-768x576.jpg
                    [width] => 768
                    [height] => 576
                    [mime-type] => image/jpeg
                )

            [large] => Array
                (
                    [file] => Penguins-1024x768.jpg
                    [width] => 1024
                    [height] => 768
                    [mime-type] => image/jpeg
                )

            [twentyseventeen-thumbnail-avatar] => Array
                (
                    [file] => Penguins-100x100.jpg
                    [width] => 100
                    [height] => 100
                    [mime-type] => image/jpeg
                )

        )
)

I tried foreach, but it's one step more so it's not working. Should I loop twice?

Here is my present foreach which does not work:

foreach($thumbs_meta as $key => $value) {
        $fullname .=$value['file'].',';

                            }
     $thumbs_count = count( $thumbs_meta );

Specifically, I want to isolate the file values from these keys: thumbnail, medium, large, medium_large, and twentyseventeen-thumbnail-avatar

My expected count would be 5

My comma-separated string would be:

Penguins-150x150.jpg,Penguins-300x225.jpg,Penguins-768x576.jpg,Penguins-1024x768.jpg,Penguins-100x100.jpg
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
janeman
  • 11
  • 3
  • Your exact desired result is what? Does `Penguins-100x100.jpg` qualify? Literally, the is only one `thumbnail` in your sample data, so I don't see the point in looping. Is this a realistic depication of your project data? – mickmackusa Aug 23 '18 at 02:39
  • 1
    @mickmackusa under sizes array u can see thumbnails, medium, medium_large, large etc and inside all there is file name. i want to count the file name and list all files seperated by comma . in this case output will be count 5 and file names=Penguins-150x150.jpg,Penguins-300x225.jpg,Penguins-768x576.jpg,Penguins-1024x768.jpg,Penguins-100x100.jpg – janeman Aug 23 '18 at 02:48
  • Looks like maybe both of your references to `$thumbs_meta` should really be `$thumbs_meta['sizes']`? – Greg Schmidt Aug 23 '18 at 02:53
  • @GregSchmidt but it will list all file names under sizes ??? – janeman Aug 23 '18 at 02:55
  • If the array you've shown is the `$thumbs_meta`, then `foreach($thumbs_meta['sizes'] as $key => $value)` will sequentially have `thumbnail`, `medium`, `medium_large`, etc. as the `$key`, and the `$value` will be the arrays which include `'file'`, as your code seems to expect. – Greg Schmidt Aug 23 '18 at 02:58
  • Related: https://stackoverflow.com/questions/4705814/create-a-comma-separated-string-from-a-single-column-of-an-array-of-objects – mickmackusa Jul 27 '22 at 10:46

1 Answers1

1

array_column() on the sizes subarray will get you want in a very succinct and readable fashion.

Code: (Demo)

$thumbnail_files = array_column($thumbs_meta['sizes'], 'file');
var_export($thumbnail_files);

echo "\nCount: " , count($thumbnail_files) , "\n\n";
echo "Imploded: " , implode(',', $thumbnail_files);

Output:

array (
  0 => 'Penguins-150x150.jpg',
  1 => 'Penguins-300x225.jpg',
  2 => 'Penguins-768x576.jpg',
  3 => 'Penguins-1024x768.jpg',
  4 => 'Penguins-100x100.jpg',
)
Count: 5

Imploded: Penguins-150x150.jpg,Penguins-300x225.jpg,Penguins-768x576.jpg,Penguins-1024x768.jpg,Penguins-100x100.jpg
mickmackusa
  • 43,625
  • 12
  • 83
  • 136