【问题标题】:How to find all the occasions where array items start with a specific letter如何查找数组项以特定字母开头的所有场合
【发布时间】:2018-07-20 07:26:01
【问题描述】:

我想知道我在这里所做的事情是否有其他解决方案:

$aArray = array(
            array('Name' => 'Apple', 'Count' => 10)
            array('Name' => 'Tomato', 'Count' => 23)
            array('Name' => 'Tree', 'Count' => 4)
            array('Name' => 'Potato', 'Count' => 44)
            array('Name' => 'Apple', 'Count' => 73)

//Generate string with the 'Name's'
$aNamesMain = [];
$nCounterMain = 0;
$aNamesMain = array_merge($aNamenMais, array_keys($aArray['Name']));

foreach($aNamesMain as $name) {
    // Check to see which items start with an A
    if (substr('$name', 0, 1) === 'A') { $nCounterMain++; }
}

所以我想知道在我的数组中以特定字母开头的 'Name' => 位置找到了多少次项目。 A 在这种情况下。但是当我现在让它工作时,就像我在上面发布的那样,但是没有更好的方法来实现这一点吗?

一些数组函数之类的,因为我一直在尝试 PHP 手册中的一些,但现在似乎找不到更好的解决方案。

这样我就不必使用foreach 或摆脱数组合并。

如果这不是主题并且需要在代码审查页面上发布,请告诉我,但我认为这是与编程相关的东西。

【问题讨论】:

    标签: php arrays multidimensional-array substring


    【解决方案1】:

    您可以使用array_filteranonymous function 来解决这个问题:

    //your array containing the items.
    $aArray = array(
        array('Name' => 'Apple', 'Count' => 10),
        array('Name' => 'Tomato', 'Count' => 23),
        array('Name' => 'Tree', 'Count' => 4),
        array('Name' => 'Potato', 'Count' => 44),
        array('Name' => 'Apple', 'Count' => 73)
    );
    
    //the character or string you want to search.   
    $startWithChar = 'A';
    
    //get all items of the array starting with the specified character or string.  
    $newArray = array_filter($aArray, function($v) use ($startWithChar) {
        return strpos($v['Name'], $startWithChar) === 0;
    });
    
    echo count($newArray); //2
    

    演示: https://ideone.com/EctCs7

    【讨论】:

    • 为什么不使用return $v['Name'][0] === $startWithChar;
    • 嗨塞巴斯蒂安,我真的很喜欢这个解决方案的外观。但我对这些匿名函数并不熟悉。那么$v['name'] 在这种情况下我的$aArray 中的$v 到底做了什么?
    • @OliCrt - 因为字符串可能为空(在此示例中不是,但可能)。
    • @H.Brendan - $v$aArray 数组的值之一。贯穿$aArray$v 的所有值的array_filter 是当前值,提供给回调(在本例中为匿名函数)。
    • @SebastianBrosch 谢谢你的解释,我有点明白这里发生了什么,也谢谢你的快速回复:)
    【解决方案2】:

    array_filter在这里是个不错的选择。

    $aArray = array(
                array('Name' => 'Apple', 'Count' => 10),
                array('Name' => 'Tomato', 'Count' => 23),
                array('Name' => 'Tree', 'Count' => 4),
                array('Name' => 'Potato', 'Count' => 44),
                array('Name' => 'Apple', 'Count' => 73),
        );
    
    $startWithChar = 'A';
    $newArray = array_filter($aArray, function($e) use ($startWithChar) {
        return substr($e['Name'], 0, 1) === $startWithChar;
    });
    
    echo count($newArray); // 2
    

    【讨论】:

    • 谢谢你的反应,对方早一点,
    【解决方案3】:

    你可以使用类似的东西:

    //initial array
    $x =[
        ['Name' => 'Apple', 'Count' => 10],
        ['Name' => 'Tomato', 'Count' => 23],
        ['Name' => 'Tree', 'Count' => 4],
        ['Name' => 'Potato', 'Count' => 44],
        ['Name' => 'Apple', 'Count' => 73]
    ];
    
    $x = array_filter(array_column($x, 'Name'), function($key) {
        return (string) substr($key, 0, 1) === 'A' ? true : false;
    });
    
    //output array
    echo count($x);  // should be 2 
    

    【讨论】:

      【解决方案4】:

      您可以使用array_reduce 将数组简化为唯一值,这里是以字母“A”开头的名称的数量。

      $aArray = array(
          array('Name' => 'Apple', 'Count' => 10),
          array('Name' => 'Tomato', 'Count' => 23),
          array('Name' => 'Tree', 'Count' => 4),
          array('Name' => 'Potato', 'Count' => 44),
          array('Name' => 'Apple', 'Count' => 73)
      );
      $startWithChar = 'A';
      
      $result = array_reduce($aArray, function($carry, $item) use($startWithChar){
          if(strpos($item['Name'], $startWithChar) === 0)
              $carry++;
      
          return $carry;
      }, 0);
      
      echo $result; //2
      

      【讨论】:

      • 而且不会像array_filter那样创建无用的数组,很好。
      猜你喜欢
      • 2015-07-14
      • 2023-02-20
      • 2020-10-05
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多