dongyong1897 2017-09-27 14:58
浏览 33
已采纳

检查数组中的索引是否不同[关闭]

I am trying to compare indexes in one array, if all indexes are not the same then It should dispay error. If they are same, take one value as string.

First I retrieve data from db,

$idNaloga = array();

 while($r=$ss->fetch()){
   $idNaloga[] = $r['idNaloga'];
 }

If I receive array like this, it should display error because of 72

array(3) {
  [0]=>
  string(2) "19"
  [1]=>
  string(2) "19"
  [2]=>
  string(2) "72"
}

If result is, it should take 19 as string.

array(4) {
  [0]=>
  string(2) "19"
  [1]=>
  string(2) "19"
  [2]=>
  string(2) "19"
  [3]=>
  string(2) "19"
}

I was trying to use array_unique() function, but could not get correct result. I was googling but could not find it.

  • 写回答

4条回答 默认 最新

  • dongmo2324 2017-09-27 15:06
    关注

    You can do this with array_unique

    if (count(array_unique($idNaloga)) > 1) {
         //return error
    } else {
        echo $idNaloga[0];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画