i have an array which is like this
Array
(
[0] => Array
(
[name] => Amanda Coleman
[id] => 98764676778
)
[1] => Array
(
[name] => Hashimi Sultana
[id] => 876304848
)
[2] => Array
(
[name] => Maren Shawesh
[id] => 988363747
)
[3] => Array
(
[name] => Ajo Khan
[id] => 039494857587
)
[4] => Array
(
[name] => Negar Jan
[id] => 948437484748
)
[5] => Array
(
[name] => Mehran Khan
[id] => 3948474947
)
[6] => Array
(
[name] => Sal Man
[id] => 039383734647
)
this is upto 566 mean this is my facebook friends name and ids
what i am trying to do is i want to make autocomlete of facebook friends
which work fine for me its showing what i want to do but i need the ids also against name
here is my code
if(isset($_REQUEST['queryString'])) {
lets $var = fa
$var = ucfirst ($_REQUEST['queryString']); //making first character uppercase for matching with fb name
$friends_inner=$_SESSION['friends'];
echo "<pre>";
print_r($friends_inner);
echo "</pre>";
the output is above array
for($i=0; $i<sizeof($friends_inner); $i++)
{
$selected_friend=$friends_inner[$i];
$selected_friend_id=$selected_friend['id']; //array of ids
$selected_friend_name=$selected_friend['name']; // array of names
$name[$i]=$selected_friend_name;
}
$result=preg_grep('/^'.$var.'.*/', $name);//returns array matching with requested alphabet from textbox which work fine dispalaying names
echo "<pre>";
print_r($result);
echo "</pre>";
here is the output
Array
(
[17] => Fazal Muhammad
[18] => Fawad Ahmad
[39] => Fayaz Zafar
[42] => Farhan Bogra
[66] => Farzana KArim Elora
[81] => Fahim Khan
[92] => Fahad Shams
[119] => Fazal Yazdan
[166] => Fakhar Alam
[173] => Faheem Ur Rahman
[183] => Fawaid Khan
[187] => Faizan Sabir
[258] => Fayaz Ali
[269] => Faizan Khattak
[308] => Faridullah Khan
[411] => Fawad Qamar
[446] => Fahad Khan
[458] => Fahad Khan
[507] => Faisl Qureshi
[529] => Faisal Khan
[538] => Faiza Baloch
[555] => Fawad Khan
)
as it its index is not sequentially so i am diong so to make it start from zero
$final_result=array();
$maxindex = max(array_keys($result));
for($i=0;$i<=$maxindex;$i++){ //returns array that start with zero
if(isset($result[$i])){
array_push($final_result,$result[$i]);
}
}
echo "<pre>";
print_r($final_result);
echo "</pre>";
the result is
Array
(
[0] => Fazal Muhammad
[1] => Fawad Ahmad
[2] => Fayaz Zafar
[3] => Farhan Bogra
[4] => Farzana KArim Elora
[5] => Fahim Khan
[6] => Fahad Shams
[7] => Fazal Yazdan
[8] => Fakhar Alam
[9] => Faheem Ur Rahman
[10] => Fawaid Khan
[11] => Faizan Sabir
[12] => Fayaz Ali
[13] => Faizan Khattak
[14] => Faridullah Khan
[15] => Fawad Qamar
[16] => Fahad Khan
[17] => Fahad Khan
[18] => Faisl Qureshi
[19] => Faisal Khan
[20] => Faiza Baloch
[21] => Fawad Khan
)
$ids_of_result=array();
now here is the big problem i want to extract all user ids from $friends_inner array which name equal to my $final_result array.i am doing so but the problem is that it goes upto last index i-e 22 and than through error that index 22 and ahead is not valid offset.how can i extract the ids from this $friends_inner which is my main array
for($j=0; $j<sizeof($friends_inner); $j++){
$selected_friend=$friends_inner[$j];
if($final_result[$j] == $selected_friend['name']){
echo $selected_friend['name'];
echo $selected_friend['id'];
array_push($ids_of_result,$selected_friend['id']);
//returns array of ids against result
}
}
echo "<pre>";
print_r($ids_of_result);
echo "</pre>";
it result in
Array
(
)
$counter=0;
foreach($final_result as $key=>$value){
echo '<li onClick="fill(\''.$ids_of_result[$counter].'\');">'.$value.'</li>';
$counter++;
}
}
please help me my code work fine in sense of autocomplete but i want the ids too.Graph api does not provide the id from name.if so i would not write much code.any help me thanx in advance
?>
function sort_arrays($final_result,$friends_inner) {
$ids_of_result = array();
foreach($final_result as $fnl_rslt){
foreach($friends_inner as $frnd_in){
if($fnl_rslt == $frnd_in['name']){
//echo 'Name : '.$frnd_in['name'].'<br>';
//echo 'Id : '.$frnd_in['id'].'<br>';
array_push($ids_of_result,$frnd_in['id']);
}
}
}
return $ids_of_result;
}
$ids_of_result = sort_arrays($final_result,$friends_inner);
Related
the text below takes google sheet json output, turns it into php array and then filters just by football club. the outcome is a drop down list of football clubs, sorted in random order. i am having trouble sorting them by alphabetical order.
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
//create array to store
$clubs=array();
//loop for each into array
foreach ($data['feed']['entry'] as $item)
{
$clubs[]= $item['gsx$clubs']['$t'];
}
//take array and get unique clubs
$clubs =array_unique($clubs);
//start select html
echo '<select>';
//print out unique clubs in option dropdown
foreach ($clubs as $key => $club) {
echo '<option>' . $club . '</option>' ;
}
//finish select html
echo '</select>';
when i print out print_r($clubs); it comes out like this
Array ( [0] => AFC Bournemouth [1] => Chelsea [2] => Wolverhampton Wanderers [3] => Crystal Palace [4] => Burnley [5] => Brighton & Hove Albion [6] => Tottenham Hotspur [8] => West Ham United [11] => Everton [12] => Manchester City [14] => Aston Villa [16] => Arsenal [20] => Manchester United [21] => Watford [23] => Sheffield United [28] => Southampton [30] => Newcastle United [39] => Norwich City [51] => Liverpool [71] => Leicester City )
so far i have already tried sort($clubs) and it doesnt seem to work
php code:
$asd = array(
"AFC Bournemouth",
"Chelsea",
"Wolverhampton Wanderers",
"Crystal Palace",
"Burnley",
"Brighton & Hove Albion",
"Tottenham Hotspur"
// ...
);
echo "<pre>";
print_r($asd);
echo "</pre>";
sort($asd);
echo "<pre>";
print_r($asd);
echo "</pre>";
output:
Array
(
[0] => AFC Bournemouth
[1] => Chelsea
[2] => Wolverhampton Wanderers
[3] => Crystal Palace
[4] => Burnley
[5] => Brighton & Hove Albion
[6] => Tottenham Hotspur
)
Array
(
[0] => AFC Bournemouth
[1] => Brighton & Hove Albion
[2] => Burnley
[3] => Chelsea
[4] => Crystal Palace
[5] => Tottenham Hotspur
[6] => Wolverhampton Wanderers
)
no problem, sorted alphabetically.
write what you want to receive in array?
There are some sorting functions for PHP arrays already. I think sort() is what you want. Have to tried it? https://www.w3schools.com/php/php_arrays_sort.asp
Usage:
<?php
$array = ["Newcastle United", "AFC Bournemouth", "Wolverhampton Wanderers", "Chelsea"];
sort($array);
print_r($array);
?>
Output
Array (
[0] => AFC Bournemouth
[1] => Chelsea
[2] => Newcastle United
[3] => Wolverhampton Wanderers
)
I am trying to figure a way to get this to work. But I have a hard time thinking out the logics.
I have this array:
Array
(
[0] => Array
(
[0] => news
[1] => {section}
[2] => {slug}
[3] => {*}
)
[1] => Array
(
[0] => {id}
[1] => {*}
)
[2] => Array
(
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
That I need to convert to this result:
0 news/{id}/{date}
1 news/{id}/25-07-1982
2 news/{id}/{section}
3 news/{id}/{slug}
4 news/{id}/{*}
5 news/{*}/{date}
6 news/{*}/25-07-1982
7 news/{*}/{section}
8 news/{*}/{slug}
9 news/{*}/{*}
10 {section}/{id}/{date}
11 {section}/{id}/25-07-1982
12 {section}/{id}/{section}
13 {section}/{id}/{slug}
14 {section}/{id}/{*}
15 {section}/{*}/{date}
16 {section}/{*}/25-07-1982
17 {section}/{*}/{section}
18 {section}/{*}/{slug}
19 {section}/{*}/{*}
20 {slug}/{id}/{date}
21 {slug}/{id}/25-07-1982
22 {slug}/{id}/{section}
23 {slug}/{id}/{slug}
24 {slug}/{id}/{*}
25 {slug}/{*}/{date}
26 {slug}/{*}/25-07-1982
27 {slug}/{*}/{section}
28 {slug}/{*}/{slug}
29 {slug}/{*}/{*}
30 {*}/{id}/{date}
31 {*}/{id}/25-07-1982
32 {*}/{id}/{section}
33 {*}/{id}/{slug}
34 {*}/{id}/{*}
35 {*}/{*}/{date}
36 {*}/{*}/25-07-1982
37 {*}/{*}/{section}
38 {*}/{*}/{slug}
39 {*}/{*}/{*}
The input array could contain more than three keys, so the solution I'm looking for should be dynamic. And the result should have the same order as the result shown above.
Does someone know how to do this in a efficient way? Can someone give me a push in the right direction? Thanks a lot! :)
Sth like this
foreach ($array[0] as $val0 )
foreach ($array[1] as $val1 )
foreach ($array[2] as $val2 )
$newArray[] = "$val0/$val1/$val2";
EDIT: for variable array length
function recursive($array , $length = 0){
$retval =array();
if($length < count($array) -1){
foreach ($array[$length] as $val0 )
foreach (recursive($array, $length+1) as $val1)
$retval[] = "$val0/$val1";
}
else
{
foreach ($array[$length] as $val0 )
$retval[] = "$val0";
}
return $retval;
}
print_r(recursive($array));
Just because I like writing functions that mis/manage PHP arrays, I put this together, mainly because I was pretty sure you could avoid recursion — because the structure itself isn't recursive. (My head seems to think that is a rule, I'm sure someone somewhere can prove it wrong).
foreach ( array_reverse($array) as $sub ) {
if ( isset($rem) ) {
$ret = array();
foreach ( $sub as $itm ) {
foreach ( $rem as $val ) { $ret[] = "$itm/$val"; }
}
$rem = $ret;
}
else {
$rem = $sub;
}
}
The output found in $rem is as follows:
Array (
[0] => news/{id}/{date}
[1] => news/{id}/25-07-1982
[2] => news/{id}/{section}
[3] => news/{id}/{slug}
[4] => news/{id}/{*}
[5] => news/{*}/{date}
[6] => news/{*}/25-07-1982
[7] => news/{*}/{section}
[8] => news/{*}/{slug}
[9] => news/{*}/{*}
[10] => {section}/{id}/{date}
[11] => {section}/{id}/25-07-1982
[12] => {section}/{id}/{section}
[13] => {section}/{id}/{slug}
[14] => {section}/{id}/{*}
[15] => {section}/{*}/{date}
[16] => {section}/{*}/25-07-1982
[17] => {section}/{*}/{section}
[18] => {section}/{*}/{slug}
[19] => {section}/{*}/{*}
[20] => {slug}/{id}/{date}
[21] => {slug}/{id}/25-07-1982
[22] => {slug}/{id}/{section}
[23] => {slug}/{id}/{slug}
[24] => {slug}/{id}/{*}
[25] => {slug}/{*}/{date}
[26] => {slug}/{*}/25-07-1982
[27] => {slug}/{*}/{section}
[28] => {slug}/{*}/{slug}
[29] => {slug}/{*}/{*}
[30] => {*}/{id}/{date}
[31] => {*}/{id}/25-07-1982
[32] => {*}/{id}/{section}
[33] => {*}/{id}/{slug}
[34] => {*}/{id}/{*}
[35] => {*}/{*}/{date}
[36] => {*}/{*}/25-07-1982
[37] => {*}/{*}/{section}
[38] => {*}/{*}/{slug}
[39] => {*}/{*}/{*}
)
Also, for those that like their arrays multidimensional, this might come in handy (although I'd hate to think what the overheads are for such a code golfed version). Just to be clear, this second example doesn't create the string list as requested by the OP, but a hierarchical array structure instead.
foreach ( array_reverse($array) as $sub ) {
$rem = isset($rem)
? array_combine($sub, array_fill(0, count($sub), $rem))
: $sub
;
}
This generates (again in $rem):
Array (
[news] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
[{*}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
[{section}] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
... and so on
Now if only PHP had a join_recursive that included keys.
(it would be almost pointless, save for helping with the above).
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] => )
I have a hierarchical data that I put in a array that I call $dt. Than I have a array that store the relationship between these data that I call $in. I have create a function that has as parameter, the initial index, level, array $dt an the array $in. I was debugging the function but I am not find why the subclass is lost during the process. The complete code is:
$dt = array(
41=>array( "pk"=>41,"parentPk"=>30,"name"=>"car1"),
15=>array("pk"=>15,"parentPk"=>11,"name"=>"food" ),
70=>array("pk"=>70,"parentPk"=>30,"name"=>"car3" ),
18=>array("pk"=>18,"parentPk"=>15,"name"=>"food1" ),
49=>array("pk"=>49,"parentPk"=>30,"name"=>"car2" ),
20=>array( "pk"=>20,"parentPk"=>15,"name"=>"food2"),
30=>array("pk"=>30,"parentPk"=>11,"name"=>"car" )
);
echo "<pre>";
print_r($dt);
echo "</pre>";
$in=array(11=>array(15,30),15=>array(18,20),30=>array(41,49,70));
echo "<pre>";
print_r($in);
echo "</pre>";
function fn_tree($parent_id, $level,$dt,$in) {
if(is_null($parent_id)){
$parent_id ="NULL";
}
if (isset($in[$parent_id])) {
foreach ($in[$parent_id] as $id) {
$pk=$in[$parent_id];
$arrEnd[$id]=str_repeat("-", $level*2) .$dt[$id]["name"];
fn_tree($id, $level + 1,$dt,$in,$arrEnd);
}
}
return $arrEnd;
}
$arrEcho = fn_tree(11, 0, $dt, $in);
echo "<br>";
echo "result";
echo "<br>";
echo "<pre>";
print_r($arrEcho);
echo "</pre>";
$desired = array(
15=>"food",
18=>"food1",
20=>"food2",
30=>"car",
41=>"car1",
49=>"car2",
70=>"car3"
);
echo "<br>";
echo "desired";
echo "<br>";
echo "<pre>";
print_r($desired);
echo "</pre>";
So why the function is losting the sub-classes?
Output is:
Array data
(
[41] => Array
(
[pk] => 41
[parentPk] => 30
[name] => car1
)
[15] => Array
(
[pk] => 15
[parentPk] => 11
[name] => food
)
[70] => Array
(
[pk] => 70
[parentPk] => 30
[name] => car3
)
[18] => Array
(
[pk] => 18
[parentPk] => 15
[name] => food1
)
[49] => Array
(
[pk] => 49
[parentPk] => 30
[name] => car2
)
[20] => Array
(
[pk] => 20
[parentPk] => 15
[name] => food2
)
[30] => Array
(
[pk] => 30
[parentPk] => 11
[name] => car
)
)
Array relationship
(
[11] => Array
(
[0] => 15
[1] => 30
)
[15] => Array
(
[0] => 18
[1] => 20
)
[30] => Array
(
[0] => 41
[1] => 49
[2] => 70
)
)
result
Array
(
[15] => food
[30] => car
)
desired
Array
(
[15] => food
[18] => food1
[20] => food2
[30] => car
[41] => car1
[49] => car2
[70] => car3
)
The clue to your problem is/should have been "Undefined variable: arrEnd." If you aren't running in an environment where you can see this warning, you should try to set up such an environment. If you are running in such an environment, you should play close attention to this and all other warnings.
I'm not sure if it is the convention here to just give a clue or a whole answer, but proceeding on to give a fairly complete answer: it seems that you intended to pass arrEnd in by reference, but did not pass it in at all.
Also, I'm a little confused by your desired output. Don't you in fact desire something with double dashes showing hierarchy, as below?
Array
(
[15] => food
[18] => --food1
[20] => --food2
[30] => car
[41] => --car1
[49] => --car2
[70] => --car3
)
This is what my array looks like :
Array (
[0] => SimpleXMLElement Object (
[key] => Array (
[0] => Track ID
[1] => Name
[2] => Artist
[3] => Album Artist
[4] => Composer
[5] => Album
[6] => Genre
[7] => Kind
[8] => Size
[9] => Total Time
[10] => Disc Number
[11] => Disc Count
[12] => Track Number
[13] => Year
[14] => Date Modified
[15] => Date Added
[16] => Bit Rate
[17] => Sample Rate
[18] => Play Count
[19] => Play Date
[20] => Play Date UTC
[21] => Artwork Count
[22] => Persistent ID
[23] => Track Type
[24] => Location
[25] => File Folder Count
[26] => Library Folder Count )
[integer] => Array (
[0] => 2056
[1] => 3732918
[2] => 230661
[3] => 1
[4] => 1
[5] => 1
[6] => 1993
[7] => 128
[8] => 44100
[9] => 3
[10] => 3439412487
[11] => 1
[12] => 5
[13] => 1 )
[string] => Array (
[0] => Eye of the Tiger
[1] => Survivor
[2] => Survivor
[3] => Frankie Sullivan/Jim Peterik
[4] => Greatest Hits
[5] => Rock
[6] => MPEG audio file
[7] => 772F0F53F195E705
[8] => File
[9] => file://localhost/Users/cameron/Music/iTunes/iTunes%20Media/Music/Survivor/Greatest%20Hits/01%20Eye%20of%20the%20Tiger.mp3 )
[date] => Array (
[0] => 2012-08-27T17:01:00Z
[1] => 2012-08-27T17:01:03Z
[2] => 2012-12-27T07:21:27Z ) )
that's 1 result, there is about 50 of them repeated.
I am trying to select the artist value in this case : Frankie Sullivan/Jim Peterik
please note: there is about 50 other results that come after this first one, so I would like to do a foreach to display all results.
any suggestions? I am stuck.this is the code I used to get these results:
$xml = simplexml_load_file('Library.xml');
$xmlx = $xml->dict->dict->dict->key;
$artist = $xmlx->xpath("//dict[key='Artist']");
echo "<pre>" . print_r($artist, true) . "</pre>";
It seems that you have an array of SimpleXMLElement,
so you can iterate over your array and use the SimpleXMLElement facilities.
Try:
foreach($yourArray as $simpleXmlElement)
{
// Retrieve the name
echo $simpleXmlElement->string[3];
}
Take a look at the documentation for more question: SimpleXMLElement
For your specific problem, assuming the key and string are synchronized, you can try:
// Loop on your array of SimpleXMLElement
foreach($yourArray as $simpleXmlElement)
{
// Initialize the position and found value
$position = 0;
$found = false;
// Loop on the sub array 'key' and search the 'Artist Album' position
foreach($simpleXmlElement->key as $index => $value)
{
if ($value == "Album Artist")
{
$found = true;
break;
}
// Not found, increment position
$position++;
}
// Retrieve the name if found the artist album key
if ($found)
echo $simpleXmlElement->string[$position];
}
As
[3] => Album Artist
will give the position 3
Then, when retrieving the string value, it will return Frankie Sullivan/Jim Peterik