简体   繁体   中英

Flattern 3rd level of array to create indexed array of associative rows

I have the following problem:

I have an php array looking like this:

$array = [
    [
        ['sales_count' => '2'],
        ['customer_id' => '1'],
    ],
    [
        ['sales_count' => '3'],
        ['customer_id' => '2'],
    ]
];

Now if I use json_encode on this array, I get the following result:

[[{"sales_count":"2"},{"customer_id":"1"}],[{"sales_count":"3"},{"customer_id":"2"}]] 

However, I'm trying to get the following output: (an array of flat, associative arrays)

[{"sales_count":"2","customer_id":"1"},{"sales_count":"3","customer_id":"2"}]

This is because of the fact that there are two arrays inside your original array on indexes 0 and 1

You need to do something like this

$masterArray = Array (
 [0] => Array (
    [0] => Array ( [sales_count] => 2 )
    [1] => Array ( [customer_id] => 1 )
 ) 
[1] => Array (
    [0] => Array ( [sales_count] => 3 )
    [1] => Array ( [customer_id] => 2 ) 
 ) 
);

$json_array = array_merge($masterArray[0], $masterArray[1]);

echo json_encode($json_array);

Syntax for the $masterArray maybe wrong but follow the concept.

on your array should be:

$data = array(
  array("sales_count" => 2),
  array("customer_id" => 1),
  array("sales_count" => 2),
  array("customer_id" => 1),
 );
json_encode($data);

for you to achieve your expected output.

though if your array is correct you can access your json object by

var data = [
   [
    {"sales_count":"2"},
    {"customer_id":"1"}
   ],
    [
     {"sales_count":"3"},
     {"customer_id":"2"}
    ]
 ];

data[0][0].sales_count will access sales_count = 2 on your 1st array.

Well, you could restructure them and put them inside a new one. Example:

$new_array = array();
array_walk_recursive($array, function($val, $key) use (&$new_array) {
    $new_array[] = array($key => $val);
});

$new_array = json_encode($new_array);
echo '<pre>';
print_r($new_array);
// [{"sales_count":2},{"customer_id":1},{"sales_count":3},{"customer_id":2}]

Or just a simple loop, just simply, push them inside:

$new_array = array();
foreach($array as $values) {
    foreach($values as $val) {
        $new_array[] = $val;
    }
}

echo json_encode($new_array);

Sample output as above.

I come from VietNam. My English does not good. So, I write this code. I hope this help you.

 $arr = array(
   0 => array(0 => array('sales_count'=>2),1 => array('customer_id' => 1)),
   1 => array(0 => array('sales_count'=>3),1 => array('customer_id' => 2)),
);
$new_arr = array();
foreach($arr as $key => $value){
  foreach($value as $kvalue => $vvalue){
     $new_arr[] = $vvalue;
  }
}
print_r(json_encode($new_arr));

To flatten the deep associative data in each "row", none of the previous answers work as required. Other techniques provided merely flatten the data to become an array of single-element associative rows (or worse destroy associative relationships while flattening). For the record, dynamically flattening an array's first level is more succinctly coded as array_merge(...$array) .

Instead, you must iterate all rows and specifically merge their subarrays. This will flatten the deep structure only so that rows now have two associative elements. THIS is what the question is actually asking for.

Code: ( Demo )

echo json_encode(
         array_map(
             fn($a) => array_merge(...$a),
             $array
         )
     );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM