简体   繁体   中英

Creating array from another array

I have an array that looks like this:

Array
(
    [0] => Dog:110
    [1] => Cat:111
    [2] => Mouse:101
)

Based on that array, I want to create another array to look like this

Array
(
   [Dog] => 110
   [Cat] => 111
   [Mouse] => 101
)

I know this is easy by creating my own function. But is there any way to do this with built in php function. Basically, I know I need to explode(), but is there any way to use this function in conjuction with one php's array functions or will I need to create my own function?

For fun one-liner:

parse_str(str_replace(':', '=', implode('&', $array)), $result);
print_r($result);

Use explode

$new_arr=array();
foreach($yourarr as $v)
{
 $v = explode(':',$v);
 $new_arr[$v[0]]=$v[1];
}

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM