I am Creating A facebook that Retrieves 10 Random friends.But I need some code to Retrieve Top friends using comment and like ativity.I used Following code but i get below error
Invalid argument supplied for foreach()
below is the code i tried so far.
$statuses = $facebook->api('/me/statuses');
foreach($statuses['data'] as $status){
// processing likes array for calculating fanbase.
foreach($status['likes']['data'] as $likesData){
$frid = $likesData['id'];
$frname = $likesData['name'];
$friendArray[$frid] = $frname;
}
foreach($status['comments']['data'] as $comArray){
// processing comments array for calculating fanbase
$frid = $comArray['from']['id'];
$frname = $comArray['from']['name'];
}
That error message generally happens when your array variable is not set. If you could add line numbers to your code and give the full error message (including line number) it might help.
Can you show a print_r of $statuses['data']?
I've been trying to get this to work myself. Found the solution. $facebook->api() is going to return a json array. This is not a valid element for a foreach() statement. You need to use json_decode($statuses) in order to loop through the array in the foreach() statement.
Foreach through JSONArray in PHP
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
Here's the json
{"msg":"OK","server_time":"2021-11-19 16:41:22","status":200,"result":{"total_pages":1,"files":[{"download_url":"DOWNLOADLINKHERE1","single_img":"IMAGEURLHERE1","file_code":"CODEHERE1","title":"TITLEHERE1"},{"download_url":"DOWNLOADLINKHERE2","single_img":"IMAGEURLHERE2","file_code":"CODEHERE2","title":"TITLEHERE2"}],"results_total":"2","results":2}}
Here's my code
$json = json_decode($data);
foreach($json["result"] as $result){
foreach($result["files"] as $file){
echo $file["file_code"];
}
}
I want to extract all values from the "file_code". I got an error
Warning: Invalid argument supplied for foreach()
I was able get the VALUE of the first one using
echo $json->result->files[0]->file_code;
Is it possible to use a LOOP for the files[0]?
This line:
foreach($json["result"] as $result){
sees $json['result'] as an object, and so the next line tests for total_pages["files"], which doesn't exist.
Putting both foreach's together solves the problem:
$data='{"msg":"OK","server_time":"2021-11-19 16:41:22","status":200,"result":{"total_pages":1,"files":[{"download_url":"DOWNLOADLINKHERE1","single_img":"IMAGEURLHERE1","file_code":"CODEHERE1","title":"TITLEHERE1"},{"download_url":"DOWNLOADLINKHERE2","single_img":"IMAGEURLHERE2","file_code":"CODEHERE2","title":"TITLEHERE2"}],"results_total":"2","results":2}}';
$json = json_decode($data, true);
foreach($json["result"]["files"] as $file)
print $file["file_code"];
Teh playground
Alternatively, make the JSON result into an array, and use object property accessors instead of associative array bindings.
$data='{"msg":"OK","server_time":"2021-11-19 16:41:22","status":200,"result":[{"total_pages":1,"files":[{"download_url":"DOWNLOADLINKHERE1","single_img":"IMAGEURLHERE1","file_code":"CODEHERE1","title":"TITLEHERE1"},{"download_url":"DOWNLOADLINKHERE2","single_img":"IMAGEURLHERE2","file_code":"CODEHERE2","title":"TITLEHERE2"}],"results_total":"2","results":2}]}';
$json = json_decode($data);
foreach($json->result as $result){
foreach($result->files as $file){
echo $file->file_code;
}
}
Teh playground
I replicated your situation and it turns out that your JSON is invalid. You're missing a } at the end.
The reason for not getting an exception is because json_decode does not throw an error by default. You can make it do so by adding the JSON_THROW_ON_ERROR flag,
read the docs for more info.
This works perfect for me. If you have any thoughts please feel free to correct me.
$num = count($json->result->files);
echo $num;
for($i=0;$i<$num;$i++)
{
echo $json->result->files[$i]->file_code;
}
I am attempting to use the Airtable API to retrieve records from my data there - specifically, a list of URLs I have in column cells.
I wrote a function, get_airtable_records, to do the API call via curl and it works - returning results as a Json object. Specifically, I am pushing the URLs to an array, $article_urls.
The only problem is, Airtable limits the return of results to "pages" of a maximum 100 records, and my data contains more than that. The API accepts parameters maxRecords and pageSize but the more important one, pageSize, is still capped at 100.
What Airtable does also return is another Json value, offset, which is used in such cases for pagination. The offset is a record ID intended to be used as an input parameter (also called offset). You can use it to denote the starting record in a subsequent additional API call. I understand this.
What I don't understand is how to modify my code to account for the possibility of needing to poll Airtable again.
In other words, we should always do a starting run from scratch, when there is no offset value.
Then, if an offset value is present in returned results, we should go around again - until an offset value is not present.
Here is what I have.
// Make get request, store result in array
$articles = get_airtable_records($offset); // $offset won't exist at the start
// Prepare Article URLs list as an array
if (!isset($article_urls)) {
$article_urls = array();
}
// For each URL found in Airtable
foreach($articles['records'] as $record){
$url = $record['fields']['Published URL'];
// Add to our array list
if (!empty($url)) {
array_push($article_urls, $url);
}
}
// URL list after first pass:
echo '<pre>';
print_r($article_urls);
echo '</pre>';
// May hit a max of 100
// echo 'Offset: ' . $articles['offset'];
// Value like "itrJYSLx0RfslI80f/recEu6TiPTPCSDxg5" may exist.
// If so, go back to start, do get_airtable_records($offset) again and array_push
// Until, once more there is no "offset" value at end
I am speculating that some sort of while loop will be useful... ?
A couple of things are true...
In the first call, there will be no originating offset value needing to be passed, since it starts from record 0.
But that and subsequent passes may generate an offset value, which should be used to make another pass.
The final call will not generate an offset value, since it will have returned the final page of exhausted results, and there is no need to start again.
Thanks largely to #anthony's answer to a similar question here, I seem to have some working code...
// Prepare Article URLs list as an array
$article_urls = array();
// Call Airtable records in pages of 100 max
do {
// Offset is either inherited from last page's results, or is nothing
$offset = $articles['offset'] ?: "";
// Make get request, store result in array
$articles = get_airtable_records($offset);
// For each URL found in Airtable
foreach($articles['records'] as $record){
$url = $record['fields']['Published url'];
// Add to our array list
if (!empty($url)) {
array_push($article_urls, $url);
}
}
} while(!empty($articles['offset'])); // If there's an offset value (ie. starting record of next page), do again
// Output URL list for check
echo '<pre>';
print_r($article_urls);
echo '</pre>';
Explanation seems to be:
Use a do while loop.
At the start of this, set offset to be either the value inherited from the previous run, or nothing.
My get_airtable_records function was already limiting the presence or not of offset in the API call, with the following, which adds the offset query string to the URL for the next API call if one is present...
if (!empty($offset)) {
$q_offset = '&offset='.$offset;
}
I have tested this and it gave me all 137 results from two pages in to my $article_urls array. I haven't tested it with any more than two pages of results.
Recursive anonymous functions also work well for an auto paginator.
// http client
$client
// closure
$autoPaginate = function (int $offset) use (&$autoPaginate, $client) {
$response = $client->get('https://blah.com, [
'offset' => $offset,
'per_page' => 100,
]);
// do response business logic/work
// check your response meta or
// wherever the next page or offset is passed back
if (!$response->meta->next->offset) return;
return $autoPaginate($response->meta->next->offset);
};
$autoPaginate(0);
I have a script that loops through and retrieves some specified values and adds them to a php array. I then have it return the value to this script:
//Returns the php array to loop through
$test_list= $db->DatabaseRequest($testing);
//Loops through the $test_list array and retrieves a row for each value
foreach ($test_list as $id => $test) {
$getList = $db->getTest($test['id']);
$id_export[] = $getList ;
}
print(json_encode($id_export));
This returns a JSON value of:
[[{"id":1,"amount":2,"type":"0"}], [{"id":2,"amount":25,"type":"0"}]]
This is causing problems when I try to parse the data onto my android App. The result needs to be something like this:
[{"id":1,"amount":2,"type":"0"}, {"id":2,"amount":25,"type":"0"}]
I realize that the loop is adding the array into another array. My question is how can I loop through a php array and put or keep all of those values into an array and output them in the JSON format above?
of course I think $getList contains an array you database's columns,
use
$id_export[] = $getList[0]
Maybe can do some checks to verify if your $getList array is effectively 1 size
$db->getTest() seems to be returning an array of a single object, maybe more, which you are then adding to a new array. Try one of the following:
If there will only ever be one row, just get the 0 index (the simplest):
$id_export[] = $db->getTest($test['id'])[0];
Or get the current array item:
$getList = $db->getTest($test['id']);
$id_export[] = current($getList); //optionally reset()
If there may be more than one row, merge them (probably a better and safer idea regardless):
$getList = $db->getTest($test['id']);
$id_export = array_merge((array)$id_export, $getList);
I keep running into the error Warning: Invalid argument supplied for foreach() and for the life of me I can't figure out why. Here is my relevant code:
$Ids = $_POST["param-0"];
$toReturn = array();
$decodedJson = json_decode($Ids,TRUE);
stripslashes($decodedJson);
foreach($decodedJson as $id)
{
... do stuff with $toReturn...
}
$Ids is a string from a previous file that is encoded with json_encode. I added the stripslashes because it was recommended in another question on Stack Overflow, but it didn't help. If I change the beginning of the foreach loop to beforeach($toReturn as $id) the error goes away. Thanks!
edit: in the previous file, $_POST["param-0"] is an integer array that I returned with json_encode. With the testing data I am working with right now, ["15","18"] is what is being passed.
First you need to decode the json (which you already did)
$decodedJson = json_decode($Ids, True);
Then to grab each value from the json and, for example, echo it. Do this:
foreach ($decodedJson as $key => $jsons) { // This will search in the 2 jsons
foreach($jsons as $key => $value) {
echo $value; // This will show jsut the value f each key like "var1" will print 9
// And then goes print 16,16,8 ...
}
}
From top to botton:
$Ids = $_POST["param-0"];
This will trigger a notice if input data does not have the exact format you expect. You should test whether the key exists, for instance with isset().
$toReturn = array();
$decodedJson = json_decode($Ids,TRUE);
This will return null if input data is not valid JSON. You should verify it with e.g. is_null().
stripslashes($decodedJson);
If input data was valid we'll first get a warning:
Warning: stripslashes() expects parameter 1 to be string, array given
Then, if our PHP version is very old we'll have our array cast to a string with the word Array in it, and if our PHP version is recent we'll get null. Whatever, our data is gone.
If input data wasn't valid, we'll get an empty string.
foreach($decodedJson as $id)
{
... do stuff with $toReturn...
}
Neither null or strings (empty or not) are iterable. There's no nothing to do here. Our data is gone forever :_(
It ended up I was incorrectly encoding what I wanted decoded. All is well again, thanks for everyone's help!
I have a problem with this code:
$a['i'] = 1;
$b = '$a[\'i\']';
echo $$b;
It display an error:
Notice: Undefined variable: $a['i'] in test.php on line 6
Is it possible to create dynamic array variable?
Thanks for your time.
EDIT: In my example I am trying to edit an multidimensional array. There is a problem when I try to add data to my array (JSON). I don't have fixed dimension of array, it my be 2 or more dimension (I am building a model for Web form, and I want to add invalid value to JSON).
Now in one of the methods of Web form object I have code which checks repopulation object to add invalid value if needed.
I can not just add a value to JSON array, I need to edit it on multidimensional level.
For now I came up on solution to dynamically generate variable name and, then, edit it. If someone have solution it will be appreciated.
private $form = array(
'form_contact'=>array(
'attr'=>array('tag'=>'FORM', 'method'=>'post'),
'elem'=>array(
'fs_contact'=>array(
'attr'=>array('legend'=>'Kontakt', 'tag'=>'FSET'),
'elem'=>array(
'name'=>array(
'attr'=>array('SPAN'=>'Ime i prezime', 'title'=>'Unesite Vaše ime i prezime', 'tag'=>'INPUT', 'type'=>'text'),
'validat'=>array('req'=>'noscript', 'length'=>255),
'invalid'=>true), // Holds info that this is invalid
'www'=>array(
'attr'=>array('SPAN'=>'Web sajt', 'title'=>'Unesite Vaš sajt', 'tag'=>'INPUT', 'type'=>'text'),
'validat'=>array('length'=>255)),
'email'=>array(
'attr'=>array('SPAN'=>'E-mail', 'title'=>'Unesite Vaš email', 'tag'=>'INPUT', 'type'=>'text'),
'validat'=>array('req'=>'email', 'length'=>255)),
'message'=>array(
'attr'=>array('SPAN'=>'Poruka', 'cols'=>'60', 'rows'=>'5', 'title'=>'Unesite Vašu poruku', 'tag'=>'TEXTA', 'value'=>'nesto'),
'validat'=>array('req'=>'all')),
'submit_new_contact_form'=>array(
'attr'=>array('tag'=>'INPUT', 'type'=>'submit', 'value'=>'Pošalji poruku!'))
))// FS end
)) // Form end
);// Array end
You can't do it that way, as PHP thinks you're looking for a variable with the name $a['i'], rather than the 'i' key in the $a array.
The proper, and conventional, way is to use a dynamic key/index instead:
$b = 'i';
echo $a[$b];