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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条