i have below json data
{
"data": [
{
"minggu a": "2",
"total a": "377033"
},
{
"minggu b": "1",
"total b": "136615"
}
]
and this is the code i made in controller
$satu= $this->model->db_week_resTender();
$arr = [];
foreach($satu as $val){
$arr['data'][] = array(
'minggu' => $val['minggu'],
'total' => $val['total']
);
}
$response = $this->set_response($arr,200);
}
how to concatenate below json data
here I issue data with a limit of 2.
{
"data": [
{
"minggu a": "2",
"total a": "377033"
"minggu b": "1",
"total b": "136615"
},
]
}
$data = '{
"data": [
{
"minggu a": "2",
"total a": "377033"
},{
"minggu b": "1",
"total b": "136615"
}
]
}';
$decoded_satu=json_decode($data,true);
$arr = [];
foreach($decoded_satu['data'] as $key=> $val){
foreach ($val as $subkey => $subvalue) {
$arr['data'][$subkey] = $subvalue;
}
}
$finalArray = json_encode($arr);
echo "<pre>";print_r($finalArray) ;die;
foreach($statu as $index => $val){
$arr['data'][$index] = $val;
}
This should give the result you have posted at the end.
Related
Here is my JSON
[
{
"TIMESTAMP": "2021-06-09 13:13:26",
"COL1": "10",
"COL2": "20",
"COL3": "30"
},
{
"TIMESTAMP": "2021-06-22 13:13:26",
"COL1": "20",
"COL2": "30",
"COL3": "40"
},
{
"TIMESTAMP": "2021-06-21 13:13:26",
"COL1": "1",
"COL2": "2",
"COL3": "3"
},
{
"TIMESTAMP": "2021-06-20 13:13:26",
"COL1": "40",
"COL2": "50",
"COL3": "60"
}
]
I need to refactor the json According to the Column name like (EXPECTED OUTPUT)
[
{
"TITLE":"COL1"
"DATA":[10,20,1,40]
},
{
"TITLE":"COL2"
"DATA":[20,30,2,50]
},
{
"TITLE":"COL3"
"DATA":[30,40,3,60]
},
]
I was tried but it not working
$data = json_decode($result, true);
$refactored = array_map(function($item) {
return (object)[
'TIMESTAMP' => $item['TIMESTAMP'],
'DATA' => [ $item['COL1'], $item['COL2'], $item['COL3'] ]
];
}, $data);
dump($refactored);
Someone help me out with this. The column may be 3 or more and it must be dynamic. Thanks in advance.
You can transform your JSON like this:
$data = json_decode($result, true);
$refactored = array_reduce($json, function($carry, $item) {
foreach($item as $key => $value) {
if (str_starts_with($key, 'COL')) {
$index = substr($key, 3, 1) - 1;
if (!isset($carry[$index])) {
$carry[$index] = [
'Title' => $key
];
}
$carry[$index]['Data'][] = $value;
}
}
return $carry;
}, []);
dump($refactored);
I am always a fan of using the value you want to group your data by as a temporary array key, that makes things easier, and can simply be reset by using array_values afterwards.
$input = json_decode('…', true);
$output = [];
foreach($input as $item) {
foreach($item as $key => $value) {
if($key != 'TIMESTAMP') {
$output[$key]['TITLE'] = $key;
$output[$key]['DATA'][] = (int)$value;
}
}
}
$output = array_values($output);
echo json_encode($output);
All you have to do is re-encode the processed data:
json_encode($refactored) will give you the output that you want.
P.S. you don't have to cast to an object. It works as array as well.
This is my JSON Response came from an URL API. I use GuzzleHttp\Client and json_decode() inorder to manage.
[{
"INDEX_ID": "0",
"NOTES": "0; Farming"
},
{
"INDEX_ID": "1",
"NOTES": "Fruit Name; List of Fruits;"
},
{
"INDEX_ID": "2",
"NOTES": "Apple"
},
{
"INDEX_ID": "3",
"NOTES": "Orange"
},
{
"INDEX_ID": "4",
"NOTES": "Grapes"
},
{
"INDEX_ID": "5",
"NOTES": "Mango"
},
{
"INDEX_ID": "6",
"NOTES": "Animal Name; List of Animal;"
},
{
"INDEX_ID": "7",
"NOTES": "Pig"
},
{
"INDEX_ID": "8",
"NOTES": "Goat"
},
{
"INDEX_ID": "9",
"NOTES": "Cow"
}]
But I only need to save list of Fruits in my logfile. Can someone help me how to exclude the other json parameters and get only the particular json parameters using foreach loop or any method inorder to get that result.
I just want it to be like this:
[
{
"NOTE_NO." : "001",
"CATEGORY" : "FRUITS",
"LISTS_TO_BUY" : [
{
"FRUITS": "Apple"
},
{
"FRUITS": "Orange"
},
{
"FRUITS": "Grapes"
},
{
"FRUITS": "Mango"
}
]
}
]
He're my codes so far:
$response_body = json_decode($json_response, true);
$json_new_param = [];
foreach ($response_body as $value) {
$json_new_param[] = [
'FRUITS' => $value['NOTES']
];
}
$header = [
[
'NOTE_NO.' => '001',
'CATEGORY' => 'FRUITS',
'LISTS_TO_BUY' => $json_new_param
]
];
echo json_encode($header);
$json_response is the response given above.
You might use array_reduce to return an array where the key will be the first word from the value using implode which has this structure: "NOTES": "Fruit Name; List of Fruits;". The key for this would then be "Fruits".
During the array_reduce keep track of the current key using a variable $currKey
At the end you could then get the data by using "FRUIT" (Which is the first word) as the array key: $response_body["FRUIT"]
$response_body = json_decode($json_response, true);
$currKey = "0";
$response_body = array_reduce($response_body, function ($carry, $item) use (&$currKey){
if (strpos($item['NOTES'], 'Name; List') !== false) {
$currKey = strtoupper(explode(" ", $item["NOTES"], 2)[0]);
return $carry;
}
$carry[$currKey][] = ["FRUITS" => $item["NOTES"]];
return $carry;
});
$result = [
[
"NOTE_NO" => "001",
"CATEGORY" => ($name = "FRUITS"),
"LISTS_TO_BUY" => $response_body["FRUIT"]
]
];
echo json_encode($result, JSON_PRETTY_PRINT);
That will result in:
[
{
"NOTE_NO": "001",
"CATEGORY": "FRUITS",
"LISTS_TO_BUY": [
{
"FRUITS": "Apple"
},
{
"FRUITS": "Orange"
},
{
"FRUITS": "Grapes"
},
{
"FRUITS": "Mango"
}
]
}
]
Demo
Try this code .
$json_new_param = [];
$fruitFlag = false;
foreach ($response_body as $value) {
if(strpos($value['NOTES'],'Fruit Name') !== false){
$fruitFlag = true;
continue;
}
if(strpos($value['NOTES'],'Animal Name') !== false){
$fruitFlag = false;
}
if($fruitFlag == true){
$json_new_param[] = [
'FRUITS' => $value['NOTES']
];
}
}
echo json_encode($json_new_param, JSON_PRETTY_PRINT);
{
"list": [
{
"id": 62,
"user_id": "34",
"updated_at": "2017-10-15 08:24:32",
"request": {
"data": "yes",
"watch": "Pending"
}
},
{
"id": 63,
"user_id": "34",
"updated_at": "2017-10-15 08:24:32",
"request": {
"data": "yes",
"watch": "Yes"
}
}, ]
}
How can i get the watch value to under the updated_at
As Expected
"list": [
{
"id": 62,
"user_id": "34",
"updated_at": "2017-10-15 08:24:32",
"watch": "Pending"
}..
I tried using array_column but doesn't work for me.
function array_column($array, $column){
$ret = array();
foreach($array as $row){
$ret[] = $row[$column];
return $ret;
}
}
Somewhat like
function updateArray(&$array){
foreach($array['list'] as $key => $item){
$array['list'][$key]['watch'] = $item['request']['watch'];
unset($array['list'][$key]['request']);
}
}
updateArray($array);
print_r($array);
Demo
I assume that you know that you need to decode the JSON first:
$array = json_decode($json, true);
Then access the array under the list element:
$result = array_column($array['list'], 'updated_at');
It was a good challenge.here you are
<pre>
<?php
$array=array("list"=>array(array("id"=>"62","user_id"=>"32","updated_at"=>2017,"request"=>array("data"=>"yes","watch"=>"panddin")),
array("id"=>"60","user_id"=>"30","updated_at"=>2016,"request"=>array("data"=>"no","watch"=>"pan"))));
//print_r($array);
foreach($array as $row){
foreach ($row as $key => $value){
if((is_array($value))){
foreach ($value as $key1 => $k){
if((is_array($k))){
foreach ($k as $key2 => $value2) {
echo $key2."=>". $value2."<br>";
}
}else echo $key1."=>".$k."<br>";
}
}else echo $key."=>".$value. "<br>";
}
}
?>
</pre>
I have a JSON file like this:
[
{
"set_id": "10001",
"name": "4x4 car",
"img_url": "https://xxx.xxx.com/img/sets-b/10001.jpg",
"parts": [
{
"part_id": "1001",
"qty": "2",
"color_id": "0",
"type": 1
},
{
"part_id": "1002",
"qty": "2",
"color_id": "0",
"type": 1
},
{
"part_id": "1003",
"qty": "2",
"color_id": "0",
"type": 1
},
{
"part_id": "1004",
"qty": "2",
"color_id": "0",
"type": 1
}
]
}
]
I am using PHP code like this:
while($item = array_shift($json_output))
{
foreach ($item as $key => $value)
{
echo $key.' => '.$value."\n";
}
}
But it returns the result:
set_id => 9398-1 descr => 4 x 4 Crawler set_img_url => https://img.rebrickable.com/img/sets-b/9398-1.jpg parts => Array
How do I want to return the parts info?
If you want only the parts
foreach ($item{"parts"] as $key => $value)
{
echo $key.' => '.$value."\n";
}
Sample here: http://sandbox.onlinephpfunctions.com
If you want every 2 level property:
$items=json_decode($json,true);
foreach ($items as $item) {
foreach ($item as $key => $value)
{
if( is_array($value) ) {
foreach ($value as $subkey => $part)
{
foreach ($part as $partkey => $partvalue)
{
echo $key .' '. $subkey . ' ['.$partkey.'] => '.$partvalue."\n";
}
}
} else {
echo $key.' => '.$value."\n";
}
}
}
Maybe you can use $array_json = json_decode($json_var, true) and then $parts = $array_json["parts"]; (I haven't execute the code, maybe has some errors)
http://php.net/manual/en/function.json-decode.php
I wrote the code that selects data from database, groups array by 2 parameters (match_day and competition_id) and generates JSON
foreach ($result as $res) {
$day = date('d', $res["match_start_time"]);
$json["response"][] = array(
"competition_id" => $res["competition_id"],
"match_start_time" => $res["match_start_time"],
"match_day" => $day
);
}
$arr = array();
$arr["items"] = array();
foreach($json["response"] as $key => $item)
{
if(!isset($arr['items'][$item['match_day']][$item['competition_id']])){
$arr['items'][$item['match_day']][$item['competition_id']] = array();
$arr['items'][$item['match_day']][$item['competition_id']]['league'] = $item['competition_id'];
$arr['items'][$item['match_day']][$item['competition_id']]['day'] = $item['match_day'];
}
$arr['items'][$item['match_day']][$item['competition_id']]['matches'][] = $item;
}
$arr['items'] = array_values($arr['items']);
echo json_encode($arr);
This code gives me following JSON
{
"items": [
{
"cuefa": {
"league": "cuefa",
"day": "01",
"matches": [
{
"competition_id": "cuefa",
"match_start_time": "1285873500"
}
]
}
},
{
"cuefa": {
"league": "cuefa",
"day": "30",
"matches": [
{
"competition_id": "cuefa",
"match_start_time": "1285866000"
}
]
}
}
]
}
I can't remove competition_id inside of items array. How to change code to get following JSON?
{
"items": [
{
"block": [
{
"league": "cuefa",
"day": "01",
"matches": [
{
"competition_id": "cuefa",
"match_start_time": "1285873500"
}
]
}
]
},
{
"block": [
{
"league": "cuefa",
"day": "30",
"matches": [
{
"competition_id": "cuefa",
"match_start_time": "1285866000"
}
]
}
]
}
]
}
Add this before the json_encode:
foreach ($arr['items'] as &$item) {
$item = array('block' => array_values($item));
}