【问题标题】:How to get max number from array key matching a pattern如何从匹配模式的数组键中获取最大数量
【发布时间】:2013-10-17 20:21:38
【问题描述】:

我有一个数组

$_images['image[1]'] = image1.jpg
$_images['image[2]'] = image2.jpg
$_images['image[3]'] = image3.jpg
$_images['image[4]'] = image4.jpg

如何找到匹配模式图像[] 的密钥中的最大数量/计数为 4。

感谢任何帮助。

【问题讨论】:

  • count(preg_grep('/pattern/', $your_array))?
  • 你想从 _images[image[?]] 获取最大密钥?
  • $_images[image[1]] 是什么意思!?
  • @mark-b preg_grep 正是我想要的!如何选择你的答案?

标签: php regex arrays array-key


【解决方案1】:

我不太明白你在问什么,但如果你的意思是

$_images[1] = 'image1.jpg';
$_images[2] = 'image2.jpg';
$_images[3] = 'image3.jpg';
$_images[4] = 'image4.jpg';

那么就做echo count($_images);

如果你的意思是

$images[1] = 'image1.jpg';
$images[2] = 'image2.jpg';
$images[3] = 'image3.jpg';
$images[4] = 'image4.jpg';

$_images = array();
array_push($_images, $images); //array of arrays

然后做

echo count($_images, COUNT_RECURSIVE) - 1; //keep in mind your parent array must only have 1 array inside it, or you have to do -x where x = number of your second-level arrays 

如果你的意思是别的,那么你必须解释更多......

【讨论】:

    【解决方案2】:

    感谢马克 B!

    count(preg_grep('/images[([0-9]+)]/', $_images));

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多