PHP in_array未找到一个值,该值是有 [英] PHP in_array isn't finding a value that is there

查看:146
本文介绍了PHP in_array未找到一个值,该值是有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为$ friend_array的数组。当我的print_r($ friend_array),它看起来是这样的:

I have an array called $friend_array. When I print_r($friend_array) it looks like this:

Array ( [0] => 3,2,5 ) 

我也有正在从网址拉出可变叫做$流体

I also have a variable called $uid that is being pulled from the url.

在我测试的页面,$ uid具有3的值,因此它是数组中

On the page I'm testing, $uid has a value of 3 so it is in the array.

但是,以下是说,它不存在:

However, the following is saying that it isn't there:

if(in_array($uid, $friend_array)){
  $is_friend = true;
}else{
  $is_friend = false;

这始终返回false。我呼应$ uid和值为3我打印阵列和3是存在的。

This always returns false. I echo the $uid and it is 3. I print the array and 3 is there.

我是什么做错了吗?任何帮助将大大AP preciated!

What am I doing wrong? Any help would be greatly appreciated!

推荐答案

阵列([0] => -3,2,5-)表示数组元素 0 是一个字符串 -3,2,5- ,这样,你做一个 is_array 检查 $ UID 所以你必须使用先打破字符串到一个数组作为一个分离器,然后再检查 $ UID

Array ( [0] => 3,2,5 ) means that the array element 0 is a string 3,2,5, so, before you do an is_array check for the $uid so you have to first break that string into an array using , as a separator and then check for$uid:

// $friend_array contains as its first element a string that
// you want to make into the "real" friend array:
$friend_array = explode(',', $friend_array[0]);

if(in_array($uid, $friend_array)){
  $is_friend = true;
}else{
  $is_friend = false;
}

工作的例子

这篇关于PHP in_array未找到一个值,该值是有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆