PHP检查数组中是否存在数据并防止添加数据

PHP检查数组中是否存在数据并防止添加数据,php,arrays,Php,Arrays,我想将数组添加到数组的数组中。然后检查之前添加的数组是否存在。这是我检查的方法 for ($x = 0; $x <= 10; $x++){ $data = array( 'order_id' => $invoice->order_id, ); $datas = array(); // this is the array contains array I create to // let me check

我想将数组添加到数组的数组中。然后检查之前添加的数组是否存在。这是我检查的方法

for ($x = 0; $x <= 10; $x++){
  $data = array(
        'order_id' => $invoice->order_id,            
      );

  $datas = array(); 
  // this is the array contains array I create to 
  // let me check whether $data exist in it.

  foreach ($datas as $value){

  if ($value['order_id']==$data['order_id']){ 
      //if the $data already exist in $datas  
      print "ok";
  }    

  $datas = array_push($datas,$data); 
  // push $data to $datas for the next checking of existing of $data, 
  // that means, next time if same order id exist, it will be found in 
  // the above checking. 
}

您的代码有几个错误

  • $datas
    为空(mwweb的注释)
  • foreach
    缺少一个
    }
  • 我建议您使用关联数组检查您的订单是否已经是数据的一部分:

    $datas[$data['order_id']] = $data;
    

    然后您可以使用isset($datas[$data['order\u id']])检查它。

    哪个部分不工作?如果在foreach内部使用continue,那么它将跳到foreach循环的下一个迭代。或者if语句捕捉不到正确的东西?您可以使用真实输入和正确的代码语法更改伪代码吗。完全不清楚您的输入数组是什么样子,您想要比较什么,您想要什么样的输出?
    $datas=array();foreach($datas as$value3){
    这永远不会工作共享原始代码?$datas=array();foreach($datas as$value3)永远不会工作。所以正确的方法是?我可以循环使用它。所以我的问题是如何将数组放入aray数组。
    $datas[$data['order_id']] = $data;