1

I have this kind of array

Array ( [0] => Array (  [id] => 1 
                        [name] => PeopleOne
                        [address] => AddressOneOfPeopleOne 

        [1] => Array (  [id] => 2 
                        [name] => PeopleTwo 
                        [address] => AddressOneOfPeopleTwo 

        [2] => Array (  [id] => 3 
                        [name] => PeopleThree 
                        [address] => AddressOneOfPeopleThree 

        [3] => Array (  [id] => 4 
                        [name] => PeopleOne
                        [address] => AddressTwoOfPeopleOne 

and I want this kind of format

Array ( [0] => Array (  [id] => 1 
                        [name] => PeopleOne
                        [address] => Array(
                                    [0] => AddressOneOfPeopleOne
                                    [1] => AddressTwoOfPeopleOne
                        )

        [1] => Array (  [id] => 2 
                        [name] => PeopleTwo 
                        [address] => AddressOneOfPeopleTwo 

        [2] => Array (  [id] => 3 
                        [name] => PeopleThree 
                        [address] => AddressOneOfPeopleThree 

I don't know how to do? Could anyone please solve this?

kmith
  • 49
  • 5
  • Please clarify your question. I can't make out what kind of array structure you are using. At least add some closing brackets so we can try to figure out what you want. – lugte098 Aug 12 '11 at 14:33
  • Duplicated question. See [question1](http://stackoverflow.com/questions/7010756/merge-and-group-by-several-arrays) and [question2](http://stackoverflow.com/questions/7011451/transaprently-flatten-an-array) – J0HN Aug 12 '11 at 14:36

2 Answers2

1

I have a feeling this isn't exactly what you're looking for, but I'm not entirely sure what needs to be inside the final array. How is this, at least for a start?

<?php
$array = array(
array('id' => 1, 'name' => 'PeopleOne', 'address' => 'Address1'),
array('id' => 2, 'name' => 'PeopleTwo', 'address' => 'Address2'),
array('id' => 3, 'name' => 'PeopleOne', 'address' => 'Address3')
);


foreach ($array as $k => $v) {
$newarray[$v['name']][] = $v['address'];
}

echo '<pre>'.print_r($array,1).'</pre>';
echo '<pre>'.print_r($newarray,1).'</pre>';

?>
pb149
  • 2,298
  • 1
  • 22
  • 30
0

I'll assume that your array is called $array.

foreach ($array as $subArray) {
    if (!is_array($subArray['address']) {
        $subArray['address'] = array($subArray['adress']);
    }
    $subArray['address'][] = 'AddressTwoOfPeopleOne';
}
lugte098
  • 2,271
  • 5
  • 21
  • 30
  • ok ... I want to update that array by looping in php and I want later format .. I don't know how to loop and update. – kmith Aug 12 '11 at 14:39