【问题标题】:PHP array filter by pre text from valuePHP数组通过值的前置文本过滤
【发布时间】:2018-09-04 12:30:05
【问题描述】:

我有一个像下面这样的数组

Array
(
    [0] => country-indonesia
    [1] => country-myanmar
    [2] => access-is_airport
    [3] => heritage-is_seagypsy
)

从该数组中,我只想为 [country] 、[access]、[heritage] 创建单独的数组

因此,我必须在“-”之前通过文本检查数组值。我不知道该怎么做。所以我不能在这里应用代码。我只有 PHP 中的数组

【问题讨论】:

  • 在循环中通过explode("-", $val) 进行爆炸。

标签: php arrays filtering


【解决方案1】:

修改后的答案,如果您只想获取特定类型。

<?php

$arr = [
  'country-indonesia',
  'country-myanmar',
  'access-is_airport',
  'heritage-is_seagypsy',
];

$new_array = [];
$types = ['country', 'heritage', 'access'];

foreach ($arr as $element) {
  $fac = explode('-', $element);
  foreach ($types as $type) {
    if ($fac[0] === $type) {
      $new_array[$type][] = $fac[1];
    }
  }
}

$country = $new_array['country'];
$access = $new_array['access'];
$heritage = $new_array['heritage'];


var_dump($new_array);

【讨论】:

    【解决方案2】:

    使用array_walk 3 行代码的简单解决方案

    <?php
    
    $arr = [
        'country-indonesia',
        'country-myanmar',
        'access-is_airport',
        'heritage-is_seagypsy',
    ];
    
    $new_array = [];
    array_walk($arr, function($item) use (&$new_array){
        //if(false === strpos($item, '-')) return;
        list($key,$value) = explode('-', $item, 2);
        $new_array[$key][] = $value;
    });
    
    
    print_r($new_array);
    

    给出这个输出:

    Array
    (
        [country] => Array
            (
                [0] => indonesia
                [1] => myanmar
            )
    
        [access] => Array
            (
                [0] => is_airport
            )
    
        [heritage] => Array
            (
                [0] => is_seagypsy
            )
    
    )
    

    如果您不想要空的和重复的条目:

    <?php
    
    $arr = [
        'country-indonesia',
        'country-myanmar',
        'access-is_airport',
        'heritage-is_seagypsy',
    ];
    
    $new_array = [];
    array_walk($arr, function($item) use (&$new_array){
        if(false === strpos($item, '-')) return;
        list($key,$value) = explode('-', $item, 2);
        if(empty($value) || array_key_exists($key, $new_array) && in_array($value, $new_array[$key])) return;
        $new_array[$key][] = $value;
    });
    
    
    print_r($new_array);
    

    【讨论】:

      【解决方案3】:

      您可以使用explodein_array 函数来做到这一点

       <?php
         $arr = ["country-indonesia","country-myanmar","access-is_airport","heritage-is_seagypsy"];
        $newArr = array();
         foreach($arr as $k=> $val){
            $valArr = explode("-", $val);
              if(!in_array($valArr[0], $newArr)){
                 $newArr[] = $valArr[0];
              }
         }
      
         print_r($newArr);
      
        ?>
      

      live demo

      【讨论】:

      • 感谢您的回答。可能我解释的不好。我从你的代码中得到了一个带有预值的数组。我想设置这样的数组 $country= array([0]=>indonesia [1] => thailand)
      • @Lemon :那我推荐 Kerkouch 回答。
      【解决方案4】:

      您需要 PHP 的 strpos() 函数。 只需循环遍历数组的每个元素并尝试以下操作:

      if( strpos($array[$i], "heritage") != false )
      {
          // Found heritage, do something with it
      }
      

      (喂宝宝时用手机写的粗略例子,可能有错别字,但这是您需要的基础知识)

      在此处进一步阅读:http://php.net/manual/en/function.strpos.php

      【讨论】:

        【解决方案5】:
        //first lets set a variable equal to our array for ease in working with i.e
        // also create a new empty array to hold our filtered values 
        $countryArray = array();
        $accessArray = array();
        $heritageArray = array();
        
        $oldArray = Array(country-indonesia, country-myanmar, access-is_airport, heritage-is_seagypsy);
        
        //Next loop through our array i.e
        
        for($x = 0; $x < count($oldArray); $x++){
        // now filter through the array contents
        $currentValue = $oldArray[$x];
        // check whether the current index has any of the strings in it  [country] ,[access], [heritage] using the method : strpos()
        
        if(strpos($currentValue,'country')){
        //if this particular value contains the keyword push it into our new country array //using the array_push() function.
        array_push($countryArray,$currentValue);
        }elseif(strpos($currentValue,'access')){
          // else check for the access string in our current value
          // once it's found the current value will be pushed to the $accessArray
        array_push($accessArray,$currentValue);
        }elseif(strpos($currentValue,'heritage')){
         // check for the last string value i.e access. If found this too should be pushed to //the new heritage array i.e
        array_push($heritageArray,$currentValue);
        }else{
        // do nothing
        }
        }
        

        //我相信应该可以:干杯希望

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-11
          • 2019-11-28
          • 2017-08-06
          • 2021-03-05
          • 1970-01-01
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多