The below is my php code.Many thanks
$data = array();
foreach ($row as $rowk) {
$data[] = array(
'message' => $row['traditionalmessage'],
'phone' => $row['telMobile']
);
break;
}
echo json_encode($data);
My current result
[{"message":"B\u578b\u809d\u708e\u75ab\u82d7\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"}][{"message":"\u75ab\u82d7\u540d\u7a314\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"}][{"message":"\u4fe1\u606f","phone":"55503234"}]
My desired result
[{"message":"B\u578b\u809d\u708e\u75ab\u82d7\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"},{"message":"\u75ab\u82d7\u540d\u7a314\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"},{"message":"\u4fe1\u606f","phone":"55503234"}]
In JSON arrays represented in [], and objects in {}, so in desired case you should have array of objects while you have array of arrays.
Try something like this:
$data = array();
foreach ($row as $rowk) {
$obj = new stdClass();
$obj->message = $row['traditionalmessage'];
$obj->phone = $row['telMobile'];
$data[] = $obj;
}
echo json_encode($data);
Related
With php, I need to convert json arrays into arrays, what should I do, json_encode didn't work for me, thanks in advance for help.
//json sequence
[
{
"name":"Menu",
"sub":
[
{
"name":"Menu 2",
"url":"menu-2.php"
}
]
}
]
this way i should do
array(
'name' => 'Menu',
'sub' => array(
array(
'name' => 'Menu 2',
'url' => 'menu-2.php'
)
)
)
i am creating json array with this function
Do I have to make a change here? I'm not really good in arrays.
<?php
$connect = new PDO("mysql:host=localhost; dbname=propanel_001", "root", "");
$parent_category_id = "";
$query = "SELECT * FROM tb_sayfalar";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$data = get_node_data($parent_category_id, $connect);
}
echo json_encode(array_values($data));
function get_node_data($parent_category_id, $connect)
{
$query = "SELECT * FROM tb_sayfalar WHERE parent_id = '".$parent_category_id."'";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = array();
foreach($result as $row)
{
$sub_array = array();
if (array_values(get_node_data($row['id'], $connect))) {
$sub_array['name'] = $row['page_name'];
$sub_array['sub'] = array_values(get_node_data($row['id'], $connect));
}else{
$sub_array['name'] = $row['page_name'];
$sub_array['url'] = $row['page_url'].".php";
}
$output[] = $sub_array;
}
return $output;
}
?>
This is what you need, json_decode($json,true);
<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = json_decode($json,1);
print_r($array[0]);
?>
DEMO: https://3v4l.org/JZQCn
OR use it as a parsable string representation of a variable with var_export()
<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = var_export(json_decode($json,1)[0]);
print($array);
?>
DEMO: https://3v4l.org/rLA9R
You must use json_decode to convert JSON representing Object to Associative array.
Example Code
$resArr = json_decode($response, true);
For more information look at PHP JSON_DECODE
I'm trying to combine a few mySQL queries in a single PHP and push it all into a single JSON object.
So, I'm starting with the first query...like this:
$data=[];
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_count = count($response);
$data['count'] = $final_count;
If I then do echo json_encode($data); I get a nicely formatted object like: {"count":61}
I then have a second query that I put the results through a loop, like so:
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach ($response as &$value) {
$items[] = $value['date_added'];
}
echo json_encode($items);
And I get my nice set of dates:
["2017-06-24 00:08:58","2017-06-26 15:01:48","2017-06-27 15:01:48","2017-06-28 23:19:41","2017-06-29 01:38:07","2017-06-30 00:08:58"]
Here's the question, how do I get this all back together like so:
{
"count": 61,
"dates": [
"2017-06-24 00:08:58",
"2017-06-26 15:01:48",
"2017-06-27 15:01:48",
"2017-06-28 23:19:41",
"2017-06-29 01:38:07",
"2017-06-30 00:08:58"
]
}
You could use
$myData['count'] = $final_count;
$myData['dates'] = $items
echo json_encode($myData);
$data=[];
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_count = count($response);
// first store count in `$data`
$data['count'] = $final_count;
$data['dates'] = [];
$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
// next, store dates in subarray of `$data`
foreach ($response as &$value) {
$data['dates'][] = $value['date_added'];
}
// finally encode everything
echo json_encode($data);
Of course you can use array_merge of all your collected data.
Or:
$data=[];
// get $final_count
$data['count'] = $final_count;
// ... do some more stuff
// load items from db
$data['dates'] = $items;
echo json_encode($data);
I have this
{"1":{"name":"cat"},"2":{"name":"elephant"}}
how to convert it to
[{"name":"dog"},{"name":"cat"}]
or convert it to vice versa
$arr = json_decode('{"1":{"name":"cat"},"2":{"name":"elephant"}}');
$result = array();
foreach($arr as $item) {
$data = ['name' => $item->name];
$result[] = $data;
}
$json = json_encode($result);
print_r($json);
I want to acheive something simliar to this adding elements onto an array but when I use this methods two nodes get created in the json element. I only want one node with all the entires within that also can you name nodes ie Properties.
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['name'];
$json['id'] = $row['id'];
$data[] = $json;
}
$custom = array('name'=>'foo', 'id' => 'bar');
$data[] = $custom;
Try this code array_push is better option here,
<?php
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$temp = array();
$temp = array('name' => $row['name'], 'id' => $row['id']);
array_push($json, $temp);
}
$custom = array('name'=>'foo', 'id' => 'bar');
array_push($json,$custom);
?>
I am trying to create a json using php
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = stripslashes($info['id']);
$pricedol = stripslashes($info['pricedol']);
$final_result = array('id' => $id,'pricedol' => $pricedol );
}
$json = json_encode($final_result);
echo $json;
This is giving the following output
{
"id": 14567,
"pricedol": 15.57
}
But i have couple of records in the db...i want the following output
{
"id": 14567,
"pricedol": 15.57
},
{
"id": 4567,
"pricedol": 55.25
},
One more thing...do i need to serialize before parsing in jquery
You could create a multi-dimensional array and use json_encode on that array:
$final_result = array();
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = stripslashes($info['id']);
$pricedol = stripslashes($info['pricedol']);
$final_result[] = array('id' => $id,'pricedol' => $pricedol);
}
$json = json_encode($final_result);
echo $json;