I am trying to get a list of all the names & id of people actually going to an event I create. Getting the list using the php graph api for facebook was the easy part and seems to work.
Code (php) the get the data:
//GET ATTENDING
$getattending = "/" . $event_id . "/attending?fields=name,id";
$req_events = new FacebookRequest($session, 'GET', $getattending);
$req_response = $req_events->execute();
$data_array = $req_response->getGraphObject()->asArray();
$counter = array_map("count", $data_array);
$count = $counter['data'];
echo "Attending: $count<BR>";
echo "<PRE>";
print_r($data_array);
echo "</PRE>";
The result:
Array (
[data] => Array
(
[0] => stdClass Object
(
[name] => Thierry Martens
[id] => 788923242
)
[1] => stdClass Object
(
[name] => Lisa Mario Laurier
[id] => 708876902536391
)
[2] => stdClass Object
(
[name] => Ramy Mahfoudhi
[id] => 735036479911364
)
[3] => stdClass Object
(
[name] => Jeremy Verriest Duroisin
[id] => 783108468420824
)
[4] => stdClass Object
(
[name] => Jonas En Svetlana Laurier
[id] => 773139856081632
)
[5] => stdClass Object
(
[name] => Maxime Demerliere
[id] => 849400761761008
)
[6] => stdClass Object
(
[name] => Jeremy Beauchamp
[id] => 10204174155667109
)
[7] => stdClass Object
(
[name] => Sari Jens Delcourte Delusinne
[id] => 10204086515874904
)
[8] => stdClass Object
(
[name] => Pieter Marysse
[id] => 10204911283045115
)
[9] => stdClass Object
(
[name] => Patrick Vanden Bosschelle
[id] => 10202907209181148
)
)
BUT i am having problems to actually gather the data itsels; i simply need the name and the id in simple array or list so i can use it in the rest of the script. Any ideas Anyone?
My second question is the php graph api seems to have a "/eventnr/attending" thing for graph 2.1; showing nr attendants to your event in question; but when i actually call it i get an error stating i need to use graph 2.1 while i uploaded the latest php sdk and can't seem to find a way to change that version. This question is not as important as the one above; but if it works i would need less code :)
Hope you guys can help me :)
!!!! GOT IT !!!!
Did look some further on here and the solutions seems to be pretty easy:
for ($x = 0; $x <= $count; $x++)
{
$names[$x] = $data_array['data'][$x]->name;
$ids[$x] = $data_array['data'][$x]->id;
}
Displays:
$names array:
Array ( [0] => Thierry Martens [1] => Lisa Mario Laurier [2] => Ramy
Mahfoudhi [3] => Jeremy Verriest Duroisin [4] => Jonas En Svetlana
Laurier [5] => Maxime Demerliere [6] => Jeremy Beauchamp [7] => Sari
Jens Delcourte Delusinne [8] => Pieter Marysse [9] => Patrick Vanden
Bosschelle [10] => )
$ids array:
Array ( [0] => 788923242 [1] => 708876902536391 [2] => 735036479911364
[3] => 783108468420824 [4] => 773139856081632 [5] => 849400761761008
[6] => 10204174155667109 [7] => 10204086515874904 [8] =>
10204911283045115 [9] => 10202907209181148 [10] => )
It's like:
$dataArray = $data_array['data'];
$firstPerson = new $dataArray[0];
echo $firstPerson->name;
echo $firstPerson->id;
Maybe you need this, though:
foreach($data_array['data'] as $a){
$o = new $a; $names[] = $o->name; $ids[] = $o->id;
}
// $names is Array of names
// $ids in Array of ids
I added your code but result is the following (empty arrays)
added code:
foreach($data_array['data'] as $a)
{
$o = new $a; $names[] = $o->name; $ids[] = $o->id;
}
echo "FIRST ELEMENT - \$data_array['data'][0]: <BR>";
print_r($data_array['data'][0]); echo "<BR><BR>";
echo "\$names array: <BR>";
print_r($names); echo "<BR><BR>";
echo "\$ids array: <BR>";
print_r($ids); echo "<BR><BR>";
Result of the echo & print_r:
FIRST ELEMENT - $data_array['data'][0]:
stdClass Object ( [name] => Thierry Martens [id] => 788923242 )
$names array:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )
$ids array:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )
Related
I have an array which have string kind of object I can say
Array
(
[0] => Name=xyz
[1] => Email=xyz43#gmail.com
[2] => Password=xyz#123
[3] => Address=xyz University Lucknow
[4] => City=xyz
[5] => Name=peter
[6] => Email=peter43#gmail.com
[7] => Password=peter#123
[8] => Address=address
[9] => City=bla
[10] => Name=Jack
[11] => Email=jack76#gmail.com
[12] => Password=jack123
[13] => Address=jackAddress
[14] => City=jackCity
)
desired output
Array
(
[Email] => xyz43#gmail.com
[Email] => peter43#gmail.com
[Email] => jack76#gmail.com
[Password] =>xyz#123
[Password] =>peter#123
[Password] =>jack123
)
Let me confess that I want this stuff for login purpose thank you in advance .
You can try like this
$stringArray = ['Name=xyz','Email=xyz43#gmail.com','Password=xyz#123','Address=xyz University Lucknow','Name=xyz','Email=xyz413#gmail.com','Password=xyz1#123','Address=xyz University Lucknow'];
$loginCredentials = [];
$i = 0;
foreach($stringArray as $item){
$data = explode('=', $item);
if(in_array('Email',$data) || in_array('Password',$data)){
$loginCredentials[$i][$data[0]] = $data[1];
if(isset($loginCredentials[$i]['Email']) && isset($loginCredentials[$i]['Password'])){
$i++;
}
}
}
//Output desired array
print_r($loginCredentials);
Im trying to insert variables to the array by using foreach loop. This is the foreach loop im trying to implement with
foreach($rows as &$url) {
$link = array("url");
array_push($url, "hello World");
}
And this is the result I get.
Array
(
[0] => Array
(
[cat_id] => 1
[id] => RT
[name] => root
[parent] => 0
[0] => hello World
)
[1] => Array
(
[cat_id] => 2
[id] => CI
[name] => Civil & Interior Works
[parent] => 1
[0] => hello World
)
)
but i would like the result to be like this.
Array
(
[0] => Array
(
[cat_id] => 1
[id] => RT
[name] => root
[parent] => 0
[url] => hello World
)
[1] => Array
(
[cat_id] => 2
[id] => CI
[name] => Civil & Interior Works
[parent] => 1
[url] => hello World
)
)
If i pass the variable $link = array("url"); to array_push($link, "hello World"); nothing happens.
if I remove the referance from the foreach($rows as &$url) the loop does not work at all. Please advise.
Just add a value under the required key:
foreach($rows as &$row) {
$row['url'] = "hello World";
}
I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.
This is my array () ...not much English'm using google translator.
I'm printing this array with print_r (). but what I deceo is sort of form this down
Array
(
[0] => Array
(
[0] => 606125999550609
[1] => Patricia
[2] => Michelle
)
[1] => Array
(
[0] => 724417787635260
[1] => Nasshy
[2] => Green
)
[2] => Array
(
[0] => 1121064174618668
[1] => Luisanna
[2] => Rodriguez
)
[3] => Array
(
[0] => 1057585894278115
[1] => Libane
[2] => Heredia
)
)
Basically what I need is to sort this array as follows......
So I do not know how to sort follows in PHP...
Array
(
[0] => 606125999550609
[1] => 724417787635260
[2] => 1121064174618668
[3] => 1057585894278115
[4] => Patricia
[5] => Nasshy
[6] => Luisanna
[7] => Libane
[8] => Michelle
[9] => Green
[10] => Rodriguez
[11] => Heredia
)
This isn't so much "sorting", its' more of a manipulation/restructure. Using a loop to regenerate your array would be the option, but if you can modify the data from where it comes from, then that's always recommended.
$new = array();
array_map(function($obj) use(&$new) {
foreach($obj as $i => $elem) {
$new[$i][] = $elem;
}
}, $array);
In the example above, we're using array_map() to apply our function() {... that runs the loop of each element, applying it to our $new array.
All you need to do is pass your $array in as you see above.
Example/Demo
I have looked around and tried several things, converting it to array and trying to access the object directly.
I want to take out the page ID, name and access token.
Here is my code to get the object with the pages I look after:
$request = new FacebookRequest($session, 'GET', '/'.$userID .'/accounts');
$response = $request->execute();
$graphObject = $response->getGraphObject();
I get the following array using $array = (array) $graphObject;:
Array([*backingData] => Array(
[data] => Array(
[0] => stdClass Object(
[access_token] => CAAVOhgjThxcBAOZB61QxkxI1qdhZCngNZADQloUasFpTRCoZC7hYiDydjXQ9U0vQIjUMyZAeb6aGLKi7VE1btwk8eOPnsEzxyZPhJV1GVoujWlbb6PHo44yIi3D5VPSBRJJuNUOXobSgQNAevpSDZBgZDZD
[category] => Organization
[category_list] => Array(
[0] => stdClass Object(
[id] => 19850828628
[name] => Organization
)
)
[name] => Side Pot Cavan
[id] => 67433931
[perms] => Array(
[0] => ADMINISTER
[1] => EDIT_PROFILE
[2] => CREATE_CONTENT
[3] => MODERATE_CONTENT
[4] => CREATE_ADS
[5] => BASIC_ADMIN
)
)
[1] => stdClass Object(
[access_token] => CAAVOhgjThxcBAD7t6VU3HJAz9AovBJ7OiNzeFRBEQcHDEyHzRmi8oZBMnUyBQHxqNPeTXZBzWaOhCs2ivNWVvHwi5MGJpAuzgsP3d3cVq4LYZBsxcQQbmB5ufZAct250ZBhfENZCFCZAqAPaWvhGFpI5FzM38PwEIZCIZAtdtcvQZC0gZDZD
[category] => Cause
[name] => James Morgan
[id] => 2773587
[perms] => Array(
[0] => ADMINISTER
[1] => EDIT_PROFILE
[2] => CREATE_CONTENT
[3] => MODERATE_CONTENT
[4] => CREATE_ADS
[5] => BASIC_ADMIN
)
)
[2] => stdClass Object(
[access_token] => CAAVOhgjThxcBAIdnByW9TV7ZAxOnBS42nZAWDFEHeb3hPe5lTEMVwzBuZBMiPB99qOM1ks8VafDxLIZBbZCHe8tkDrtjRcPw8DUtpvnBUsNWvqKxt4y7oub10SgHqxRS194At5HBTLJ0K4vpo7qWaXZCIJwZDZD
[category] => Sports venue
[name] => Galway Poker Festival
[id] => 9751854
[perms] => Array(
[0] => ADMINISTER
[1] => EDIT_PROFILE
[2] => CREATE_CONTENT
[3] => MODERATE_CONTENT
[4] => CREATE_ADS
[5] => BASIC_ADMIN
)
)
I have edited the IDs and tokens here for public use.
I am sure I am just missing a next trick to get these values.
You can use the methods getProperty() and getPropertyAsArray() to get the values of a GraphObject. If you want to know what properties there are, use getPropertyNames().
Since this is an array of data (multiple pages the user is admin of), it might be easier to use it as a simple array, like you are trying to - there is a method called asArray() which does exactly this:
$graphObject = $response->getGraphObject();
$data = $graphObject->asArray();
Here's the reference for the Facebook GraphObject class: https://developers.facebook.com/docs/php/GraphObject/4.0.0
so i got a clue from the above answer and moved on:
my finished code was:
foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $invalue) {
$array = (array) $invalue;
foreach ($array as $key2 => $value2) {
if ($key2=='access_token') {
$c++;
$ats[$c]=$value2;
}
if ($key2=='name') {
$pagenames[$c]=$value2;
}
if ($key2=='id') {
$ids[$c]=$value2;
}
}
}
}
}
So i got a nice clean array from $data = $graphObject->asArray();
then proceeded.
There was another object inside the array so i typecast that to array then run foreach loops over it to get the keys i wanted.
Not the prettiest but it did the job.
thanks