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'];
}
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 answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed last year.
I'm trying to add a value media_type to every child of the array. The code I'm using is:
while($row = $result->fetch_assoc()) {
$url = "example";
$path = $this->get_url($url);
$popular = ['media_type' => $row["type"],$path];
}
Here's an example of the array:
.
This content in the array is from themoviedb API. And is opensource.
Instead of overwrite array every time just use array_push like:
$popular[] = ['media_type' => $row["type"],$path];
Reference:
array_push
This question already has answers here:
How can I use a string within the tree of an array in PHP?
(2 answers)
Closed 3 years ago.
I have a string like below
"\$tempArray['acct'][0]".$det["PATH"]
$det["PATH"] is an dynamically generated in each iteration.
something like this "['e2']['2']['e2']['0']['e2']['0']"
I want an array to be defined like this
$tempArray['acct'][0]['e2']['2']['e2']['0']['e2']['0'] = array ();
Since $det["path"] changes in every iteration . I need this array to be created for in each iteration with the name
I have tried the below code . But it creates and array with $tmpArray. not like this $tempArray['acct'][0]['e2']['2']['e2']['0']['e2']['0']
if(!is_array("\$tempArray['acct'][0]".$det["PATH"])){
$tArray = "\$tempArray['acct'][0]".$det["PATH"];
var_dump($tArray );
$tArray = array();
The insecure way to do this is eval:
eval("\$tempArray['acct'][0]".$det["PATH"]." = array();");
The better way is to use the path as the key:
$tempArray['acct'][0][$det["PATH"]] = array();
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);
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
}