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

6
  • Show us what you tried, you may be closer to the answer than you think Commented May 14, 2018 at 15:10
  • 3
    Of course it would be a better idea to look at how you managed to create the first JSON String and fix that rather than try and fiddle with a bad result Commented May 14, 2018 at 15:11
  • 1
    Bot the array you wrote are not vaild php arrays. Commented May 14, 2018 at 15:11
  • 3
    @T30 I think that is a JSON String representation of an array of objects Commented May 14, 2018 at 15:12
  • 1
    Show us the code you used to create the initial JSON String Commented May 14, 2018 at 15:14

4 Answers 4

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 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I agree array_merge is perfect for this.
1

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

3 Comments

Can you explain how this would help in creating a single JSON object?
Not sure how that link has anything to do with this question
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}

1 Comment

It would still be better to fix the intial creation errors as the initial JSON String is really no use to man or beast
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.