1

I am new to PHP and I am trying to convert an array to json, without the indices.

For an example, I have:

[{"name":"Dean"},{"last_name":"Don"},{"age":31},{"height":181}]

I need it to be a single json object:

{
"name":"Dean,
"last_name":"Don",
"age":31,
"height":181
}

I tried using json_encode() but all I get is not correct, I event tried specifying JSON_FORCE_OBJECT, which put the indices, which I don't want.

Anyone has an idea how to solve it? Thank you

Syscall
  • 19,327
  • 10
  • 37
  • 52
Moshe Davidi
  • 83
  • 1
  • 5

4 Answers4

4

Another way is to decode, merge and recode:

$json = '[{"name":"Dean"},{"last_name":"Don"},{"age":31},{"height":181}]';
$data = json_decode($json,true); // decode
$data = array_merge(...$data); // merge
echo json_encode($data, JSON_PRETTY_PRINT); // recode

Output:

{
    "name": "Dean", 
    "last_name": "Don", 
    "age": 31, 
    "height": 181 
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
1

did you try json_encode(array_values($array))?

Dev web
  • 81
  • 1
  • 13
  • 2
    Can you explain how this would help in creating a single JSON object? – Don't Panic May 14 '18 at 15:20
  • see more details here: https://stackoverflow.com/questions/11195692/json-encode-sparse-php-array-as-json-array-not-json-object?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Dev web May 14 '18 at 15:33
  • 1
    Not sure how that link has anything to do with this question – iainn May 14 '18 at 15:37
1

You can use json_decode to convert the json into an array. Use array_reduce to make a new array. Use json_encode to convert the array into a json again

$str = '[{"name":"Dean"},{"last_name":"Don"},{"age":31},{"height":181}]';

//Convert the json into array
$arr = json_decode($str, true);

//Make the multi dementional array into an associative array
$arr = array_reduce($arr, function($c, $v){
    foreach ($v as $key => $val) $c[$key] = $val;
    return $c;
}, array());

//Convert the array to json
$result = json_encode($arr);

echo $result;

This will result to:

{"name":"Dean","last_name":"Don","age":31,"height":181}
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • 1
    It would still be better to fix the intial creation errors as the initial JSON String is really no use to man or beast – RiggsFolly May 14 '18 at 15:19
1

The first bit of JSON looks like the result of encoding an array of key-value pairs like this:

$data = [
    ['name' => 'Dean'],
    ['last_name' => 'Don'],
    ['age' => 31],
    ['height' => 181]
];

If that's what you are starting with, you can iterate the set of attributes and construct an entity that will encode to a single object.

foreach ($data as $attribute) {
    $entity[key($attribute)] = reset($attribute);
}

echo json_encode($entity);

As mentioned in the comments, there may be a better way to do this earlier in your code, so you can create the entity you want in the first place instead of something like the $data example that you'll have to re-process before you can output it.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80