1

I have a 2 level deep array like this:

Array(5) { 
[0]=> array(5) { 

    [0]=> string(0) "" 
    [1]=> string(21) "0,245.19000000,864432"
    [2]=> string(21) "1,245.26000000,864432" 
    [3]=> string(21) "2,245.49000000,864432" 
    [4]=> string(21) "4,245.33000000,864432" 
}

[1]=> array(5) { 
    [0]=> string(0) "" 
    [1]=> string(21) "0,245.19000000,864453" 
    [2]=> string(21) "1,245.26000000,864453" 
    [3]=> string(21) "2,245.49000000,864453" 
    [4]=> string(21) "4,245.33000000,864453" 
 }
}...

I want to explode the inner string by commas ("2,245.49000000,864453") so the arrays becomes at most 3 levels deep like so:

Array(5) { 
  [0]=> array(5) { 

    [0]=> string(0) "" 

    [1]=> array (3)
              [0]=> "0"
              [1]=> "245.19000000"
              [2]=> "864432"

    [2]=> array (3)
              [0]=> "1"
              [1]=> "245.26000000"
              [2]=> "864432"

    [3]=> array (3)
              [0]=> "3"
              [1]=> "245.49000000"
              [2]=> "864432"

    [4]=> array (3)
              [0]=> "4"
              [1]=> "245.3000000"
              [2]=> "864432"

    [4]=> array (3)
              [0]=> "5"
              [1]=> "245.3300000"
              [2]=> "864432"
 }
}
...

So far I have:

$done = array();

for ($i = 0; $i<=count($chunks); $i++) { //loops to get size of each 2d array
$r = count($chunks[$i]);

        for ($c = 0; $c<=count($chunks[$r]); $c++) { //loops through 3d array

        $arrayparts = $chunks[$i][$c];
        $done[] = explode(",", $arrayparts); //$arrayparts is 3d array string that is exploded each time through loop

    }

}

I think this code should work but when I var_dump nothing prints.

1
  • So did my coded worked now or why do you post my answer in your question? Commented Apr 1, 2015 at 22:51

3 Answers 3

0

Don't make it complicated, just use this:

(Here I go through each innerArray with a foreach loop and then I go through all values with array_map() and explode it and return it to the results array)

<?php

    foreach($arr as $innerArray) {
        $result[] = array_map(function($v){
            return explode(",", $v);
        }, $innerArray);
    }

    print_r($result);

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

4 Comments

I don't quite understand the $innerArray vs $arr
@SeanM With the foreach loop I loop over all: [0]=> array(5) { [0]=> string(0) ""... of these arrays and then I explode all values of the array with array_map() in the loop and return it to the results array
Ok. I still doesn't want to work. I'm using the array named 3d array $chunks. See edit
My arrays are set up wrong but this should work. Thanks!
0

This uses array_map() two times. Maybe a better way but I'm on beer three:

$result = array_map(function($v){
                        return array_map(function($v){ return explode(',', $v); }, $v);
                    }, $array);

Comments

0

To ensure that empty strings are not converted to subarrays AND to convert the parsed values into integer and float values, you can enjoy sscanf() to parse each string. Demo

var_export(
    array_map(
        fn($row) => array_map(
            fn($string) => sscanf($string, '%d,%f,%d') ?: '',
            $row
        ),
        $array
    )
);

Comments

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.