0

I'm having an issue when I dump the array below. This dumps several arrays. Some are 2 some are 3, which complicates it even more. Basically what I want I put below. I have tried array_push, array_combine, array_merge, several different ways including $array[$param] = $insertValue and I'm stuck. I am open to creating a brand new array too.

Please note not all arrays are counts of 3 but always return at least 1.

Original array:

array(3) {
 [0]=>
 array(2) {
    ["contact_id"]=>
    string(9) "CONTACTID"
    ["contact_id_content"]=>
    string(19) "123456789123456"
}
 [1]=>
 array(2) {
    ["sm_owner"]=>
    string(9) "SMOWNERID"
    ["sm_owner_content"]=>
    string(19) "123456798452"
}
[2]=>
 array(2) {
    ["contact_owner"]=>
    string(13) "Contact Owner"
    ["contact_owner_content"]=>
    string(16) "Jane Doe"
}

Array desired:

array(3) {
 [0]=>
 array(6) {
   ["contact_id"]=>
   string(9) "CONTACTID"
   ["contact_id_content"]=>
   string(19) "123456789123456"
   ["sm_owner"]=>
   string(9) "SMOWNERID"
   ["sm_owner_content"]=>
   string(19) "123456798452"
   ["contact_owner"]=>
   string(13) "Contact Owner"
   ["contact_owner_content"]=>
   string(16) "Jane Doe"
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Patrick Florian
  • 41
  • 1
  • 10

4 Answers4

0

try this code:

$NewArray = array();
foreach($OriginalArray as $value) {
    $NewArray[] = array_merge($value,$NewArray);
}

or you can use array_merge_recursive

Milad Teimouri
  • 1,141
  • 9
  • 17
0
let $result = [];

foreach ($yourarray as $key => $value) {
  $result  = $value;
}
var_dump($result);
jvk
  • 2,133
  • 3
  • 19
  • 28
0

Here you go: How to Flatten a Multidimensional Array? – Barmar 23 mins ago

                function flatten($array)
                {
                    return array_reduce($array, function($acc, $item){
                        return array_merge($acc, is_array($item) ? flatten($item) : [$item]);
                    }, []);
                }

                // loop the individual fields 
                for ($i=0; $i<count($row_data); $i++) {
                     $newArray = flatten([$i => $response_row]);
                }
Patrick Florian
  • 41
  • 1
  • 10
-1

Try something like this:

function flatten(array $array) : array
{
    $newArray = [];
    foreach ($array as $subArray) {
        foreach ($subArray as $key => $value) {
            $newArray[$key] = $value;
        }
    }
    return $newArray;
}
Stratadox
  • 1,291
  • 8
  • 21