how can split associative array and create new array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
destiny_cores
Forum Newbie
Posts: 8
Joined: Fri Mar 17, 2006 9:05 am

how can split associative array and create new array?

Post by destiny_cores »

I have an array

$arr=('cat'=>'1:2:3', 'dog'=>'2:4:5');

i want to split this array and use key for new array name

ex. $cat=array('1','2','3')
$dog=array('2','4','5')


Thanks for answer and suggestion
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

If I understand you correctly... this should do it.

Code: Select all

$arr= array('cat' => '1:2:3', 'dog' => '2:4:5');
foreach($arr as $key => $val){
    ${$key} = explode(":", $val);
}
Post Reply