Getting data with Facebook Graph Api - php

I am trying to get all the facebook group members ids with facebook php sdk, I am getting an array, and can't get out of the array with only ids.
Here is my array:
Array
(
[data] => Array
(
[0] => stdClass Object
(
[name] => Name Surname
[administrator] =>
[id] => 655330041265756
)
[1] => stdClass Object
(
[name] => Name Surname2
[administrator] =>
[id] => 10206563909924840
)
[2] => stdClass Object
(
[name] => Name Surname3
[administrator] =>
[id] => 1035931373098451
)
I am trying to export only [id], how is this possible?

Just a simple foreach can be used to iterate through the array and extract the id from each object.
foreach ($array as $key=>$value) {
echo $value->id;
}
Edit: If you want to assign the ids to an array, use the following code:
foreach ($array as $key=>$value) {
$idArray[] = $value->id;
}
Where $idArray is your array of ids.

Related

How to delete the value inside the array in PHP

I want to delete the ['Phone'] inside my array.
i tried foreach and unset but only the first array delete the ['Phone'].
my example array below.
Array
(
[0] => Array
(
[Name] => ads
[Phone] => 32132
)
[1] => Array
(
[Name] => ads
[Phone] => 321322
)
[2] => Array
(
[Name] => ads
[Phone] => 3213222
)
)
and my expected array.
Array
(
[0] => Array
(
[Name] => ads
)
[1] => Array
(
[Name] => ads
)
[2] => Array
(
[Name] => ads
)
)
You can use array_walk
array_walk($arr, function(&$v, $k){
unset($v['Phone']);
});
If all you want is to fetch the names, you can just pluck those using array_column().
$array = array_column($array, "Name");
Live demo at https://3v4l.org/VJTli
You can use map() function of Laravel Collection. and alter your original array however you want.
$newArray = collect($oldArray)->map(function($element) {
return [
'name' => $element['name'];
];
})->toArray();
Qirel's answer is faster I would imagine if you only have "name" and "phone". Another alternative that is easy on the eyes is:
foreach ($array as &$val) {
unset($val["Phone"]);
}

echo array value return form facebook api php

i am trying to display array values return from facebook api. array is stored in $result below is my array
Array
(
[data] => Array
(
[0] => Array
(
[name] => Ravi Shankar
[id] => 10206576743272319
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.5/1705944052976370/friends?limit=25&offset=25&__after_id=enc_AdBV9ZBlJwwdjBL8iWeIAZBSxDqJO0gvQWS45qwBBg1X8tCbZAoj9Cz506ZCGuDnddOL07MZD
)
[summary] => Array
(
[total_count] => 4628
)
)
i tried using foreach but confused
foreach($result as $key=>$value){
echo $key."-".$value."<br />";
}
i want to display the result like below:
name id
xxxx 123456
yyyy 173453
Write your foreach loop as below:-
foreach($result['data'] as $key=>$value){
echo $value['name'].'-'.$value['id']."<br>";
}
Hope it will help you :)
You need to use foreach in the $result["data"]:
foreach ($result["data"] as $friend) {
echo "{$friend["id"]} => {$friend["name"]}";
}
The each $friend variable is an array, of this format:
Array
(
[name] => Ravi Shankar
[id] => 10206576743272319
)

Replace array keys and values

For example I have this array:
Array (
[0] => Array (
[id] => 45 [name] => Name1 [message] => Ololo [date_create] => 21:03:56 )
[1] => Array (
[id] => 46 [name] => visitor [message] => Hi! [date_create] => 21:06:28 )
)
I need converting to:
Array (
[id] => Array (
[0] => 45, [1] => 46
)
[name] => Array (
[0] => Name1, [1] => visitor
)
[message] => Array (
[0] => Ololo, [1] => Hi!
)
[date_create] => Array (
[0] => 21:03:56, [1] => 21:06:28
)
)
I like to know a function for converting this,
Try this block of code:
// Assuming the array you have is called $mainArray.
// The output will be $outputArray.
$outputArray = array();
foreach ($mainArray as $index => $array) { // Iterate through all the arrays inside the main array.
// foreach ($mainArray as $array) { // Use this if the numeric index order doesn't matter.
foreach ($array as $key => $value) { // Iterate through each inner array.
// Load the multidimensional array with the first key as one of (id, name, message, date_create) and second key as the numeric index (if you need it).
$outputArray[$key][$index] = $value;
// $outputArray[$key][] = $value; // Use this if the numeric index order doesn't matter.
}
}
print_r($outputArray);

What is an efficient way to split a multidimensional array into a set of arrays based on a value?

I have a multidimensional array like the following:
Array (
[0] => stdClass Object (
[name] => StackOverflow
[image] => CanHelp.jpg
)
[1] => stdClass Object (
[name] => AnotherObject
[image] => SecondImage.jpg
)
)
How can I arrange/split this array into groups based on the first letter of [name]?
i.e. There are about 1,000 items in this array, which I have already ordered alphabetically by [name], however I want to be able to have groups that begin with 'A', 'B', etc.
Like this, for 'A' and 'S':
Array (
[0] => stdClass Object (
[name] => AnotherObject
[image] => SecondImage.jpg
)
[1] => stdClass Object (
[name] => AndAnother
[image] => notImportant.jpg
)
)
Array (
[0] => stdClass Object (
[name] => StackOverflow
[image] => CanHelp.jpg
)
)
$split = array();
foreach ($array as $item) {
$split[$item->name[0]][] = $item;
}

how to extract the exact data from an array in php

i got this array from an database...how to get the exact value attribute......
Array
(
[0] => TRowResult Object
(
[row] => tests
[columns] => Array
(
[a21ha:link_0] => TCell Object
(
[value] =>testsample
[timestamp] => 1265010584060
)
[a21ha:link_1] => TCell Object
(
[value] => example
[timestamp] => 1265092697040
)
)
)
how to get the [value] alone in php
print $myArray[0]->columns['a21ha:link_0']->value;
This would give you the first. To get all, you'd have to loop through the contents.
foreach ($myArray[0]->columns as $column) {
print $column->value;
}
Supposed your array called $array:
foreach($array as $arridx => $rowobj){
foreach($rowobj->columns as $cellid => $rowcell){
echo $rowcell->value;
}
}

Categories