gpt4 book ai didi

php - 如何删除 jsondecode 中的特定值

转载 作者:行者123 更新时间:2023-11-29 22:25:53 26 4
gpt4 key购买 nike

My table

我有一个名为contracters_system的表,我想删除contracter_id中的特定值,我该怎么做?

$plansarray = Array ( [0] => 4 [1] => 10 [2] => 47 );
$contractid = '21';


foreach ($plansarray as $servicesecondarray){

$servicequery = "select contracterid from contracters_system where planoption_id='$servicesecondarray'";
$servicequeryresult = Yii::app()->db->createCommand($servicequery)->queryAll();

$servicequery1 = "select contracterid from contracters_system where planoption_id <> '$servicesecondarray'";
$servicequeryresult1 = Yii::app()->db->createCommand($servicequery1)->queryAll();


$stack = json_decode($servicequeryresult[0]['contracterid'], true);
array_push($stack, $contractid);


if(empty($stack)){
$arraypush[] = $contractid;
} else {
$arraypush = $stack;
}


$finaldecode = json_encode(array_unique($arraypush));
if(!empty($finaldecode)){

$updatequery = "update contracters_system set contracterid = '$finaldecode' where planoption_id='$servicesecondarray' ";
$updateresult = Yii::app()->db->createCommand($updatequery)->query();

}



for($c=0;$c<count($servicequeryresult1);$c++){

$jsondecode = json_decode($servicequeryresult1[$c]['contracterid']);
//print_r($jsondecode);
if(($key = array_search($_GET['id'], json_decode($servicequeryresult1[$c]['contracterid'])) !== false) {
unset(json_decode($servicequeryresult1[$c]['contracterid'])[$key]);
// echo "test".$key;

$updatequery = "update contracters_system set contracterid = '".unset($jsondecode[$key])."' where planoption_id='$servicesecondarray' ";
$updateresult = Yii::app()->db->createCommand($updatequery)->query();
}

}

["1","21"]中的值类似于21。我的预期输出是["1"] .

如何在 mysql 和 php 中实现此目的?

最佳答案

  1. 读取该字段值$value
  2. 对其进行解码$decoded = json_decode($value)
  3. 遍历$decoded数组
  4. 查找值并删除/取消设置
  5. 再次json_encode($updated_array)
  6. 将此字段值更新回数据库

关于php - 如何删除 jsondecode 中的特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com