关联数组键数

Associative arrays key count

我有这个数组:

$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>' ]

我只想计算 'local images' 数组中的第一个键。即:1.jpg、2.jpg、3.jpg、4.jpg,不是 link1、link2、link3、link4。

我正在使用:

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

但它输出的是 8,因为我知道这个数组总共有 8 个键,但是我只想定位“.jpg”键。有办法实现吗?

您可以使用过滤器,然后对过滤后的数组应用计数。

<?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));

你可以在这里查看https://phpsandbox.io/n/white-sound-eken-zjnnp

您需要遍历键并可以使用正则表达式匹配来检查它们是否以 .jpg 结尾。

<?php

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

echo getJpgCount($config);