Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following problem:

I have an php array looking like this:

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 ) 
 ) 
) 

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:

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

How can I accomplish this? I already read through a lot of similar questions, I used their answers to get to this point, however I found no solution for this output.

Thanks a lot in advance!

share|improve this question
    
you have 2D array and want to output 1D array. So you obviously first need to convert your array to 1D, then json_encode will get expected result –  Kyborek Sep 16 '14 at 8:03
    
where's the code that creates the arrays...flatten it there –  charlietfl Sep 16 '14 at 8:03
    
No, you dont want the result youre describing. I cant think of any useful scenario where you have duplicate data in same field names in same array. That might be a failing of my imagination perhaps, but still, I think your solution might not work, or, at the least, will cause more trouble than its worth. –  Tuncay Göncüoğlu Sep 16 '14 at 8:06
    
@TuncayGöncüoğlu you obviously don't do much front end AJAX work with JSON API's –  charlietfl Sep 16 '14 at 8:10
    
I'm sorry, I'm really really new to this, but I think I do want that result. I'm pretty sure it's not the best to do this like you mentioned, but at I want this to work at least and clean it up later. And no, I really don't do much AJAX at all, but my next step is to try and learn how to do it. –  Sirence Sep 16 '14 at 8:11

4 Answers 4

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you for your answer. $json_array = array_merge($masterarray[0][0], $masterarray[0][1]); gives me what I need. How would I loop through this if I do not know how many entries each outer and inner array has? –  Sirence Sep 16 '14 at 8:06
    
use a loop starting with $i = 0 until $i reaches sizeof($masterarray[0]). You want me to write the code for that also? –  Dee Jay' Sep 16 '14 at 8:10
    
I'll try and if I don't manage to, I'll ask again if that is okay with you? –  Sirence Sep 16 '14 at 8:13
    
yes sure ...... –  Dee Jay' Sep 16 '14 at 8:16
    
Woooo I did it! Thanks a lot! –  Sirence Sep 16 '14 at 8:27

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.
share|improve this answer

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));
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.