I am new to PHP and trying to produce the following json output :
{
"contacts": [
{
"id": "1",
"name": "John Bob",
"email": "john#bob.com"
},
{
"id": "2",
"name": "Johnny Mac",
"email": "johnny#mac.com"
},
.....
]
}
and I have the following PHP code:
$final = array();
foreach ($contacts as $contact)
{
.....
$final[] = array(
'id' => (string)$id,
'name' => $name,
'email' => $email
);
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($final, JSON_PRETTY_PRINT);
however, this outputs:
[
{
"id": "1",
"name": "John Bob",
"email": "john#bob.com"
},
{
"id": "2",
"name": "Johnny Mac",
"email": "johnny#mac.com"
},
]
I read over this tutorial and changed $final[] to $final['contacts'] but still couldn't produce the desired json file.
Either:
$final['contacts'][] = array(..);
or:
$contacts_output = array();
foreach ($contacts as $contact) {
$contacts_output[] = array(..);
}
$final = array('contacts' => $contacts_output);
echo json_encode($final, JSON_PRETTY_PRINT);
foreach ($contacts as $contact)
{
.....
$final['contacts'][] = array(
array( //insert all your array in contacts
'id' => (string)$id,
'name' => $name,
'email' => $email
);
}
Try this:
$final = array();
$final['contacts'] = array();
foreach ($contacts as $contact)
{
.........
$final['contacts'][] = array(
'id' => (string)$id,
'name' => $name,
'email' => $email
);
}
$final = array('contacts'=>[]);
foreach ($contacts as $contact)
{
$final['contacts'] []=
array( //insert all your array in contacts
'id' => (string)$id,
'name' => $name,
'email' => $email
);
}
Related
I need some help to reorder the data in a json file.
I have these few lines :
foreach($rows as $row) {
$data[] = array(
"$row[0]" => array(
"name" => "$row[1]",
"value" => "$row[2]",
"conso" => "$row[3]",
"ml" => "$row[4]"
)
);
}
$data = json_encode($data, JSON_FORCE_OBJECT);
This function allows me to obtain something like this :
This function allows me to obtain something like this :
{
"0": {
"tata": {
"name": "name_01",
"value": "value_01",
"conso": "conso_01",
"ml": "ml_01"
}
},
"1": {
"toto": {
"name": "name_02",
"value": "value_02",
"conso": "conso_02",
"ml": "ml_02"
}
}
}
I would like something like that :
{
"tata": {
"name": "name_01",
"value": "value_01",
"conso": "conso_01",
"ml": "ml_01"
},
"toto": {
"name": "name_02",
"value": "value_02",
"conso": "conso_02",
"ml": "ml_02"
}
}
Get rid of a layer of array, and specify the key you want. You also don't need to quote all the variables like that.
foreach($rows as $row) {
$data[$row[0]] = array(
"name" => $row[1],
"value" => $row[2],
"conso" => $row[3],
"ml" => $row[4]
);
}
$data = json_encode($data, JSON_FORCE_OBJECT);
I need to create a json with this format:
{
"reservs": [
{
"ResId": "58",
"time": "2020-05-15 19:41:50",
"boxEntering": null,
"boxClosing": null,
"active": "1",
"UserId": "29",
"BoxId": "4",
"boxPlace": null,
"box": {
"id": "4",
"Nom": "Hortillonages",
"Lat": "49.8953",
"Lng": "2.31034",
"place": "0",
"placeMax": "9"
}
}
]
}
in entries, a $header who check the user token(not use for my problem)
$table the table returned from PDO::FETCHASSOC from sql SELECT request
My php code:
function generateJson($table, headerChecker $header){
$final = array();
foreach ($table as $item) {
$box = array(
"id" => $item["id"],
"Nom" => $item["Nom"],
"Lat" => $item["Lat"],
"Lng" => $item["Lng"],
"place" => $item["place"],
"placeMax" => $item["placeMax"]
);
$reserv = array(
"ResId" => $item["ResId"],
"time" => $item["time"],
"boxEntering" => $item["boxEntering"],
"boxClosing" => $item["boxClosing"],
"active" => $item["active"],
"UserId" => $item["UserId"],
"BoxId" => $item["BoxId"],
"boxPlace" => $item["boxPlace"],
);
$reserv["box"] = $box;
array_merge($final,$reserv);
}
$arr = array("reservs" => $table);
$header->tokenJson($arr);
echo json_encode($arr);
}
I have this result
{"reservs": [
{
"ResId": "58",
"time": "2020-05-15 19:41:50",
"boxEntering": null,
"boxClosing": null,
"active": "1",
"UserId": "29",
"BoxId": "4",
"boxPlace": null,
"id": "4",
"Nom": "Hortillonages",
"Lat": "49.8953",
"Lng": "2.31034",
"place": "0",
"placeMax": "9",
"QRID": "",
"boxToken": ""
}]
}
I think the Json format eror is in the array_merge function.
What add array function can I use to not remove the Box object
Your problem is that you are not assigning the return of array_merge and using the wrong variable $table. Just dynamically append to $final:
foreach ($table as $item) {
// this all appears good
//
$reserv["box"] = $box;
$final[] = $reserv; // append to $final
}
$arr = array("reservs" => $final); // use $final
$header->tokenJson($arr);
echo json_encode($arr);
Merge the new $reserv into $final and assign it to $final, then use $final.
Try this one
function generateJson($table, headerChecker $header){
$final = array();
foreach ($table as $item) {
$box = array(
"id" => $item["id"],
"Nom" => $item["Nom"],
"Lat" => $item["Lat"],
"Lng" => $item["Lng"],
"place" => $item["place"],
"placeMax" => $item["placeMax"]
);
$reserv = array(
"ResId" => $item["ResId"],
"time" => $item["time"],
"boxEntering" => $item["boxEntering"],
"boxClosing" => $item["boxClosing"],
"active" => $item["active"],
"UserId" => $item["UserId"],
"BoxId" => $item["BoxId"],
"boxPlace" => $item["boxPlace"],
);
$reserv["box"] = $box;
$final[] = $reserv;
}
$arr = array("reservs" => $final);
$header->tokenJson($arr);
echo json_encode($arr);
}
I want to merge two same keys in an array and get the sum of the values.
I want the same structure as it is now.Because this data needs to be converted to JSON.
This is what i get now.
{
"data": [{
"count_of_invites": 5,
"user": "Rajesh",
"id": "53"
},
{
"count_of_invites": 9,
"user": "Student",
"id": "45"
},
{
"count_of_invites": 4,
"user": "Student",
"id": "45"
}
]
}
As you can see the id 45 are repeated.As i want the result as,
Expected output
{
"data": [{
"count_of_invites": 5,
"user": "Rajesh",
"id": "53"
},
{
"count_of_invites": 13,
"user": "Student",
"id": "45"
}
]
}
As you can see the duplicate entry should be removed as well as the count_of_invites of duplicate entry should be added.
<?php
$data = [
[
'id' => 2,
'name' => 'Paul',
'count' => 4
],
[
'id' => 3,
'name' => 'Peter',
'count' => 5
],
[
'id' => 3,
'name' => 'Peter',
'count' => 7
]
];
foreach($data as $array)
$counts[$array['id']][] = $array['count'];
$counts = array_map('array_sum', $counts);
foreach($data as $k => $array)
$data[$k]['count'] = $counts[$array['id']];
$data = array_unique($data, SORT_REGULAR);
print json_encode($data, JSON_PRETTY_PRINT);
Output:
[
{
"id": 2,
"name": "Paul",
"count": 4
},
{
"id": 3,
"name": "Peter",
"count": 12
}
]
You can achieve it this way:
$ids = array();
$output = array();
foreach ($input as $value) {
if (!isset($ids[$value["id"]])) {
$ids[$value["id"]]=$count($output);
$output[]=$value;
} else {
$output[$ids[$value["id"]]]["count_of_invites"] = $value["count_of_invites"];
$output[$ids[$value["id"]]]["user"] = $value["user"];
}
}
The count method was declared as variable and i've added with addition assignment operator.
Thank You for helping.
$ids = array();
$output = array();
foreach ($response as $value) {
if (!isset($ids[$value["id"]])) {
$ids[$value["id"]] = count($output);
$output[] = $value;
}
else {
$output[$ids[$value["id"]]]["count_of_invites"] += $value["count_of_invites"];
$output[$ids[$value["id"]]]["user"] = $value["user"];
}
}
How can I add an array to array position:
Something like a:
<?php
$newArr = array('email' => array("id" => "5678", "token" => "fghjk"));
$arr = array(
"auth"=>
array(
'users'=>
array(
'id' =>"456yhjoiu",
'token' => "asdfghjkrtyui678"
)
)
);
somefunction($arr['auth'], $newArr);
I've tried array_push() but it added zero (0) before 'email' instead.~
I'm doing this to get a json output, something like this:
}
"auth": {
"users": {
"id": "456yhjoiu",
"token": "asdfghjkrtyui678"
},
"email": {
"id": "5678",
"token": "fghjk"
}
}
}
but I have this output:
{
"auth": {
"users": {
"id": "456yhjoiu",
"token": "asdfghjkrtyui678"
},
"0": {
"email": {
"id": "5678",
"token": "fghjk"
}
}
}
$data = ['auth' => array_merge($arr['auth'], $newArr)];
or old array notation <= PHP5.3
$data = array('auth' => array_merge($arr['auth'], $newArr));
I can't get my json_encode format that I need.
Current Format:
{
"substantiv": [
{"text":"auto"},
{"text":"noch ein auto"}
],
"verben":[
{"text":"auto fahren"}
]
}
What I need:
[
{
"type":"substantiv",
"items": [
{"text":"auto"},
{"text":"noch ein auto"}
]
} , {
"type":"verben",
"items": [
{"text":"auto fahren"}
]
}
]
My current php code:
$data = array();
while($row = $rs->fetch_assoc()){
$tmp = array(
"text" => $row['gtext']
);
$data[$row['type']][] = $tmp;
}
echo json_encode($data);
I have tried a few things but just can't figure it out.
You could try something like this
while($row = $rs->fetch_assoc()){
$data[$row['type']][] = array(
"text" => $row['gtext']
);
}
$result = array();
foreach($data AS $type => $items) {
$result[] = array(
'type' => $type,
'items' => $items
);
}
echo json_encode($result);
What you actually want is this:
[
{
"type": "substantiv",
"items": [
{
"text": "auto"
},
{
"text": "noch ein auto"
}
]
},
{
"type": "verben",
"items": [
{
"text": "auto fahren"
}
]
}
]
which is the output you'll get from Louis H.'s answer's code.