5

I have an array like :

Array
(
   [2] => 2,6
   [3] => 1
   [4] => 14
   [5] => 10
   [6] => 8
)

I want to explode each element of an array and return a new array using array_map, so that I can avoid using loop, and creating extra function to call back.

O/p should be like :

  Array
(
[2] => Array
    (
        [0] => 2
        [1] => 6
    )

[3] => Array
    (
        [0] => 1
    )

[4] => Array
    (
        [0] => 14
    )

[5] => Array
    (
        [0] => 10
    )

[6] => Array
    (
        [0] => 8
    )

)
4
  • 1
    So where are you stuck with using array_map()? Also you know that at the end array_map will also loop over your array. Commented Aug 29, 2016 at 10:17
  • 1
    avoid using loop, and creating extra function to call back - array_map also uses callback function as its first argument Commented Aug 29, 2016 at 10:19
  • avoid using loop, and creating extra function to call back - Means , as we have function explode already , so not to create other function Commented Aug 29, 2016 at 10:48
  • Related: Split Values in Array Using Explode to form Multidimensional Array and Make an element in array explode Commented Jul 14, 2024 at 0:10

4 Answers 4

8

You can use

$result = array_map(function($val) {
    return explode(',', $val);
}, $input);

Which will result in

Array
(
    [2] => Array
        (
            [0] => 2
            [1] => 6
        )

    [3] => Array
        (
            [0] => 1
        )

    [4] => Array
        (
            [0] => 14
        )

    [5] => Array
        (
            [0] => 10
        )

    [6] => Array
        (
            [0] => 8
        )

)

This will work on PHP >= 5.3 with support for anonymous functions.

Sign up to request clarification or add additional context in comments.

Comments

4

You can also do

$result = array_map('str_getcsv', $input);

This will also result in

Array
(
    [2] => Array
        (
            [0] => 2
            [1] => 6
        )

    [3] => Array
        (
            [0] => 1
        )

    [4] => Array
        (
            [0] => 14
        )

    [5] => Array
        (
            [0] => 10
        )

    [6] => Array
        (
            [0] => 8
        )

)

Comments

2

Try following code

$newArr = array_map(function($val, $key){
    return explode(",", $val);
}, $arr);

Comments

0

$data = array(2 => '2,6',3 => '1',4 => '14',5 => '10',6 => '8');

foreach($data as $key => $val) {
    $new = explode(',',$val);
    $data[$key] = $new;
}
$output = $data;

echo '<pre>';
print_r($output);

2 Comments

I dont want any extra loops, as we have array_map
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!

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.