从多维数组中提取数组



我有一个像这样的数组

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表示这是我的facebook好友的名字和id我想做的是我想让facebook的朋友自动完成这对我来说很好它显示了我想要做的事情但我还需要针对name

的id

是我的代码

   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
   )

因为它的索引不是顺序的所以我要让它从0

开始
     $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数组。我正在这样做,但问题是,它上升到最后一个索引I - e22,然后通过错误,索引22和前面是无效的偏移。我如何从这个$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

?>

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

相关内容

  • 没有找到相关文章

最新更新