Array
(
[0] => 159042564114859_722842697734840
[1] => 180466813056_10152147419628057
[2] => 201613513237343_622449081153782
)
$post_ids = array('201613513237343_622449081153782', '180466813056_10152147419628057', '159042564114859_722842697734840');
foreach($posts->data as $post)
{
if(array_search( $post->id, $post_ids ))
{
print '<p>FOUND ' . $post->id . '<p>';
}
}
Outputs:
输出:
FOUND 159042564114859_722842697734840
FOUND 180466813056_10152147419628057
For some reason, it does not detect the third item in the array, with the index of 3, value of 201613513237343_622449081153782
由于某种原因,它没有检测到数组中的第三个项,索引为3,值为201613513237343_622449081153782
This outputs:
这个输出:
foreach($posts->data as $post)
{
print '<p>post->id: ' . $post->id . '</p>';
}
post->id: 159042564114859_722842697734840
post->id: 180466813056_10152147419628057
post->id: 201613513237343_622449081153782
So I know the data is in there, what am I doing wrong with the array_search function?
我知道数据在这里,array_search函数哪里出错了?
1 个解决方案
#1
5
It doesn't output the first post id because array_search
returns the index
, which is 0
.
它不会输出第一个post id,因为array_search返回索引,即0。
Change your check to:
改变你的检查:
if (array_search($post->id, $post_ids) !== false) {
to explicitly compare with false
.
与false进行显式比较。
0 == false; // true
0 === false; // false
PHP is not very nice about it's arguments and return values, but most search functions use some kind of 'not found' value. -1
, false
and null
are common values used for this purpose.
PHP对于它的参数和返回值不是很好,但是大多数搜索函数都使用某种“未找到”值。-1、false和null是用于此目的的通用值。
#1
5
It doesn't output the first post id because array_search
returns the index
, which is 0
.
它不会输出第一个post id,因为array_search返回索引,即0。
Change your check to:
改变你的检查:
if (array_search($post->id, $post_ids) !== false) {
to explicitly compare with false
.
与false进行显式比较。
0 == false; // true
0 === false; // false
PHP is not very nice about it's arguments and return values, but most search functions use some kind of 'not found' value. -1
, false
and null
are common values used for this purpose.
PHP对于它的参数和返回值不是很好,但是大多数搜索函数都使用某种“未找到”值。-1、false和null是用于此目的的通用值。