如何cek数组php



我有一个数组,我将插入到项目表中,这里我使用多个插入

Array
(
[0] => Array
(
[id_service] => 2
[tracking_number] => RJC219384044389234035
)
[1] => Array
(
[id_service] => 1
[tracking_number] => RJC749944771469498146
)
)

在item表中,已经存在id_service:2如何进行验证,当其中一个输入已经在项表中时,那么所有的输入都是false,所以它失败了?

我的代码

public function nyobain()
{
$resi             = $this->input->post('kode'); //tracking_number
$expld            = explode(' ', $resi);
$data             = $this->M_outbound_destinasi->dbGet($expld); //get 'id_service' where tracking_number
// $db  = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
// foreach ($db $key) {
//     $temp=array($key->id_service);
// }
// how to cek ? if id_service already in the item table
foreach ($data as $key) {
$idnya                = $key['id_service'];
$id_outbound          = $key['id_outbound'];
$id_indes             = $key['id_indes'];
$id_outbag            = $key['id_outbag'];
$data_insert = array(
'jenis'           => 'outbound-destinasi',
'id_service'      => $idnya,
'id_indes'        => $id_indes,
'id_outbound'     => $id_outbound,
'id_outbag'       => $id_outbag,
'id_admin'        => $this->session->userdata('ses_id')
);
$this->db->insert('tabel_items', $data_insert);
}
echo json_encode($data);
}

您可以使用in_array来实现此

public function nyobain(){
$resi             = $this->input->post('kode');
$expld            = explode(' ', $resi);
$data             = $this->M_outbound_destinasi->dbGet($expld); 

$db  = $this->db->query("SELECT id_service FROM `tabel_items`")->result();

if(isset($data) && $data !== ''){//check if $data has a value and it not null
if (in_array($data ,$db, true)) {
return $data.'already exists in the DB!';
}else{

foreach ($data as $key) {
$idnya                = $key['id_service'];
$id_outbound          = $key['id_outbound'];
$id_indes             = $key['id_indes'];
$id_outbag            = $key['id_outbag'];

$data_insert = [
'jenis'           => 'outbound-destinasi',
'id_service'      => $idnya,
'id_indes'        => $id_indes,
'id_outbound'     => $id_outbound,
'id_outbag'       => $id_outbag,
'id_admin'        => $this->session->userdata('ses_id')
];
$this->db->insert('tabel_items', $data_insert);
}

return json_encode($data);
}
}
}

相关内容

  • 没有找到相关文章

最新更新