This is my code for array creation
$insured_data['QuotaDtls']['Riskdtls'] = [
"InsuredName"=> "Testone",
];
$insured_data['Authenticate'] = [
'WACode' => '0000',
];
$insured_data['QuotaDtls'] = [
'ProductType'=> 'Individual'
];
Output:
{
"Authenticate": {
"WACode": "0000",
},
"QuotaDtls": {
"ProductType": "Individual",
"Riskdtls": {
"InsuredName": "Testone",
}
}
}
I want to do some modifications in this array tried many different ways but not able to do. This is done in code igniter, Please help.
{
"Authenticate": {
"WACode": "0000"
},
"QuotaDtls": {
"ProductType": "Individual",
"Riskdtls": [
{
"InsuredName": "Testone",
}
]
}
}
You need to make Riskdtls an array further in $insured_data['QuotaDtls']['Riskdtls'] like following
$insured_data['Authenticate'] = [
'WACode' => '0000',
];
$insured_data['QuotaDtls'] = [
'ProductType'=> 'Individual'
];
$insured_data['QuotaDtls']['Riskdtls'][] = [
"InsuredName"=> "Testone",
"entry2"=> "testdata2",
"entry3"=> "testdata3"
];
$json = json_encode($insured_data,JSON_PRETTY_PRINT) ;
printf("<pre>%s</pre>", $json);
The desired output would be achieved by encoding the array to JSON format using json_encode function and the final output would be following:
{
"Authenticate": {
"WACode": "0000"
},
"QuotaDtls": {
"ProductType": "Individual",
"Riskdtls": [
{
"InsuredName": "Testone",
"entry2": "testdata2",
"entry3": "testdata3"
}
]
}
}
Related
I have the code below, but the result of the json response doesn't match
public function tes_get(){
$kode= $this->M_mymodel->tbl_kode('302'); // row
$res = array();
foreach ($kode as $key=> $value) {
$win = $this->M_mymodel->db_aa('302'); //row_array
$res = $this->M_mymodel->db_bb('302','LOSE'); //row_array
$res['data'][] = array(
'win' => $win['menang'],
'lose' =>$res['kalah']
);
}
$response = $this->set_response($res,200);
}
Below is the result of the response from the code I made above
{
"data": [
{
"win": "2",
"lose": "11"
}
]
}
How to make json response like below?
{
"data": [
{
"win": "2",
}
{
"lose": "11"
}
]
}
You can try :
$res['data'][] = array(
array('win' => $win['menang']),
array('lose' =>$res['kalah'])
);
I am trying to get results when converting a url to json. I thank those who can help me, thank you.
I have this string: id_user123=123;456;789&id_user456=333;545;908
I would like to get this result:
{"result": [
{"id_user123":[ "123", "456", "789" ] },
{"id_user456":[ "333", "545", "908" ] }
]}
Use parse_url to get the "query" part of your string
and parse_str to get each parameter and values.
<?php
$url = 'test.php?id_user123=123;456;789&id_user456=333;545;908';
parse_str(parse_url($url, PHP_URL_QUERY), $queryArray);
$result = [];
foreach ($queryArray as $userId => $values) {
$result['result'][] = [
$userId => explode(';', $values)
];
}
echo "<pre>";
echo json_encode($result, JSON_PRETTY_PRINT);
echo "</pre>";
will output
{
"result": [
{
"id_user123": [
"123",
"456",
"789"
]
},
{
"id_user456": [
"333",
"545",
"908"
]
}
]
}
Simple loop with explode():
$result = ['result' => []];
foreach ($_GET as $userId => $values) {
$result[] = [
$userId => explode(';', $values);
];
}
echo json_encode($result);
I have php script which is storing data into an array and then converting into json array. Following is script
$gameTraining = array();
$index = 0;
foreach($todayTraining->toArray() as $training){
if($todayTraining[$index]['type'] === 'easy'){
$gameTraining['easy'][]['game_id'] = $todayTraining[$index]['game_id'];
}
$index++;
}
return $gameTraining;
And following response I am getting
{
"training": {
"easy": [
{
"game_id": 12
},
{
"game_id": 6
},
{
"game_id": 26
}
]
}
}
But I would like to remove the brackets from array, so can you kindly guide me how can I do that? I would like to convert as following
{
"training": {
"easy": [
"game_id": 12,
"game_id": 6,
"game_id": 26
]
}
}
You cannot have multiple items in an array with the same key. You can make an array with the ids for the game, so this line:
$gameTraining['easy'][]['game_id'] = $todayTraining[$index]['game_id'];
can be changed with this line:
$gameTraining['easy']['game_ids'][] = $todayTraining[$index]['game_id'];
Try something like this:
$todayTraining = [
[
'type' => 'easy',
'game_id' => 123
],
[
'type' => 'easy',
'game_id' => 456
]
];
$gameTraining = array();
$index = 0;
foreach($todayTraining as $training){
if($todayTraining[$index]['type'] === 'easy'){
$gameTraining['easy'][] = substr(json_encode(['game_id' => $todayTraining[$index]['game_id']]), 1, -1);
}
$index++;
}
echo json_encode($gameTraining, JSON_PRETTY_PRINT);
Just use below line :
$gameTraining['easy'][] = $todayTraining[$index]['game_id'];
Instead of :
$gameTraining['easy'][]['game_id'] = $todayTraining[$index]['game_id'];
Hopefully, it will work.
I parse Excel sheet and get this JSON:
[
{
"A":"Samsung",
"Groupe":{
"F":"TV",
"D":"HDR"
}
},
{
"A":null,
"Groupe":{
"F":null,
"D":null
}
},
{
"A":"Sony",
"Groupe":{
"F":"T.V",
"D":"LCD"
}
},
{
"A":"Sony",
"Groupe":{
"F":"PS4",
"D":"Pro edition"
}
},
{
"A":"Sony",
"Groupe":{
"F":"Smart Phone",
"D":"Quad core"
}
}
]
Php code:
$data = [];
for ($row = 15; $row <= 25; $row++) {
$data[] = [
'A' => $worksheet->getCell('A'.$row)->getValue(),
'Groupe' => [
'F' => $worksheet->getCell('F'.$row)->getValue(),
'D' => $worksheet->getCell('D'.$row)->getValue()
]
];
}
How can I organize(sort) json depending on "A"?
I tried this but I still couldn't merge "Groupe" for same "A" together:
Take away NULL colomns.
Create a copy of the Array.
Regroup fields for same element in the new Array(this didnt work)
Code:
$data1 = [];
for ($l = 0; $l < count($data); $l++){
$data1[$l] = $data[$l];
}
for ($j = 0; $j < count($data); $j++) {
if($data[$j]['A'] != NULL){
if($data[$j]['A'] !== $data[$j+1]['A']){
$data1[$j] = $data[$j];
}
else{
$data1[$j]['A']= $data[$j]['A'];
$data1[$j]['Groupe']= array_merge($data[$j]['Groupe'], $data[$j+1]['Groupe']);
}
}
}
EDIT:
The result that I'm getting for $data1 is exactly the same as the input JSON(except that NULL was deleted), so it looks like merge Array didnt work and what I need is:
[
{
"A":"Samsung",
"Groupe":{
"F":"TV",
"D":"HDR"
}
},
{
"A":"Sony",
"Groupe": [{
"F":"T.V",
"D":"LCD"
},{
"F":"PS4",
"D":"Pro edition"
}, {"F":"Smart Phone",
"D":"Quad core"
}]
}]
Plus it's showing me this :
Notice: Undefined offset: 11 in C:\xampp\htdocs\phptoexcel.php on line
43
Line 43: if($data[$j]['A'] !== $data[$j+1]['A']){
Use the A value as key in $data, so you can group by it:
$data = [];
for ($row = 15; $row <= 25; $row++) {
//get A value, skip if A = NULL
$a = $worksheet->getCell('A'.$row)->getValue(),
if($a===NULL)continue;
//get F and D VALUE, skip if one of them = NULL
$f = $worksheet->getCell('F'.$row)->getValue();
$d = $worksheet->getCell('D'.$row)->getValue();
if($f===null || $d===null)continue;
//test if A is a key in $data. If not, create
if(!array_key_exist( $a, $data ){
$data[$a]=[
'A'=>$a,
'Groupe'=>[]
];
}
//Put F and D in a new array in Groupe
$data[$a]['Groupe'][]=["F"=>$f,"D"=>$d];
}
You will end up with:
$data=>
[ "Samsung" =>[ "A" => "Samsung",
"Groupe" => [ 0 =>[ "F" => "TV",
"D" => "HDR"
]
]
],
"Sony" => [ "A" => "Sony",
"Groupe" => [ 0 =>[ "F":"TV",
"D":"HDR"
],
1 =>[ "F":"T.V",
"D":"LCD"
],
2 =>[ "F":"PS4",
"D":"Pro edition"
],
3 =>[ "F":"Smart Phone",
"D":"Quad core"
],
]
]
Try This
$arrUnique = array();
$result = array();
$i=0;
foreach($data as $value){
if($value['A']!=null){
$data1 = [];
$intID = $value['A'];
if( in_array( $intID, $arrUnique ) ) {
$key = array_search ($intID, $arrUnique);
$result[$key]['Groupe'][] = $value['Groupe'];
}else{
$data1['A'] = $value['A'];
$data1['Groupe'][] = $value['Groupe'];
$result[$i]=$data1;
$arrUnique[]=$value['A'];
$i++;
}
}
}
I usually don't perform JSON to JSON transformation using PHP but using jq command line utility.
Given your input JSON file, you can use this jq filter:
jq '[[sort_by(.A)|.[]|select(.A!=null)]|group_by(.A)|.[]as $i|{A:$i[].A,Groupe:$i|map(.Groupe)}]|unique' file
[
{
"A": "Samsung",
"Groupe": [
{
"F": "TV",
"D": "HDR"
}
]
},
{
"A": "Sony",
"Groupe": [
{
"F": "T.V",
"D": "LCD"
},
{
"F": "PS4",
"D": "Pro edition"
},
{
"F": "Smart Phone",
"D": "Quad core"
}
]
}
]
I have an issue with an API I'm developing that returns arrays of lat/lon coordinates. In order to not break the parser on the App side, I need the data to return as uniform nested arrays, like this:
{
coordinates: [
[
-88.451,
41.41
],
[
-85.123,
40.10
],
[
-59.451,
42.42
],
[
-89.124,
44.10
]
]
}
But for some reason the data I'm pulling from is occasionally nested into groups like this:
{
coordinates: [
[
-88.451,
41.41
],
[
-85.123,
40.10
],
[
[
-59.451,
42.42
],
[
-89.124,
44.10
]
],
]
}
If you have a good way to merge these nested arrays into a uniform set of nested arrays [[lat,lon],[lat,lon]] I'd be eternally grateful.
Much thanks,
Rob
$myarray = [
[
-88.451,
41.41
],
[
-85.123,
40.10
],
[
-59.451,
42.42
],
[
-89.124,
44.10
]
];
function contains_array($array) {
foreach ($array as $val) {
if (is_array($val)) {
return true;
}
}
return false;
}
function array_flatten($array, $return) {
foreach ($array as $key => $value) {
if (is_array($value) && contains_array($value)) {
$return = array_flatten($value, $return);
} else {
$return[$key] = $value;
}
}
return $return;
}
$res = array_flatten($myarray, array());
var_dump($res);
This should do the trick