0

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.

4
  • serialize() doesnt work for you ? Commented Mar 21, 2017 at 10:52
  • I want that as string like mention up. nope serialize is not working. Commented Mar 21, 2017 at 10:55
  • @Eknoes I don't think it's duplicate. Commented Mar 21, 2017 at 10:57
  • 1
    key value pair, simple use array_map() with implode(), And there are no such functions or methods that your 'Select One' changed to None, You need to use IF. Commented Mar 21, 2017 at 11:02

1 Answer 1

4

you can try this

$output = implode(';', array_map(
    function ($v, $k) { return $k.":".$v; },
    $data,
    array_keys($data)
));
Sign up to request clarification or add additional context in comments.

4 Comments

@FrayneKonok you should have submitted an answer instead of comment
No need to submit an answer, There is an answer which solve the problem.
@FrayneKonok upvote for your comment, actually i didn't see your comment before answering this
@VaraPrasad, Its okey.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.