dongtaochan0777 2017-11-25 06:20
浏览 74
已采纳

如何检查数组中是否存在值并使用PHP将值插入数据库

I need some help. I need to insert the the value if its present inside the user input array using PHP and MySQL. I am explaining my table below.

db_images:

id         image     subcat_id

Here I need to insert into above table as the following json array value.

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

Here I need to match both array if any value from $subcat array is present inside the second (i.e-$imageArr) array then the resepective image will insert into the table and if not present the the blank image value will insert with the respective subcat_id . Please help.

  • 写回答

2条回答 默认 最新

  • douganggu4392 2017-11-25 06:34
    关注

    For every element in the subcat array, you can iterate on the imageArr and check if the ids match (nested loop), like this:

    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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大