This question already has answers here:
Remove an element from Json array
(2 answers)
Closed 2 years ago.
I have structure of an array like this :
[{"name":"username1","id":0},{"name":"username2","id":1}]
in my mysql database it is json encoded
i am trying to search for a specific id and delete that object and save other array as it is.
$arr_data=json_decode($jsonData);
foreach(arr_data as $k=> $data)
{
if($request->id==$data->id)
{
unset($arr_data[$k]);
$updateData = DataTable::where(['id'=>$request->id])
->update(['update_column' =>json_encode($arr_data)]);
}
}
Issue is my data is not stored in proper format after deleting.
New json formatted data is store in this format :
{"1":{"name":"username1","id":1}}
Any suggestion to solve this issue.
This works for me
$jsonData = '[{"name":"username1","id":0},{"name":"username2","id":1},{"name":"username2","id":2}]';
$arr_data=json_decode($jsonData);
if(($index = array_search($request->id, array_column( $arr_data, 'id'))) !== false) {
unset($arr_data[$index]);
$arr_data = array_values($arr_data);
}
$updateData = DataTable::where(['id'=>$request->id])
->update(['update_column' =>json_encode($arr_data)]);
Related
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 3 months ago.
I have generated html table data to Json array using jquery, it worked for me and I got the array below, I need php code to iterate each value of this array in php codeigniter 4 controller.
array(1){
[
0
]=>string(966)"[{"ID":"\n2","Name":"\nCP","Price":"\n350.20"},{"ID":"\n3","Name":"\nLFT","Price":"\n700.10"},{"ID":"\n4","Name":"\nRFT","Price":"\n200"},{"ID":"\n5","Name":"\nurinetest","Price":"\n1000"}]"
}
It's a php array that contains a json array, if you are sure you will only have one element in the array, you can do so:
$array = json_decode($var[0]); // $var holds the data you mentioned above
foreach ($array as $item) {
//
}
otherwise:
foreach ($var as $array) { // $var holds the data you mentioned above
$array = json_decode($array);
foreach ($array as $item) {
//
}
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 months ago.
I have output file json but i can't read the properties in array [0]
contacts:
Array(20)
0:
canonical-vid:00000
form-submissions:[]
identity-profiles:[{…}]
is-contact:true
merge-audits:[]
merged-vids:[]
portal-id:000000
properties:{....}
I use this code in php :
$json = file_get_contents('./file,json');
$data = json_decode($json,true);
$firstname = $data['contacts'][0]['properties']['firstname']['value'];
I think $data['contacts'][0]['properties'] is json. Try like this:
$properties = $data['contacts'][0]['properties'];
$nameData = json_decode($properties,true);
This question already has answers here:
How to create a JSON object
(5 answers)
Closed 3 years ago.
I have the following PHP code, which gets executed when I hit the Submit button in a form:
// Save data
if(isset($_POST['save'])){
$newDataArr = array();
foreach ($data_array[0] as $k=>$v){
$newDataArr[] = array($k => $_POST[$k]);
}// ./ foreach
echo json_encode($newDataArr);
}
The echo I get is the following:
[{"ID->id":"esKZSCDfIC"},{"DT->createdAt":"2020-01-26T13:02:42"},{"DT->updatedAt":"2020-01-26T13:02:42"},{"ST->aString":"hey1"},{"NU->number":123},{"GPS->coords":["2.2222","44.4444"]},{"BL->aBool":false},{"AR->theArray":["xx","ww"]},{"FL->theFile":"https:\/\/xscoder.com\/xserver\/uploads\/6dydDtoTt5JjZzFc5L5V_image.jpg"},{"PO->userPointer":"vbN3b0C7bC"}]
Then I'll have to add that array into my JSON file as it follows:
$data = json_encode(array_values($newDataArr), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents('Test.json', $data);
But of course the syntax of my $newDataArr is not the right one, each JSON object must not be inside the { }, so this:
{"ID->id":"esKZSCDfIC"},
must become:
"ID->id":"esKZSCDfIC",
Is there a way to dynamically create a valid JSON array and push it to my Test.json file?
I've been through many posts on StackOverflow, but the result I get is always the same. I must use the PHP code above to get keys and values from my HTML inputs.
Instead of creating and pushing a new array on each iteration, set the values this way:
$newDataArr[$k] = $_POST[$k];
Hope this helps.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);
This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array