Rana Ghosh
Rana Ghosh

Reputation: 4674

Convert Array to serialize like string

I have a Array like below:

Array
(
    [0] => Select one
    [1] => FB
    [2] => RM
    [3] => Joey
    [4] => Isaac
    [5] => Christina
    [6] => James
    [7] => Armando
    [8] => Kent
    [9] => Tyler
    [10] => Michael
    [11] => Dylan
    [12] => Ryan
)

I want to convert it to 0:None;1:FB;2:RM;3:Joey;4:Isaac;5:Christina;6:James;7:Armando;8:Kent;9:Tyler;10:Michael;11:Dylan

I can do it using FOR loop and using IF, But I want to know the shortest way or is there any function available.
Any reference will be helpfull.

Thanks in advance.

Upvotes: 0

Views: 127

Answers (1)

Vara Prasad
Vara Prasad

Reputation: 497

you can try this

$output = implode(';', array_map(
    function ($v, $k) { return $k.":".$v; },
    $data,
    array_keys($data)
));

Upvotes: 4

Related Questions