从另一个表中获取foreach数据

时间:2016-03-02 07:57:22

标签: php foreach php-5.3

我有以下代码:

Array
(
[0] => Array
    (
        [id] => 89783187
        [level] => 1
        [score] => 8952
    )

[1] => Array
    (
        [id] => 100563554
        [level] => 1
        [score] => 8034
    )

[2] => Array
    (
        [id] => 98857523
        [level] => 1
        [score] => 744
    )

[3] => Array
    (
        [id] => 76413076
        [level] => 2
        [score] => 6944
    )

[4] => Array
    (
        [id] => 100287731
        [level] => 2
        [score] => 6570
    )

) 现在我想创建一个具有以下结构的数组,如下所示:

Array
(
 [1] => Array
      (
       [1] => Array
         (
           [id] => 1212
           [level]=>34343
           [score]=>878
         )
        [2] => Array
         (
            [id] => 23
            [level]=>878
            [score]=>76
         )
      )
  [2] => Array
     ...........

我尝试使用foreach循环,但它无效。你能帮我吗 ?提前致谢并抱歉我的英文

1 个答案:

答案 0 :(得分:1)

这对你有用

<?php 
$array =array(0=>array('id'=>1,'level'=>1,'score'=>111),1=>array('id'=>2,'level'=>2,'score'=>222),2=>array('id'=>3,'level'=>3,'score'=>333),3=>array('id'=>4,'level'=>4,'score'=>444));
$new_array =array_chunk($array,2);
print_r($new_array);
?>

这将输出

Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [level] => 1
                        [score] => 111
                    )

                [1] => Array
                    (
                        [id] => 2
                        [level] => 2
                        [score] => 222
                    )

            )

        [1] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [level] => 3
                        [score] => 333
                    )

                [1] => Array
                    (
                        [id] => 4
                        [level] => 4
                        [score] => 444
                    )

            )

    )