2

I have this array:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => a,b,c
        )

    [1] => Array
        (
            [0] => 5
            [1] => d,e,f
        )
)

I want the final array to be this:

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

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

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

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

    [4] => Array
        (
            [0] => 5
            [1] => e
        )

    [5] => Array
        (
            [0] => 5
            [1] => f
        )
)

This is what I did:

<?php
    $array = array(array(1,"a,b,c"),array(5,"d,e,f"));
    $temp=array();
    $count = 0;
    foreach($array as $arr){
        $rows = explode(",",$arr[1]);
        foreach($rows as $row){
            $temp[$count] = $arr;
            $temp[$count][1] = $row;
            $count++;
        }
    }
    print_r($temp);
?>

This totally works but I was wondering if there was a better way to do this. This can be very slow when I have huge data.

4
  • Using loops will generally be the fastest way. Often slowness is caused by inefficient loops, but that doesn't appear to be the case here. Your dataset would have to be genuinely MASSIVE for this to have a significant impact on the execution time of your script. Commented Apr 19, 2017 at 2:30
  • are you saying this is the best way to do it? Commented Apr 19, 2017 at 2:35
  • Yes. Loops will generally always be the fastest way. Using some callback function like array_map (not that this will work here) will be slower. Commented Apr 19, 2017 at 2:37
  • codereview.stackexchange.com Commented Apr 19, 2017 at 2:38

3 Answers 3

1

Try like this way...

<?php

$array = array(array(1,"a,b,c"),array(5,"d,e,f"));
$temp=array();
$count = 0;
foreach($array as $arr){
    $rows = explode(",",$arr[1]);
    foreach($rows as $row){
        $temp[$count][] = $arr[0];
        $temp[$count][] = $row;
        $count++;
    }
}

/*print "<pre>";
  print_r($temp);
  print "<pre>";*/

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

1 Comment

The problem is you copied the code from question itself, had to down vote you
1

Here's a functional approach:

$result = array_merge(...array_map(function(array $a) {
    return array_map(function($x) use ($a) {
        return [$a[0], $x];
    }, explode(",", $a[1]));
}, $array));

Try it online.

Or simply with two loops:

$result = [];
foreach ($array as $a) {
    foreach (explode(",", $a[1]) as $x) {
        $result[] = [$a[0], $x];
    }
}

Try it online.

Timing these reveals that a simple loop construct is ~8 times faster.

  • functional: 4.06s user 0.08s system 99% cpu 4.160 total
  • loop: 0.53s user 0.05s system 102% cpu 0.561 total

2 Comments

Is it faster than what we have?
@Prakash: Nope, actually a lot slower than by simply looping the array. I've modified my answer accordingly.
0

If you need other way around,

$array = array(array(1, "a,b,c"), array(5, "d,e,f"));
$temp  = [];
array_walk($array, function ($item, $key) use (&$temp) {
    $second = explode(',', $item[1]);
    foreach ($second as $v) {
        $temp[] = [$item[0], $v];
    }
});
print_r($temp);

array_walk — Apply a user supplied function to every member of an array

Here is working demo.

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.