代码之家  ›  专栏  ›  技术社区  ›  Lemon Kazi

PHP数组按值中的预文本筛选

  •  0
  • Lemon Kazi  · 技术社区  · 6 年前

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

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

    从这个数组中,我只想为[国家]、[访问]、[遗产]创建单独的数组。

    因此,我必须在“—”之前按文本检查数组值。我不知道该怎么做。所以我不能在这里应用代码。我只是用PHP编写了数组

    5 回复  |  直到 6 年前
        1
  •  2
  •   Anish Sheela Stephen Salve    6 年前

    一个修改过的答案,如果您只想获得特定的类型。

    <?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
  •  2
  •   Kerkouch    6 年前

    一个简单易行的3行代码解决方案 array_walk

    <?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
  •  1
  •   Niklesh Raut    6 年前

    你可以用 explode in_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

        4
  •  0
  •   Lemon Kazi    6 年前

    你需要PHP的 strpos() 功能。 只需循环遍历数组的每个元素,然后尝试如下操作:

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

    (粗略的例子写在我的手机喂养婴儿时,可能有打字错误,但这是你所需要的基础)

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

        5
  •  0
  •   NickZero    6 年前
    //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
    }
    }
    

    //我认为这应该有效:干杯,希望