开发者

extract array from multidimensional array

开发者 https://www.devze.com 2023-04-08 08:27 出处:网络
i have an array which is like this Array ( [0] => Array ( [name] => Amanda Coleman [id] => 98764676778

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);      
0

精彩评论

暂无评论...
验证码 换一张
取 消