Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查数组中的值,并使用PHP将值插入数据库_Php_Mysql_Arrays - Fatal编程技术网

如何检查数组中的值,并使用PHP将值插入数据库

如何检查数组中的值,并使用PHP将值插入数据库,php,mysql,arrays,Php,Mysql,Arrays,我需要一些帮助。我需要使用PHP和MySQL在用户输入数组中插入值(如果存在)。我在下面解释我的表格 db_图像: 这里我需要在上表中插入以下json数组值 $subcat=array(array("id"=>63),array("id"=>64)); $imageArr=array(array("image"=>"abc.png","id"=>63)); 这里,我需要匹配两个数组,如果从代码< >$CASCAT//COD>数组中存在任何值,则在第二个( .E-$IMA

我需要一些帮助。我需要使用PHP和MySQL在用户输入数组中插入值(如果存在)。我在下面解释我的表格

db_图像:

这里我需要在上表中插入以下json数组值

$subcat=array(array("id"=>63),array("id"=>64));
$imageArr=array(array("image"=>"abc.png","id"=>63));

这里,我需要匹配两个数组,如果从代码< >$CASCAT//COD>数组中存在任何值,则在第二个(<代码> .E-$IMACHEARR )数组中,然后将恢复图像插入到表中,如果不存在,空白图像值将插入相应的<代码>子CATILID ID>代码>。请提供帮助。

对于
子类别
数组中的每个元素,可以在
imageArr
上迭代,并检查ID是否匹配(嵌套循环),如下所示:

foreach($subcat as $s) {
    $flag = false;

    foreach($imageArr as $i) {
        if ($s['id'] == $i['id']) {
            // insert ($i['image'], $s['id']) into db
            $flag = true;
            break;
        } 
    }

    if ($flag == false) {
        // insert $s['id'] into db
    }
}

您好,您甚至可以在
array\u column
in\u array
的帮助下,通过减少循环来执行以下操作

<?php

$subcat=array(array("id"=>63),array("id"=>64));
$imageArr=array(array("image"=>"abc.png","id"=>63), array("image"=>"abc.png","id"=>65));

foreach($imageArr as $image){
    /* array_column will do the job with in_array to search in the multi dimension array */
    if(in_array($image['id'], array_column($subcat, 'id'))){
        echo 'exists'. $image['id'].'<br>'; 
    }else{
        echo 'not exists'. $image['id'].'<br>';
    }
}

始终
$subcat
值将插入数据库。但根据我上面的示例,它可以正常工作。你能发布你的完整阵列吗?这样我就可以测试了
<?php

$subcat=array(array("id"=>63),array("id"=>64));
$imageArr=array(array("image"=>"abc.png","id"=>63), array("image"=>"abc.png","id"=>65));

foreach($imageArr as $image){
    /* array_column will do the job with in_array to search in the multi dimension array */
    if(in_array($image['id'], array_column($subcat, 'id'))){
        echo 'exists'. $image['id'].'<br>'; 
    }else{
        echo 'not exists'. $image['id'].'<br>';
    }
}