drpogkqqi536984960 2011-06-12 18:35
浏览 9
已采纳

array_unique中的问题

I am newer studing php. I have asked my question in php facebook people search how to remove repeat value?

but still have some problem want to understand well.

here is the data example.

https://graph.facebook.com/search?q=tom&type=user&access_token=2227470867|2.AQD2FG3bzBMEiDV3.3600.1307905200.0-100001799728875|LowLfLcqSZ9YKujFEpIrlFNVZPQ

I want clear the repeat values. here is the answer form Tomalak Geret'kal, thanks him. It can clear the repeat name.

$names = Array();

foreach ($status_list['data'] as $data) {
   $names[] = $data['name'];
}

$names = array_unique($names);  // not print the same name.

foreach ($names as $name) {
   echo $name;
}

for I still want to echo $id. I tired 2 method here:

$names = Array();

foreach ($status_list['data'] as $data) {
   $names[] = $data['name'];
   $id[] = $data['id'];
}

$names = array_unique($names);  // not print the same name.

foreach ($names as $name) {
   echo $name;
   echo $id; // no result
}



$names = Array();

foreach ($status_list['data'] as $data) {
   $names[] = $data['name'];
}

$names = array_unique($names);  // not print the same name.

foreach ($names as $name) {
   echo $name;
   echo $data['id']; // all the id is the last people. 
}

How to do the right method? Thanks.

  • 写回答

3条回答 默认 最新

  • dsfgds4215 2011-06-12 18:40
    关注

    Try this:

    $data = array_unique($status_list['data']);
    
    foreach ($data as $i) {
        echo $i['name'];
        echo $i['id'];
    }
    

    By using $names = array_unique($names); you only put all the names in the array $names.

    EDIT: well i agree with Tomalak Geret'kal, cause it makes no sense to echo the ID but you could try the following:

    foreach ($status_list['data'] as $data) {
       $names[] = $data['name'];
       $ids[] = $data['id'];
    }
    
    $names = array_unique($names);  // not print the same name.
    $ids = array_unique($ids);  // not print the same name.
    
    $i = 0
    
    foreach ($names as $name) {
        echo $name;
        echo $ids[$i];
        $i ++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示