我正在使用CakePHP
我有$ needle和$ numbers:
$needle = 14;
$numbers =
Array
(
[0] => 14
[1] => 15
[2] => 16
)
我只是这样做:
$n = array_search($needle,$numbers);
和$ n返回NULL。
如果你想要所有代码的完整上下文,它是来自ajax调用的php:
if(isset($_POST['numbers']))
{
$jobsX = $this->Job->find('all',array('conditions' => array('Job.owner' => $id)));
$numbers = $_POST['numbers'];
$n = 0;
$this->autoRender = false;
foreach($jobsX as $key => $jo)
{
$needle = $jo['Job']['id'];
$n = array_search($needle,$numbers);
$jobsX[$key]['Job']['order'] = $n;
}
$this->Job->saveAll($jobsX);
echo 1;
}
对我毫无意义......
编辑: 如果你在foreach之外做的话,它会工作......但为什么呢?
答案 0 :(得分:0)
解决方案是使用intval:
$needle = intval($jo['Job']['id']);
我不明白,在另一个案例中这不是必要的......