三维数组循环



我真的被困在项目的某个点上了。我试着把二维数组转换成三维数组。目前我在这一点:

array (size=4)
  0 => 
    array (size=4)
      0 => string 'foo' (length=3)
      1 => string 'a' (length=0)
      2 => string 'b' (length=0)
      3 => string '0' (length=1)
  1 => 
    array (size=4)
      0 => string 'bar' (length=3)
      1 => string 'a' (length=0)
      2 => string 'b' (length=0)
      3 => string '1' (length=1)
  2 => 
    array (size=4)
      0 => string 'bas' (length=3)
      1 => string 'a' (length=0)
      2 => string 'b' (length=0)
      3 => string '1' (length=1)
  3 => 
    array (size=4)
      0 => string 'tas' (length=3)
      1 => string 'a' (length=0)
      2 => string 'b' (length=0)
      3 => string '0' (length=1)

我想做的是用array[][3]的键值创建一个新的数组(我希望这是有意义的),所以像这样:

array (size=2)
    0 => array (size=2)
        0 => array (size=2)
                   0 => string 'foo' (length=3)
                   1 => string 'a' (length=0)
                   2 => string 'b' (length=0)
        1 => array (size=2)
                   0 => string 'tas' (length=3)
                   1 => string 'a' (length=0)
                   2 => string 'b' (length=0)
    1 => array (size=2)
        0 => array (size=2)
                   0 => string 'bar' (length=3)
                   1 => string 'a' (length=0)
                   2 => string 'b' (length=0)
        1 => array (size=2)
                   0 => string 'bas' (length=3)
                   1 => string 'a' (length=0)
                   2 => string 'b' (length=0)

现在这是我能解释它的最好方式,我不确定我是否有任何意义,或者如果这甚至是可能的,但如果有人能对这一点有所了解,那将是非常感激的!

提前感谢,

托马斯

$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);

function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
    $key=$v[$kn];
    if(!array_key_exists($v[$kn],$new_array)){
    $new_array[$key]=array();
    }
    unset($v[$kn]);
    $new_array[$key][] = $v;
}
return $new_array;
}

相关内容

  • 没有找到相关文章

最新更新