简体   繁体   中英

Need advice for efficient solution with operations on array in PHP

I'm looking for an efficient solution to isolate an element of array and edit it: My array is:

$starting_array = array_slice($main_array, 8, 1);

Where value of array element is

print_r($starting_array); 

// output:
Array (
    [key] => A;B;C;D;E;
)

I have to get this result:

$ending_array = Array ( 
    [A] => 0 
    [B] => 1 
    [C] => 2 
    [D] => 3 
    [E] => 4 
)

I have followed this approach:

$middle1 = implode(" ", $starting_array);
$middle2 = explode(";", $middle1);
$ending_array = array_flip($middle2);

Is there a more efficient solution to achieve the same result?

@AbraCadaver and @ChrisG, I realize I had made a mistake. $middle1 is now correct. Thanks to both!

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