如何使用while或foreach循环此数组

时间:2016-01-20 07:51:16

标签: php arrays foreach

我有这个数组

$data = array(
"id" => "1",
"code" => "5",
"name" => "AAA",
"id" => "2",
"code" => "6",
"name" => "VVV",
"id" => "3",
"code" => "7",
"name" => "DDD"
);

我尝试使用foreach这样的循环代码

foreach($data as $x=>$rows)
{
     $tmpdata['alphabet'][$x] =  $rows;
}

我希望得到这样的输出:

[alphabet] => Array
(
    [1] => Array
    (
        [id] => 1
        [code] => 5
        [name] => AAA
    )

    [2] => Array
    (
        [id] => 2
        [code] => 6
        [name] => VVV
    )

    [3] => Array
    (
        [id] => 3
        [code] => 7
        [name] => DDD
    )
)

我尝试使用foreachwhile之类的循环方法,但没有得到上面的输出。

3 个答案:

答案 0 :(得分:1)

您的array

无效

改变
$data = array("id"=>"1","code"=>"5","name"=>"AAA","id"=>"2","code"=>"6",
               "name"=>"VVV","id"=>"3","code"=>"7","name"=>"DDD");

$data = array(
    array("id"=>"1","code"=>"5","name"=>"AAA"),
    array("id"=>"2","code"=>"6","name"=>"VVV"),
    array("id"=>"3","code"=>"7","name"=>"DDD")

);

然后您可以使用foreach

foreach($data as $x=>$rows)
{
     $tmpdata['alphabet'][$x] =  $rows;
}
print_r($tmpdata); // Print Array

总之,你可以得到像这样的欲望输出

$data['alphabet'][] = array("id"=>"1","code"=>"5","name"=>"AAA");
$data['alphabet'][] = array("id"=>"2","code"=>"6","name"=>"VVV");
$data['alphabet'][] = array("id"=>"3","code"=>"7","name"=>"DDD");

print_r($data);

答案 1 :(得分:1)

创建一个多维数组:

$data = array(
    array(
        "id"=>"1",
        "code"=>"5",
        "name"=>"AAA"
    ),
    array(
        "id"=>"2",
        "code"=>"6",
        "name"=>"VVV"
    ),
    array(
        "id"=>"3",
        "code"=>"7",
        "name"=>"DDD"
    )  
);

foreach($data as $x => $rows){
    $tmpdata['alphabet'][$x] =  $rows;
}

var_dump($tmpdata);

答案 2 :(得分:0)

首先,您需要更改数组:

$data = array(
            array("id"=>"1","code"=>"5","name"=>"AAA"),
            array("id"=>"2","code"=>"6","name"=>"VVV"),
            array("id"=>"3","code"=>"7","name"=>"DDD")
        );

你可以解决这个问题:

$newArr['alphabet'] = $data;
echo "<pre>";
print_r($newArr);

第二个解决方案,如果你想从1而不是0开始INDEX而不是你可以用作:

$newArr = array();
$i = 1;
foreach ($data as $key => $value) {
    $newArr['alphabet'][$i] = $value;   
    $i++;
}

第一个解决方案的结果:

Array
(
    [alphabet] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [code] => 5
                    [name] => AAA
                )

            [1] => Array
                (
                    [id] => 2
                    [code] => 6
                    [name] => VVV
                )

            [2] => Array
                (
                    [id] => 3
                    [code] => 7
                    [name] => DDD
                )    
        )    
)

第二个解决方案的结果:

Array
(
    [alphabet] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [code] => 5
                    [name] => AAA
                )

            [2] => Array
                (
                    [id] => 2
                    [code] => 6
                    [name] => VVV
                )

            [3] => Array
                (
                    [id] => 3
                    [code] => 7
                    [name] => DDD
                )    
        )    
)