根据键创建新数组

时间:2013-12-13 04:32:57

标签: php arrays

我的数组是这样的:

Array
(
    [0] => Array
        (
            [des_id] => 1
            [des_name] => bagan
            [tran_id] => 1
            [tran_name] => private
            [tran_image] => 1251961905A1.jpg
            [type] => car
            [troute_id] => 10
        )

    [1] => Array
        (
            [des_id] => 1
            [des_name] => bagan
            [tran_id] => 2
            [tran_name] => express
            [tran_image] => bus3.jpg
            [type] => car
            [troute_id] => 13
        )

    [2] => Array
        (
            [des_id] => 1
            [des_name] => bagan
            [tran_id] => 3
            [tran_name] => MyanmarTrain
            [tran_image] => Burma-Gorteikviaduct.jpg
            [type] => train
            [troute_id] => 16
        )

    [3] => Array
        (
            [des_id] => 1
            [des_name] => bagan
            [tran_id] => 4
            [tran_name] => Ayeyarwaddy Cruise
            [tran_image] => boat-ChutzpahToo1.jpg
            [type] => cruise
            [troute_id] => 22
        )

)

我想根据键['type']更改那个数组。如果数组键['type']相同,我想改变那样的数组:

Array
(
   [car] => Array(
        [0]=>Array
                (
                    [des_id] => 1
                    [des_name] => bagan
                    [tran_id] => 1
                    [tran_name] => private
                    [tran_image] => 1251961905A1.jpg
                    [type] => car
                    [troute_id] => 10
                ),
        [1] => Array
                (
                    [des_id] => 1
                    [des_name] => bagan
                    [tran_id] => 2
                    [tran_name] => express
                    [tran_image] => bus3.jpg
                    [type] => car
                    [troute_id] => 13
                )
            ),
   [train]=>Array(
             [0] => Array
                (
                    [des_id] => 1
                    [des_name] => bagan
                    [tran_id] => 3
                    [tran_name] => MyanmarTrain
                    [tran_image] => Burma-Gorteikviaduct.jpg
                    [type] => train
                    [troute_id] => 16
                )
   [cruise]=>Array(
            [0] => Array
                (
                    [des_id] => 1
                    [des_name] => bagan
                    [tran_id] => 4
                    [tran_name] => Ayeyarwaddy Cruise
                    [tran_image] => boat-ChutzpahToo1.jpg
                    [type] => cruise
                    [troute_id] => 22
                )
        )
    )
)

我的意思是,如果键['type']是汽车,我想创建汽车阵列或如果类型是火车我想创建火车阵列或如果类型是巡航我想创建巡航阵列。我不知道如何循环数组。有人请帮助我。非常感谢!

2 个答案:

答案 0 :(得分:2)

这是一种简单的方法:循环数据,然后附加到匹配type值的子数组:

// starting data
$starting_array = array (
    0 => array (
            'des_id' =>  1,
            'des_name' =>  'bagan',
            'tran_id' =>  1,
            'tran_name' =>  'private',
            'tran_image' =>  '1251961905A1.jpg',
            'type' =>  'car',
            'troute_id' =>  10
        ),
    1 => array (
            'des_id' =>  1,
            'des_name' =>  'bagan',
            'tran_id' =>  2,
            'tran_name' =>  'express',
            'tran_image' =>  'bus3.jpg',
            'type' =>  'car',
            'troute_id' =>  13
        ),
    2 => array (
            'des_id' =>  1,
            'des_name' =>  'bagan',
            'tran_id' =>  3,
            'tran_name' =>  'MyanmarTrain',
            'tran_image' =>  'Burma-Gorteikviaduct.jpg',
            'type' =>  'train',
            'troute_id' =>  16
        ),
    3 => array (
            'des_id' =>  1,
            'des_name' =>  'bagan',
            'tran_id' =>  4,
            'tran_name' =>  'Ayeyarwaddy Cruise',
            'tran_image' =>  'boat-ChutzpahToo1.jpg',
            'type' =>  'cruise',
            'troute_id' =>  22
        )
);

// initialize the result array
$result = array();

// loop over the starting array
foreach($starting_array as $entry) {
    // make sure the result array has a key matching this item's type
    if(!array_key_exists($entry['type'], $result)) { 
        $result[ $entry['type'] ] = array();
    }
    // add this item to the result array
    $result[ $entry['type'] ][] = $entry;
}

// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";

答案 1 :(得分:0)

试试这个:

<?php

$tempArr = Array
    (
        Array(
        "des_id" => 1,
        "des_name" => "bagan",
        "tran_id" => 1,
        "tran_name" => "private",
        "tran_image" => "1251961905A1.jpg",
        "type" => "car",
        "troute_id" => 10
    ),
    Array
        (
        "des_id" => 1,
        "des_name" => "bagan",
        "tran_id" => 2,
        "tran_name" => "express",
        "tran_image" => "bus3.jpg",
        "type" => "car",
        "troute_id" => 13
    ),
    Array
        (
        "des_id" => 1,
        "des_name" => "bagan",
        "tran_id" => 3,
        "tran_name" => "MyanmarTrain",
        "tran_image" => "Burma-Gorteikviaduct.jpg",
        "type" => "train",
        "troute_id" => 16
    ),
    Array
        (
        "des_id" => 1,
        "des_name" => "bagan",
        "tran_id" => 4,
        "tran_name" => "Ayeyarwaddy Cruise",
        "tran_image" => "boat-ChutzpahToo1.jpg",
        "type" => "cruise",
        "troute_id" => 22
    )
        );

        $resultArr = array();

        foreach($tempArr as $tempKey=>$temp)
        {
            if(!array_key_exists($temp['type'], $resultArr))
            {
                $resultArr[$temp['type']] = array();
            }        
            $resultArr[$temp['type']][] = $temp;
        }

        echo '<pre>';
        print_r($resultArr);
?>

这很好用.....