简体   繁体   中英

PHP multidimensional array into JSON object without indexes showing

I have form output coming from repeater looking like this:

array:2 [ 0 => array:1 ["participant_name" => "John" ], 0 => array:1 ["participant_name" => "Jane" ]]

I need to make from this JSON object looking like this:

{{"participant_name":"John"},{"participant_name":"Jane"}}

So far I tried $participantsJson = json_encode( array_values($participants) , JSON_FORCE_OBJECT); but I'm always getting this shape

 {"0":{"participant_name":"John"},"1":{"participant_name":"Jane"}}

How to get rid of those indexes?

Your attempt is tto complex, use the immediate approach:

<?php
$input = [
  ["participant_name" => "John" ], 
  ["participant_name" => "Jane" ],
];
$output = json_encode($input);
var_dump($output);

The output is:

string(57) "[{"participant_name":"John"},{"participant_name":"Jane"}]"

Not that the output is an array encoded in JSON format...

Your question demonstrates an encoded object instead, but that is invalid JSON. Reason is that properties inside an object need to have a name , that is how objects are defined. You cannot have an object holding members without a name (what you called an "index" in your question).

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