get json date by array in php [duplicate] - php

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 6 years ago.
just sample data of json
{
"title":"Wedding Hair Photo Montage",
"app_id":"com.blue.weddinghairphotomontage", "developer":"Blue Photo Montage",
"developer_id":"Blue Photo Montage",
"image":"https://sampake.net/uda.jpg",
"rating_width":"width: 66.66666507720947%;",
"time":1469293200,"Version":"1.2"
}
my php code
<?php foreach( $recent_download as $key => $recent ) {
get_template( 'templates/app-item', $recent );
}?>
i needed Sort data by time

Looks like something similar was asked before: Sort JSON object in PHP by a key value
In case you are having trouble loading the JSON String into memory, you can use the following:
$jsonArray = json_decode($jsonString,true);

Related

Search and delete specific object inside an array in PHP [duplicate]

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)]);

How to get Json Objects inside the Json Array and store it in MySql In php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want get Json objects from the json array and store each values in mysql using php.
$arr = "[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]";
try this code
Using json_decode
<?php
$arr = '[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]';
$data = json_decode($arr);
echo "<pre>";
print_r($data);
foreach($data as $k=>$v){
echo $v->fruit." ".$v->isSelected." ".$v->number."<br>"; //store in db here using value
print_r($v);
}
?>

How to make json objects within array in php [duplicate]

This question already has answers here:
How to create an array for JSON using PHP?
(8 answers)
Closed 8 years ago.
I want to make json as followsin php:
{["key1":"value1"],["key2":"value2"]}
Can any one help?
See the PHP manual for json_encode() and json_decode():
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
You can do it like this: set it to true, if you want an associative array.
$array = json_decode('[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]',true);
if you want to iterate:
foreach($array as $item) { //foreach element in $arr
$uses = $item['var1']; //etc
}

Can i use ip address as Object array key? [duplicate]

This question already has answers here:
PHP json_decode notation issue
(2 answers)
Closed 8 years ago.
I m having json file as
{"103.186.190.216":1398640126}
i read the file and json decode it returs as
stdClass Object ( [203.196.190.226] => 1398640126 )
How can i print simple as
echo $json_arr->203.196.190.226;
Can have ip as key ?
i know can retrieve it by
foreach ($json_arr as $key => $val) {
echo $val;
}
Or how can print it in for loop rather than foreach
And also how can push values inside these type of object array
Coz normal php array_push(); Didn't work.
Or on the lighter note what is best way to create these type of array which we want give the key And value dynamically ?
Use this syntax:
echo $json_arr->{'203.196.190.226'};

Get array output using php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php loop through associative arrays
i have this piece of code which gets me as an output an array like this here:
http://penelope-ns.net/fb/fig.jpg
How can i save the [name] as a variable $name?
The code i use to generate is this:
$fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); ///// Add this line
As far as I can see:
$fbid = 500591729;
$name = $array[$fbid] ['data'] [0] ['name'];
Or for all names:
foreach ($array as $fbid=>$value) {
echo $value['data'][0]['name'];
}

Categories