Hey all i am trying to get all the likes for a wall post in PHP. The array frame looks like this:
Array (
[data] => Array (
[0] => Array (
[id] => XXXXXXXX_XXXXXXXXXXX
[from] => Array (
[name] => Bob Barker
[id] => XXXXXXXXXXX
)
[message] => This is a message here!!!
[story] => We shared a message with others
[story_tags] => Array (
[0] => Array (
[0] => Array (
[id] => XXXXXXXXXX
[name] => Bob Barker
[offset] => 0
[length] => 11
[type] => user
)
)
[19] => Array (
[0] => Array (
[id] => XXXXXXXXXXXXXXXXXXXXXXX
[name] => NAME
[offset] => 19
[length] => 5
[type] => page
)
)
)
[picture] => [removed]
[link] => [removed]
[name] => Timeline Photos
[caption] => This is just a caption for the post here
[properties] => Array (
[0] => Array (
[name] => By
[text] => NAME
[href] => [removed]
)
)
[icon] => https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif
[actions] => Array (
[0] => Array (
[name] => Like
[link] => [removed]
)
)
[privacy] => Array (
[value] =>
)
[type] => photo
[status_type] => shared_story
[object_id] => XXXXXXXXXXXXXXXX
[application] => Array (
[name] => Photos
[id] => XXXXXXXXXX
)
[created_time] => 2014-02-12T21:33:17+0000
[updated_time] => 2014-02-12T21:33:17+0000
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => XXXXXXXXXXXX
[name] => John Doe
)
[1] => Array (
[id] => XXXXXXXXXXXX
[name] => Steve Doe
)
)
[paging] => Array (
[cursors] => Array (
[after] => XXXXXXXXXXXXXXXX
[before] => XXXXXXXXXXXX==
)
)
)
)
My current PHP code is:
$feed = $facebook->api('/me/home'); //This is the array above
foreach($feed['data'] as $post) {
$story = (isset($post['story']) ? $post['story'] : null);
$id = (isset($post['id']) ? $post['id'] : null);
$name = (isset($post['name']) ? $post['name'] : null);
$message = (isset($post['message']) ? $post['message'] : null);
$post_link = (isset($post['actions'][0]['link']) ? $post['actions'][0]['link'] : null);
$picture = (isset($post['picture']) ? $post['picture'] : null);
foreach($post['likes'] as $liked) {
echo $post['id'];
}
echo $name . ' ' . $story . ' it was: ' . $message . ' and ' . $post_link . ' and <img src="' . $picture . '" /><br />';
echo '=======================================================================================================';
}
The
foreach($post['likes'] as $liked) {
echo $post['id'];
}
doesnt seem to be working as-is. What am i missing in order to make it loop through all the "liked" posts in the array?
Yes
foreach($post['likes'] as $liked) {
echo $post['id'];
}
will not work since
$post['likes'] elements are again a nested array as
likes => array => data => array=> ..
So you need to do the loop as
if(array_key_exists('data',$post['likes'])){
foreach($post['likes']['data'] as $liked=>$val) {
echo '<br />Like ID: ' .$val["id"].'<br />' ;
}
}
It is always better to do a check if the array key exists before doing the loop, so that if the returned data does not contain the key you do not get undefined index error.
Try
foreach($post['likes']->data as $like) {
echo $like->id;
}
Related
I have an Array of Strings, each String that exist in Advanced Custom Fields groups should be returned in foreach loop. Final results should be a single array of all values.
$lubuvna_groups = acf_get_field_groups();
$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
foreach($ArrayDiffs as $ArrayDiff) {
$resultsFileToImports[] = $ArrayDiff;
}
//$keysToImports = implode(", ",$resultsFileToImports);
$keysToImports = 'group_lubuvna_contact, group_lubuvna_subscriber';
foreach($resultsFileToImports as $resultsFileToImportsKey) {
$keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
return ( strpos($el['key'], $keysToImports) !== false );
});
}
The above code returns only if one string exists in $keysToImports. Once there are more than one value it doesn't work. I am sure i am missing something, but can't find any solution in here!
It shows me empty Array:
Array ( )
Maybe there is a different way how to get the arrays without strpos?
Final Array should looks like following:
Array ( [0] => Array ( [ID] => 0 [key] => group_lubuvna_contact [title] => ACF Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781382 [_valid] => 1 ) [1] => Array ( [ID] => 0 [key] => group_lubuvna_subscriber [title] => Lubuvna - Subscriber Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => post ) ) [1] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) [2] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => lubuvna_subscriber ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781369 [_valid] => 1 ) )
Try changing:
$keysToImports = 'group_lubuvna_contact', 'group_lubuvna_subscriber';
// then return
return ( strpos($el['key'], $keysToImports) !== false );
to
$keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
// then return
return in_array($el['key'], $keysToImports);
Full code update:
$lubuvna_groups = acf_get_field_groups();
$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
foreach($ArrayDiffs as $ArrayDiff) {
$resultsFileToImports[] = $ArrayDiff;
}
$keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
foreach($resultsFileToImports as $resultsFileToImportsKey) {
$keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
return in_array($el['key'], $keysToImports);
});
}
I have this array in a foreach-loop with the 'as' $train
Now i want to read out the 'mat'-node.
I've tried this:
<?php
echo "<!-- ";
foreach ($train['mat'] as $mat) {
echo "Mat:" . $mat . "";
}
echo " -->";
?>
But it gives a empty foreach result in mijn HTML-source, between the comments.
Array
(
[status] => 0
[via] => Utrecht C., Houten, Geldermalsen
[bestemming] => Tiel
[vleugels] => Array
(
[0] => Array
(
[stopstations] => Array
(
[0] => Array
(
[naam] => Vleuten
[code] => VTN
)
[1] => Array
(
[naam] => Utrecht Terwijde
[code] => UTT
)
[2] => Array
(
[naam] => Utrecht Leidsche Rijn
[code] => UTLR
)
)
[bestemming] => Tiel
[mat] => Array
(
[0] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2422
)
[1] => Array
(
[0] => SLT-4
[1] => Tiel
[2] => 2464
)
)
)
)
[vervoerder] => NS
[spoor] => 6
)
Hope this help.
But I think you need read more about PHP Array and Array index
echo "<!-- ";
foreach($train['vleugels'][0]['mat'] as $mat) {
echo "Mat:".$mat[2]."<br>";
}
echo " -->";
// <!-- 2422 2464 -->
I am success to import email contact with yahoo Oauth API. And I can see that all my contact email is listed in the page when I echo it. However, while it success to print the email, there is an error message there :
Trying to get property of non-object in globalses.php on line 295 AND
Undefined offset: 1 in globalses.php on line 295
This is the code who shows that error:
if (! empty($response)) {
list($info, $header, $body) = $response;
if ($body) {
//logit("callcontact:INFO:response:");
//print(json_pretty_print($body));
$yahoo_array = json_decode($body);
echo "<pre/>";
//print_r($yahoo_array);
foreach($yahoo_array as $key=>$values){
foreach($values->contact as $keys=>$values_sub){
// echo '<pre/>';
// print_r($values_sub);
// echo $values_sub->fields[1]->value->givenName;
$email = $values_sub->fields[1]->value; //This is line 295
if(trim($email)!="")
$newList .= $email.",";
}
}
}
$retarr = $newList."";
}
return $retarr;
[UPDATE] Th print result of $values->contact
Array
(
[0] => stdClass Object
(
[isConnection] =>
[id] => 50331977
[fields] => Array
(
[0] => stdClass Object
(
[id] => 50332026
[type] => email
[value] => academic_interlingua#cbn.net.id
[editedBy] => OWNER
[flags] => Array
(
)
[categories] => Array
(
)
)
)
[categories] => Array
(
)
[error] => 0
[restoredId] => 0
)
[1] => stdClass Object
(
[isConnection] =>
[id] => 41
[fields] => Array
(
[0] => stdClass Object
(
[id] => 63
[type] => email
[value] => access#sampoernafoundation.org
[editedBy] => OWNER
[flags] => Array
(
)
[categories] => Array
(
)
)
)
[categories] => Array
(
)
[error] => 0
[restoredId] => 0
)
[2] => stdClass Object
(
[isConnection] =>
[id] => 50331986
[fields] => Array
(
[0] => stdClass Object
(
[id] => 50332036
[type] => email
[value] => activeindonesia#yahoo.com
[editedBy] => OWNER
[flags] => Array
(
)
[categories] => Array
(
)
)
[1] => stdClass Object
(
[id] => 50332037
[type] => guid
[value] => APQMLKWC3QLQRAMYZQABSF63ZA
[editedBy] => OWNER
[flags] => Array
(
[0] => Y360
)
[isConnection] =>
[categories] => Array
(
)
)
)
[categories] => Array
(
)
[error] => 0
[restoredId] => 0
)
[UPDATE]
foreach($values->contact as $keys=>$values_sub){
if(property_exists($values_sub, 'value') && !is_array($values_sub->value))
//echo $values_sub->fields[1]->value->givenName;
$email = $values_sub->fields[1]->value;
if(trim($email)!="")
$newList .= $email.",";
[UPDATE] the result of var_dump
academic_interlingua#cbn.net.idstring(31) "academic_interlingua#cbn.net.id"
access#sampoernafoundation.orgstring(30) "access#sampoernafoundation.org"
APQMLKWC3QLQRAMYZQABSF63ZAstring(26) "APQMLKWC3QLQRAMYZQABSF63ZA"
activeindonesia#yahoo.comstring(25) "activeindonesia#yahoo.com"
ade.nugraha#bisnis.co.idstring(24) "ade.nugraha#bisnis.co.id"
IKN34TUEMHOJNOBUJQP5D2CBDQstring(26) "IKN34TUEMHOJNOBUJQP5D2CBDQ"
adebete#yahoo.comstring(17) "adebete#yahoo.com"
aditamiva.recruitment#gmail.comstring(31) "aditamiva.recruitment#gmail.com"
admin#goodlife.co.idstring(20) "admin#goodlife.co.id"
admin#klaudia.p.htstring(18) "admin#klaudia.p.ht"
admin#l-cq.comstring(14) "admin#l-cq.com"
admin#mujahidpress.comstring(22) "admin#mujahidpress.com"
agoes#kesaintblanc.co.idstring(24) "agoes#kesaintblanc.co.id"
agro.rekrutmen#agromediagroup.comstring(33) "agro.rekrutmen#agromediagroup.com"
You are accessing the email by its fields as:
$email = $values_sub->fields[1]->value; //This is line 295
When you see the first element of an array, there is no any fields with key 1, since it is only value in the array. $fields[0].
By seeing your code and the actual array, i come up with the following, this may not be the complete solution, but hope guides towards the result.
So this must be something like this:
foreach($values->contact as $keys=>$values_sub){
$fields = $values_sub->fields;
foreach($fields as $field){
if(property_exists($field,'value') && !is_array($field->value)){
echo (string) $field->value;
}
}
}
I have a facebook array and I am trying to echo out the names and ID.
All I have is this array: $friends_list.
When I use this code:
print_r($friends_list);
Then the following comes out below: But how do I loop thru these? Or what if I just wanted to find out how many names there are or just call on one var by id. In short how do I echo out $friends_list which is a multiD array?
Array ( [data] => Array ( [0] => Array ( [name] => Wendy Cukierski [id] => 524320383 ) [1] => Array ( [name] => Leticia Mercado Correa [id] => 537763225 ) [2] => Array ( [name] => Olivia Urbina [id] => 538711855 ) [3] => Array ( [name] => Ruth Albrecht [id] => 541610111 ) [4] => Array ( [name] => Josh Brahm [id] => 577546485 ) [5] => Array ( [name] => Kim Yu [id] => 583515871 ) [6] => Array ( [name] => SisterTracey Dugas [id] => 643567171 ) [97] => Array ( [name] => Mary Martinez [id] => 100004696266210 ) ) [paging] => Array ( [next] => https://graph.facebook.com/1566027944/friends?limit=5000&offset=5000&__after_id=100004696266210 ) )
DUPLICATE --> How to store a Facebook user's friends list in MySQL using PHP?
$friends_list = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $atoken );
$friends_list_array = json_decode($friends_list,true);
$arr= $friends_list_array['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$friend_ids = implode("," , $friend_ids_arr);
echo $friend_ids; // output: id1,id2,id3...etc
How do I get data out of SimpleDB? All I wanted to do was put data in and then get data out. It looks like the data is not easy to get out and in the end will require computing power to extract by looping etc. Am I correct?
Here is my code to extract the data:
<?php
// Include the SDK
require_once 'sdk.class.php';
// Instantiate
$sdb = new AmazonSDB();
$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;
do {
if ($next_token) {
$response = $sdb->select($select_expression, array(
'NextToken' => $next_token,
));
} else {
$response = $sdb->select($select_expression);
}
// Get Data for row
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
$next_token = isset($response->body->SelectResult->NextToken)
? (string) $response->body->SelectResult->NextToken
: null;
}
while ($next_token);
echo "<br>";
?>
Here is the extract of the data I'm trying to extract.
Array
(
[#attributes] => Array
(
[ns] => http://sdb.amazonaws.com/doc/2009-04-15/
)
[SelectResult] => Array
(
[Item] => Array
(
[0] => Array
(
[Name] => 1
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => +2782555555
)
[1] => Array
(
[Name] => msgType
[Value] => S
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test SMS message for ID no 1
)
)
)
[1] => Array
(
[Name] => 2
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => +27825555555
)
[1] => Array
(
[Name] => msgType
[Value] => P
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test phone message for ID no 2
)
)
)
[2] => Array
(
[Name] => 3
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => name#domain.co.za
)
[1] => Array
(
[Name] => msgType
[Value] => E
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test email message for ID no 3 to name#domain.co.za
)
)
)
[3] => Array
(
[Name] => 4
[Attribute] => Array
(
[0] => Array
(
[Name] => msgAddress
[Value] => andrebruton
)
[1] => Array
(
[Name] => msgType
[Value] => P
)
[2] => Array
(
[Name] => msgSubmitDate
[Value] => 2012-09-02 15:48:46
)
[3] => Array
(
[Name] => msgText
[Value] => Test push notification message for ID no 4
)
)
)
)
)
)
All I want to get is the following variables:
msgId (the Name), msgType, msgAddress, msgText, msgSubmitDate
If I can get it using $msgType = Body->SelectResult->Item->Name or something like that.
Using XPath is going to perform about 100x faster. I haven't tested this code, but the XPath expression should be pretty close:
$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();
I figured it out using some old Tarzan code. This works for a 2 dimensional array (data similar to a standard database)
Here is the working code:
<?php
// Include the SDK
require_once 'sdk.class.php';
// Instantiate
$sdb = new AmazonSDB();
$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;
do {
if ($next_token) {
$response = $sdb->select($select_expression, array(
'NextToken' => $next_token,
));
} else {
$response = $sdb->select($select_expression);
}
// Get Data for row
foreach ($response->body->SelectResult->Item as $item)
{
$msgId = $item->Name;
foreach ($item->Attribute as $attr)
{
if ($attr->Name == 'msgAddress') {
$msgAddress = $attr->Value;
}
if ($attr->Name == 'msgType') {
$msgType = $attr->Value;
}
if ($attr->Name == 'msgSubmitDate') {
$msgSubmitDate = $attr->Value;
}
if ($attr->Name == 'msgText') {
$msgText = $attr->Value;
}
}
echo "<br>msgId: $msgId<br>";
echo "msgAddress: $msgAddress<br>";
echo "msgType: $msgType<br>";
echo "msgSubmitDate: $msgSubmitDate<br>";
echo "msgText: $msgText<br><br>";
}
$next_token = isset($response->body->SelectResult->NextToken)
? (string) $response->body->SelectResult->NextToken
: null;
}
while ($next_token);
echo "<br>";
?>
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
//but $msgId is not set