Home > Blockchain >  Associative arrays key count
Associative arrays key count

Time:01-07

I have this array:

$config = [
    'gallery_name' => 'My Gallery',
    'unsplash_categories' => ['Nature' => '<img src = "https://source.unsplash.com/300x200/?nature" alt = "Nature" />',
                              'Water' => '<img src = "https://source.unsplash.com/300x200/?water" alt = "Water" />',
                              'Food' => '<img src = "https://source.unsplash.com/300x200/?food" alt = "Food" />', 
                              'Night' => '<img src = "https://source.unsplash.com/300x200/?night" alt = "Night" />'],
    'local_images' => ['1.jpg' => '<img src = "./images/1.jpg" alt = "Clem Onojeghuo" />', 'link1' => '<a href = "https://unsplash.com/@clemono"> Clem Onojeghuo </a>',
                       '2.jpg' => '<img src = "./images/2.jpg" alt = "Jordan Whitt" />', 'link2' => '<a href = "https://unsplash.com/@jwwhitt"> Jordan Whitt </a>',
                       '3.jpg' => '<img src = "./images/3.jpg" alt = "Michael Kucharski" />', 'link3' => '<a href = "https://unsplash.com/@intacts"> Michael Kucharski </a>',
                       '4.jpg' => '<img src = "./images/4.jpg" alt = "Paul Gilmore" />', 'link4' => '<a href = "https://unsplash.com/@pueblovista"> Paul Gilmore </a>' ]

and I want to count ONLY the first keys in the 'local images' array. Ie: 1.jpg, 2.jpg, 3.jpg, 4.jpg, NOT link1, link2, link3, link4.

I am using:

<h1><?php echo(count($config['local_images'])) . " Large Images" ?></h1>

but it is outputting 8 since I know their are 8 keys total for this array, however I only want to target the '.jpg' ones. Is there a way to achieve this?

CodePudding user response:

You can use filter and then apply count on the filtered array.

<?php
    $config = [
        'gallery_name' => 'My Gallery',
        'unsplash_categories' => [
            'Nature' => '<img src = "https://source.unsplash.com/300x200/?nature" alt = "Nature" />',
            'Water' => '<img src = "https://source.unsplash.com/300x200/?water" alt = "Water" />',
            'Food' => '<img src = "https://source.unsplash.com/300x200/?food" alt = "Food" />', 
            'Night' => '<img src = "https://source.unsplash.com/300x200/?night" alt = "Night" />'
        ],
        'local_images' => [
            '1.jpg' => '<img src = "./images/1.jpg" alt = "Clem Onojeghuo" />', 'link1' => '<a href = "https://unsplash.com/@clemono"> Clem Onojeghuo </a>',
            '2.jpg' => '<img src = "./images/2.jpg" alt = "Jordan Whitt" />', 'link2' => '<a href = "https://unsplash.com/@jwwhitt"> Jordan Whitt </a>',
            '3.jpg' => '<img src = "./images/3.jpg" alt = "Michael Kucharski" />', 'link3' => '<a href = "https://unsplash.com/@intacts"> Michael Kucharski </a>',
            '4.jpg' => '<img src = "./images/4.jpg" alt = "Paul Gilmore" />', 'link4' => '<a href = "https://unsplash.com/@pueblovista"> Paul Gilmore </a>' 
        ]
    ];

    $filteredArray = array_filter($config['local_images'], function($k) {
        if(strpos($k, 'jpg') !== false)
            return $k;
    }, ARRAY_FILTER_USE_KEY);

    var_dump(count($filteredArray));

You can check that out here https://phpsandbox.io/n/white-sound-eken-zjnnp

CodePudding user response:

You need to loop over keys and can use a regex match to check if they end with .jpg.

<?php

function getJpgCount($config){
    return count(array_filter(array_keys($config['local_images']),fn($key) => preg_match('/\.jpg$/i', $key)));
}

echo getJpgCount($config);
  •  Tags:  
  • Related