Explode each values of Array element
Asked Answered
C

4

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
    )

)
Culex answered 29/8, 2016 at 10:14 Comment(4)
So where are you stuck with using array_map()? Also you know that at the end array_map will also loop over your array.Undersell
avoid using loop, and creating extra function to call back - array_map also uses callback function as its first argumentCorinacorine
avoid using loop, and creating extra function to call back - Means , as we have function explode already , so not to create other functionCulex
Related: Split Values in Array Using Explode to form Multidimensional Array and Make an element in array explodeJohiah
F
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.

Fulminant answered 29/8, 2016 at 10:24 Comment(0)
C
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
        )

)
Chadburn answered 17/9, 2019 at 7:34 Comment(0)
R
2

Try following code

$newArr = array_map(function($val, $key){
    return explode(",", $val);
}, $arr);
Rawley answered 29/8, 2016 at 10:24 Comment(0)
M
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);
Muttra answered 29/8, 2016 at 10:27 Comment(2)
I dont want any extra loops, as we have array_mapCulex
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!Coco

© 2022 - 2024 — McMap. All rights reserved.