2016-05-15 41 views
1

是的,这已经通过使用不同的方法被无数次地回答,在大多数情况下,它可以工作,但不适用于我的情况,请允许我解释。

以键值为基础的组数组

我从一个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 
       ) 

我正在寻找这个This answer这是最接近我需要,但没有工作,这就是为什么我要求帮助。

更新:我想我只是解决了它...

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

看起来很不错。

回答

0

你可以从explode('/', $item['cats'])[1]关键:

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

也许你帮助这个代码:

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" 
    } 
    } 
} 

UPDATE

array_filter - 优雅的解决方案但编号为的类别将被忽略。如果是的话,那么空($键)在周期比array_filter - 因为它也通过数组元素

+0

这是对我的更新同样的逻辑,。请参阅代码中的if($ key ==“”),而是使用array_filter(),它的作用相同,您的答案在更新后发布了一分钟,因此不允许我选择答案只是尚未... – Tanker

+0

array_filter - 优雅的解决方案,但ID为0的类别将被忽略。 如果是这样,那么!empty($ key)在循环中比array_filter更好 - 因为它也通过数组元素 –

0

这可能是扭曲的变通,但我敢打赌它诀窍:

 <?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); 

试试吧,让我们知道如何去....