Related
This question already has answers here:
Get min and max value in PHP Array
(9 answers)
Closed 4 months ago.
I have the following array $data. n and 0 are identical, it's just the way the data is returned. I need to determine the max and min values for the id value, it looks like the id values are stored as strings, how is this achieved?
Array
(
[0] => stdClass Object
(
[n] => artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}
[0] => artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}
)
[1] => stdClass Object
(
[n] => artefact[10.4]{"id": "2", "name": "002", "type": "Artefact"}
[0] => artefact[10.4]{"id": "2", "name": "002", "type": "Artefact"}
)
[2] => stdClass Object
(
[n] => artefact[10.5]{"id": "3", "name": "003", "type": "Artefact"}
[0] => artefact[10.5]{"id": "3", "name": "003", "type": "Artefact"}
)
)
It seems that {"id":....} is a JSON value, and we need to parse the value and collect IDs. Before we need to clean artefact string with preg_replace function. In this case, we can determine max and min values:
<?php
$data = [
[
'0'=>'artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}',
'n'=>'artefact[10.3]{"id": "1", "name": "001", "type": "Artefact"}',
],
[
'0'=>'artefact[10.4]{"id": "1", "name": "001", "type": "Artefact"}',
'n'=>'artefact[10.4]{"id": "3", "name": "001", "type": "Artefact"}',
],
];
$ids = [];
foreach ($data as $datum) {
foreach ($datum as $item) {
$parsedItem = preg_replace("/artefact\[(.+?)\]/",'',$item);
$jsonDecoded = json_decode($parsedItem);
$ids[] = $jsonDecoded->id;
}
}
$maxId = $ids ? max($ids):0;
$minId = $ids ? min($ids):0;
echo "Max id: $maxId <br> Min id $minId ";
I just learned php
I have a separate database table, and I want to combine that table with only 1 parameter with JSON output, but the resulting output is wrong, how to change my JSON to json which is correct and easy to use.
thanks
json output :
[
[
{
"id_service": "3",
"reference_number": "",
"tracking_number": "RJC-0000-0001",
"kd_inbound": "INB-1000-0001",
"tgl_inbound": "2019-11-07 00:00:00",
"status_inb": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbag": "BAG-1468-0002",
"tanggal_outbag": "2019-11-07 00:00:00",
"status_outbag": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbound": "OTB-1826-0001",
"tgl_outbound": "2019-11-07 17:04:49",
"status_otb": "1"
}
],
]
this my code
public function awb_get() {
$id = $this->get('tracking_number');
$res= array(
$this->M_tarif->tampil_status_inbound($id),
$this->M_tarif->tampil_status_otboundbag($id),
$this->M_tarif->tampil_status_otboundori($id),
$this->M_tarif->tampil_status_indes($id),
$this->M_tarif->tampil_status_outdes($id),
$this->M_tarif->tampil_status_runsheet($id),
$this->M_tarif->tampil_db_service_status($id)
);
$this->response($res, 200);
}
i want in like this
{
"status": 200,
"error": false,
"awb": [
{
"tracking_number": "RJC-0000-0004",
"status": "order",
"tanggal": "2019-10-30"
},
{
"tracking_number": "RJC-0000-0004",
"status": "Inbound to origin",
"tanggal": "2019-11-03"
}
]
}
Your json response is not correct.
So, first you have to reset your JSON before send to function. I was make sample to rearrange JSON and convert it in stdClass Object. Please check below code to convert with your response.
$arr ='[
[
{
"id_service": "3",
"reference_number": "",
"tracking_number": "RJC-0000-0001",
"kd_inbound": "INB-1000-0001",
"tgl_inbound": "2019-11-07 00:00:00",
"status_inb": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbag": "BAG-1468-0002",
"tanggal_outbag": "2019-11-07 00:00:00",
"status_outbag": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbound": "OTB-1826-0001",
"tgl_outbound": "2019-11-07 17:04:49",
"status_otb": "1"
}
],
]';
$result = str_replace(array('[',']','\n'), '',htmlspecialchars(json_encode($arr), ENT_NOQUOTES));
$str = preg_replace('/\\\"/',"\"", $result);
$json = '[';
$json .= substr($str, 2,-1); // Substring -1 character from the end of the json variable, this will be the trailing comma.
$json .= ']';
$jsonData = preg_replace("/,(?!.*,)/", "", $json);
echo "<pre>";
print_r(json_decode($jsonData));
echo "</pre>";
output:
Array
(
[0] => stdClass Object
(
[id_service] => 3
[reference_number] =>
[tracking_number] => RJC-0000-0001
[kd_inbound] => INB-1000-0001
[tgl_inbound] => 2019-11-07 00:00:00
[status_inb] => 1
)
[1] => stdClass Object
(
[id_service] => 3
[reference_number] =>
[kd_outbag] => BAG-1468-0002
[tanggal_outbag] => 2019-11-07 00:00:00
[status_outbag] => 1
)
[2] => stdClass Object
(
[id_service] => 3
[reference_number] =>
[kd_outbound] => OTB-1826-0001
[tgl_outbound] => 2019-11-07 17:04:49
[status_otb] => 1
)
)
Here i have one array(first array) inside i have one more array(second array), now i want to display only first image from second array(galleryImages) , how can do this. i tried but i am not able to get the results
print_r($response);
Array
(
[0] => stdClass Object
(
[gallery_id] => 2
[title] => Annual Day 2017
[description] =>
[galleryImages] => ["1.jpg","2.jpg","3.jpg","4.jpg"]
[reg_on] => 2017-05-17 01:55:12
[created_by] => rajeshdash123#gmail.com
[school_id] => 2
[status] => 0
)
[1] => stdClass Object
(
[gallery_id] => 3
[title] => Sports Day
[description] =>
[galleryImages] => ["1.jpg","2.jpg","3.jpg"]
[reg_on] => 2017-05-17 01:55:36
[created_by] => rajeshdash123#gmail.com
[school_id] => 2
[status] => 0
)
)
Expected Results
{
"status": "Success",
"data": [
{
"gallery_id": "2",
"title": "Annual Day 2017",
"description": "",
"galleryImagesCount": 4,
"gallery":"1.jpg"
},
{
"gallery_id": "3",
"title": "Sports Day 2017",
"description": "",
"galleryImagesCount": 4,
"gallery":"1.jpg"
}
],
}
I tried like this but is i am not getting the exact results
$images = array();
foreach ($response as $key => $value)
{
$img['gallery_id'] = $value->gallery_id;
$img['title'] = $value->title;
$img['description'] = $value->description;
$img['galleryImagesCount'] = count(json_decode($value->galleryImages,true));
$img['gallery'] = json_decode($value->galleryImages,true);
array_push($images,$img);
}
$return=array('status'=>"Success",'Images'=>$images);
echo json_encode($return);
Getting Results
{
"status": "Success",
"Images": [
{
"gallery_id": "2",
"title": "Annual Day 2017",
"description": "",
"galleryImagesCount": 4,
"gallery": [
"d17ac9d0aeb6435eaa294e0d69d4cc8f.jpg",
"a91945e0cf55379f51cf5faef10d7a4a.jpg",
"2d1501045ddbb3ccc238e70f9af05027.jpg",
"071c3b5f969bed1d1e2ee4b6531e4444.jpg"
]
},
{
"gallery_id": "3",
"title": "Sports Day",
"description": "",
"galleryImagesCount": 4,
"gallery": [
"f0ba574fd46a01ff5a41855a97c710ca.jpg",
"1d10802f1b74e660117f36bd6dd0aa26.jpg",
"e705fb66f767a1b914200ca8d3cae700.jpg",
"3d5d8828331e13d3decc94021a64e5ca.jpg"
]
}
]
}
Here what happening means gallery is coming an array , for me don't want array i need first image only, please check my expected results, update the answer
Updated expected results
{
"status": "Success",
"Images": [
{
"gallery_id": "2",
"title": "Annual Day 2017",
"description": "",
"galleryImagesCount": 4,
"gallery": [
{
"galleryimage": "1.jpg"
},
{
"galleryimage": "2.jpg"
}
]
},
{
"gallery_id": "3",
"title": "Sports Day",
"description": "",
"galleryImagesCount": 4,
"gallery": [
{
"galleryimage": "1.jpg"
},
{
"galleryimage": "2.jpg"
}
]
}
]
}
Instead of [galleryImages] => ["1.jpg","2.jpg","3.jpg"], use for loop to iterate through galleryImages.
Create an associative array with key =>value pair like [galleryImages] => ["galleryimage1" => "1.jpg", "galleryimage2" => "2.jpg", "galleryimage3" => "3.jpg"].
While json_decode you will get intended output.
My changes and explanations are in the code block:
Code: (Demo)
// assumed that previous line was something like $response=json_decode($json);
$response=[
(object)[
'gallery_id'=>2,
'title'=>'Annual Day 2017',
'description'=>'',
'galleryImages'=>["1.jpg","2.jpg","3.jpg","4.jpg"],
'reg_on'=>'2017-05-17 01:55:12',
'created_by'=>'rajeshdash123#gmail.com',
'school_id'=>2,
'status'=>0
],
(object)[
'gallery_id'=>3,
'title'=>'Sports Day',
'description'=>'',
'galleryImages'=>["1.jpg","2.jpg","3.jpg"],
'reg_on'=>'2017-05-17 01:55:36',
'created_by'=>'rajeshdash123#gmail.com',
'school_id'=>2,
'status'=>0
]
];
foreach ($response as $value){ // removed $key=> because it was unnecessary
$img['gallery_id'] = $value->gallery_id;
$img['title'] = $value->title;
$img['description'] = $value->description;
$img['galleryImagesCount'] = count($value->galleryImages); // removed json_decode()
$img['gallery'] = $value->galleryImages[0]; // removed json_decode and added [0] to access first
$images[]=$img; // swapped push() call with identical function-less "push"
}
if(isset($images)){ // added this condition to ensure there was something to return
$return=array('status'=>"Success",'Images'=>$images);
//var_export($return);
echo json_encode($return);
}else{
// enter some sort of error message / default behavior
}
Output:
{"status":"Success","Images":[{"gallery_id":2,"title":"Annual Day 2017","description":"","galleryImagesCount":4,"gallery":"1.jpg"},{"gallery_id":3,"title":"Sports Day","description":"","galleryImagesCount":3,"gallery":"1.jpg"}]}
I'm trying to set a custom field of type Multiselect via JIRA rest API using php. For reason I'm getting the following error:
{"errorMessages":[],"errors":{"Functionality Impacted":"expected 'value' property to be a string"}}
I have the following code for setting the custom field:
public function requestRemedy($summary, $description, $priority, $goliveDate,$startTime,
$endTime,$clientImpact,$impactedFunctionality,$fundTransfers ,$fieldAffected, $remedyChangeType,
$changeReason,$remedyImpact, $urgency,$riskLevel,$nameRequestor,$emailRequestor)
{
$json = Array ( "fields" => Array (
"project" => Array
( "id" => 10051 ),
"summary" => $summary,
"description" =>$description,
"issuetype" => Array ( "name" => "Remedy Change Management" ),
"priority" => Array("id" => $priority),
"customfield_13774" => $goliveDate,
"customfield_14408" => "$startTime",
"customfield_14409" => "$endTime",
"customfield_14412" => "$clientImpact",
"customfield_14908" =>Array ( 0 => Array(
"value" => ($impactedFunctionality),
)),
"customfield_14414" => Array("id" =>$fundTransfers),
// "customfield_14415" =>Array("id" => $businessOnline,"child" =>Array("id" => $businessDependency) ),
"customfield_14602" => Array("id" =>$fieldAffected),
"customfield_14422" => Array("id" =>$remedyChangeType),
"customfield_14423" => Array("id" =>$changeReason),
"customfield_14424" => Array("id" =>$remedyImpact),
"customfield_14425" => Array("id" =>$urgency),
"customfield_14426" => Array("id" =>$riskLevel),
"customfield_14700"=>$nameRequestor,
"customfield_14702"=>$emailRequestor
)
);
return $json;
}
After adding some dummy data to test the final structure of json, I get the following result:
{
"fields": {
"project": {
"id": 10051
},
"summary": "Test",
"description": "Human DeSC",
"issuetype": {
"name": "Remedy Change Management"
},
"priority": {
"id": "2"
},
"customfield_13774": "2016-11-08",
"customfield_14408": "2016-11-01T07:35:00.000+0200",
"customfield_14409": "2016-11-01T08:35:00.000+0200",
"customfield_14412": "cLIENT iMPACT",
"customfield_14908": [{
"value": ["Savings Accounts", "Current Accounts", "Call Deposits"]
}],
"customfield_14414": {
"id": "13647"
},
"customfield_14602": {
"id": "14022"
},
"customfield_14422": {
"id": "13712"
},
"customfield_14423": {
"id": "13718"
},
"customfield_14424": {
"id": "13722"
},
"customfield_14425": {
"id": "13726"
},
"customfield_14426": {
"id": "13730"
},
"customfield_14700": "Pastor Dan",
"customfield_14702": "email#time.com"
}
}
Which means the issue is here:
"customfield_14908": [{
"value": ["Savings Accounts", "Current Accounts", "Call Deposits"]
}],
the expected structure is:
"customfield_10008": [ {"value": "Savings Accounts" }, {"value": "Current Accounts" }, {"value": "Call Deposits" }]
Now I'm not sure how to build this using an associative array
The custom field I'm trying to set is customfield_14908. What I'm finding weird is that I used similar code for setting a multiselect field on JIRA version 7.2.1 and it works well, but not working on JIRA version 7.0.
I have a result from my MySQL DB that I'm json encoding in PHP, the result looks like
:
[
{
"id": "8488",
"name": "Tenby",
"area": "Area1"
},
{
"id": "8489",
"name": "Harbour",
"area": "Area1"
},
{
"id": "8490",
"name": "Mobius",
"area": "Area1"
}
]
What I would like to do is to add a new key/value pair to that JSON so that it will be :
[
{
"id": "8488",
"name": "Tenby",
"area": "Area1",
"image": "1278.jpg"
},
{
"id": "8489",
"name": "Harbour",
"area": "Area1",
"image": "1279.jpg"
},
{
"id": "8490",
"name": "Mobius",
"area": "Area1",
"image": "1280.jpg"
}
]
So how can I do that in PHP?
<?php
$data[0]['id']="8488";
$data[0]['name']="Tenby";
$data[0]['area']="Area1";
$data[1]['id']="8489";
$data[1]['name']="Harbour";
$data[1]['area']="Area1";
$data[2]['id']="8490";
$data[2]['name']="Mobius";
$data[2]['area']="Area1";
echo json_encode($data)."<br/>";
/*Add Image element (or whatever) into the array according to your needs*/
$data[0]['image']="1278.jpg";
$data[1]['image']="1279.jpg";
$data[2]['image']="1280.jpg";
echo json_encode($data);
?>
PHP is not very good with JSON. Its best to convert from JSON to Array to do this - what #egig has also recommended.
Example code:
$temp = json_decode($json);
$temp[] = new data, whatever you want to add...;
$json = json_encode($temp);
Hope this helps.
hu?
the result of the db query will be an array or an object...
add the additional entry to that data, only encode after every necessary data manipulation is done
alternatives, but clumsy (horrible):
json_decode, add stuff, json_encode
build your additional data as a string, str_replace for example "area": "Area1" in your json string with "area": "Area1", "image": "1278.jpg"
but really:
output formatting like json_encode should only be done once you are sure that you have the whole output together and it is send out
Maybe things have changed since then, but I know this will work at this current stage:-
So here is the JSON string:-
$strJSON = '[
{
"id": "8488",
"name": "Tenby",
"area": "Area1"
},
{
"id": "8489",
"name": "Harbour",
"area": "Area1"
},
{
"id": "8490",
"name": "Mobius",
"area": "Area1"
}
]';
If this is still a string, then I would convert it into an object like this:-
$json = json_decode( $strJSON );
Now that it is in object format, to add my "image" keys with their values, I would then add them like it is shown below:-
$json[0]->image = "1278.jpg";
$json[1]->image = "1279.jpg";
$json[2]->image = "1280.jpg";
So then if I add the code below after the above code:-
echo "<pre>";
print_r( $json );
echo "</pre>";
We should then see it outputting on the page like so:-
Array
(
[0] => stdClass Object
(
[id] => 8488
[name] => Tenby
[area] => Area1
[image] => 1278.jpg
)
[1] => stdClass Object
(
[id] => 8489
[name] => Harbour
[area] => Area1
[image] => 1279.jpg
)
[2] => stdClass Object
(
[id] => 8490
[name] => Mobius
[area] => Area1
[image] => 1280.jpg
)
)
//$index = some value in array;
for($i=0;$i<count($index);$i++){
$data[$i]->key = $index[$i];
}
print $data;