根据键值对数组进行分组


Group array base on key value

是的,这个问题已经用不同的方法回答了无数次,在大多数情况下它有效,但在我的情况下无效,请允许我解释一下

我从CSV文件构建一个数组,这是容易的部分,困难的部分是,我必须从第一个数组构建另一个数组。根据一个键值对结果进行分组,问题是这个值不是数组,也不是简单的字符串
这是CSV文件数组中的一个示例

[0] => Array
     (
       [key_1] => FOO
       [cats] => /30/
       [key_2] => FTU-1
     )
[1] => Array
     (
       [key_1] => FOO
       [cats] => /30/
       [key_2] => FTU-2
     )
[2] => Array
     (
       [key_1] => FOO
       [cats] => /30/10/
       [key_2] => FTU-3
     )
[3] => Array
     (
       [key_1] => FOO
       [cats] => /15/
       [key_2] => FTU-4
     )
[4] => Array
     (
       [key_1] => FOO
       [cats] => /10/
       [key_2] => FTU-5
     )
[0] => Array
     (
       [key_1] => FOO
       [cats] => /15/
       [key_2] => FTU-6
     )

基于列cats:,最终数组必须看起来像这样

[30] => Array 
        (
           [0] => Array 
                 (
                   [key_1] => FOO
                   [cats] => /30/
                   [key_2] => FTU-1
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /30/
                   [key_2] => FTU-2
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /30/10/
                   [key_2] => FTU-3
                 )
[15] => Array 
        (
           [0] => Array 
                 (
                   [key_1] => FOO
                   [cats] => /15/
                   [key_2] => FTU-4
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /15/
                   [key_2] => FTU-6
                 )
[10] => Array 
        (
           [0] => Array 
                 (
                   [key_1] => FOO
                   [cats] => /30/10/
                   [key_2] => FTU-3
                 )
           [1] => Array
                 (
                   [key_1] => FOO
                   [cats] => /10/
                   [key_2] => FTU-5
                 )

我一直在寻找这个答案,这个答案最接近我需要的,但没有奏效,这就是我寻求帮助的原因。

更新:我想我刚刚解决了它…

foreach($firstarr as $k => $v) {
  $cats = array_filter(explode('/', $v['cats']));
  foreach($cats as $ks=>$vs) {
    if(stripos($v['cats'], $vs)){
      $pp[$vs][] = $v;
    }
  }
}

看起来不错。

也许你可以帮助这个代码:

function arrayByOneKey($array, $keyName)
{
    $result = [];
    foreach ($array as $item)
    {
        $keys = explode('/', (string)$item[$keyName]);
        foreach($keys as $key)
        {
            if($key == '')
            {
                continue;
            }
            $result[$key][] = $item;
        }
    }
    return $result;
}
$array = [
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-1',
        'cats' => '/15/'
    ],
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-2',
        'cats' => '/15/'
    ],
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-3',
        'cats' => '/30/10/'
    ],
    [
        'key_1' => 'FOO',
        'key_2' => 'FTU-4',
        'cats' => '/30/10/0'
    ]
];
$array = arrayByOneKey($array, 'cats');
var_dump($array);

结果:

array(4) {
  [15]=>
  array(2) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-1"
      ["cats"]=>
      string(4) "/15/"
    }
    [1]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-2"
      ["cats"]=>
      string(4) "/15/"
    }
  }
  [30]=>
  array(2) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-3"
      ["cats"]=>
      string(7) "/30/10/"
    }
    [1]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-4"
      ["cats"]=>
      string(8) "/30/10/0"
    }
  }
  [10]=>
  array(2) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-3"
      ["cats"]=>
      string(7) "/30/10/"
    }
    [1]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-4"
      ["cats"]=>
      string(8) "/30/10/0"
    }
  }
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      ["key_1"]=>
      string(3) "FOO"
      ["key_2"]=>
      string(5) "FTU-4"
      ["cats"]=>
      string(8) "/30/10/0"
    }
  }
}

更新

array_filter-id为0的优雅解决方案BUT类别将被忽略。如果是,那么!empty($key)在循环中优于array_filter,因为它还通过数组元素

您可以从explode('/', $item['cats'])[1]:获取密钥

$newArray = [];
foreach($originalArray as $item) {
    $key = explode('/', $item['cats'])[1];
    $newArray[$key][] = $item;
}

这可能是一个扭曲的工作,但我敢打赌它会成功:

        <?php
        $arrResultant = array();
        foreach($arr as $intKey=>$arrData){
            $stripped   = preg_replace("#^'/#", "", $arrData['cats']);
            $arrParts   = preg_split("#'/#", $stripped);
            $intCat1    = isset($arrParts[0])? $arrParts[0]:null;
            $intCat2    = isset($arrParts[1])? $arrParts[1]:null;
            if(!array_key_exists($intCat1, $arrResultant)){
                $arrResultant[$intCat1] = array();
                if( stristr($arrData["cats"], $intCat1) ){
                    $arrResultant[$intCat1][] = $arrData;
                }
            }else{
                if( stristr( $arrData["cats"], $intCat1) ){
                    $arrResultant[$intCat1][] = $arrData;
                }
            }
            if(!array_key_exists($intCat2, $arrResultant) && !is_null($intCat2)){
                $arrResultant[$intCat2] = array();
                if( stristr($arrData["cats"], $intCat2) ){
                    $arrResultant[$intCat2][] = $arrData;
                }
            }else{
                if( stristr( $arrData["cats"], $intCat2) ){
                    $arrResultant[$intCat2][] = $arrData;
                }
            }
        }
        var_dump($arrResultant);

试试看,让我们知道它的进展。。。。