从多维数组中提取数组

时间:2011-09-27 15:57:59

标签: php facebook-graph-api multidimensional-array

我有一个像这样的数组

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
    )

这高达566意味着这是我的脸书朋友的名字和ids            我想要做的是我想做facebook朋友的autocomlete 这对我来说很好,它显示了我想做的事情,但我还需要ids对名字

这是我的代码             

   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>";

输出高于数组

   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>";

这是输出

       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
   )

因为它的索引不是顺序所以我是diong所以让它从零开始

     $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>";

结果是

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

现在这里是一个大问题我想从$ friends_inner数组中提取所有用户ID,其名称等于我的$ final_result数组。我这样做但问题是它上升到最后一个索引即22而不是通过错误index 22和ahead是无效的偏移。如何从这个$ friends_inner中提取id,这是我的主阵列

   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>";

导致        排列        (         )

     $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

&GT;

1 个答案:

答案 0 :(得分:0)

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