0

My array is like that:

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
        )

)

I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:

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

what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!

2
  • You can't do it the way you want, as the array keys have to be unique, otherwise you have no way to differentiate between one key or the other. Then there's no way to access a specific key's data successfully. Commented Dec 13, 2013 at 4:45
  • @Thyu Can you please explain more clearly what you really want to do? Commented Dec 13, 2013 at 4:48

2 Answers 2

2

Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:

// 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>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, Ed Cottrell. It's ok now! :)
0

Try this:

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

This is working fine .....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.