-1

Array-like this:

Array
(
    [0] => Array
        (
            [0] => <img s
            [1] => Ric
            [2] => 130
            [3] => 608
        )

    [1] => Array
        (
            [0] => In Stock
            [1] => 10 lb
            [2] => <img src=
            [3] => Rice La
        )
)

What I want is to convert this complex array into a simple one, so in the end, I can get this result:

Array
(

            [0] => <img s
            [1] => Ric
            [2] => 130
            [3] => 608
            [4] => In Stock
            [5] => 10 lb
            [6] => <img src=
            [7] => Rice La

)

I really tried a lot of different codes and helps that I found on the internet like this:

    function array_flatten($array) { 
  if (!is_array($array)) { 
    return FALSE; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
      $result = array_merge($result, array_flatten($value)); 
    } 
    else { 
      $result[$key] = $value; 
    } 
  } 
  return $result; 
} 

This didn't help me at all. Does someone know which method I can try to solve this problem? This didn't help me at all. Does someone know which method I can try to solve this problem?

4 Answers4

2

Simplest way is to merge them using Argument unpacking via the splat ... operator:

$result = array_merge(...$array);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0
$elements = array();
foreach ( $array as $mElement ) {
    foreach ( $mElement as $singleElement ) {
        $elements[] = $singleElement;
    }
}

print_r($elements);

Assuming $array contain:

Array
(
    [0] => Array
        (
            [0] => <img s
            [1] => Ric
            [2] => 130
            [3] => 608
        )

    [1] => Array
        (
            [0] => In Stock
            [1] => 10 lb
            [2] => <img src=
            [3] => Rice La
        )
)

Your output will be:

Array
(

            [0] => <img s
            [1] => Ric
            [2] => 130
            [3] => 608
            [4] => In Stock
            [5] => 10 lb
            [6] => <img src=
            [7] => Rice La

)
Fernando Torres
  • 460
  • 7
  • 24
  • LOL you are fast, how did you manage to answer this fast? ;) – Piyush Nov 07 '19 at 18:25
  • that's sarcasm? – Fernando Torres Nov 07 '19 at 18:26
  • Nope, seriously i am in awe, you answered faster and more detailed than me even if i was the first one to view it after the op obviously. – Piyush Nov 07 '19 at 18:29
  • jeje, idk, kind of lucky maybe? hope any question here hopes buddy solve his problem, regards! – Fernando Torres Nov 07 '19 at 18:31
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Nov 07 '19 at 18:34
0

This will do it.

$simplearray = array();
foreach($higharray as $midarray){
    foreach($midarray as $childarray){
        $simplearray[] = $childarray;
    }
}
print_r($simplearray)
Piyush
  • 62
  • 14
0

This function does what you want, and avoids using multiple loops.

function array_flatten($array) {
    $temp = [];
    foreach($array as $a) {
        $temp = array_merge($temp, $a);
    }
    return $temp;
}
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71