我在mysql表中有一个colomn,其中包含数据数组
{#369 ▼
+"secondaryleader": "ddfsfsfsdfd"
+"role": "Meet-LSV"
+"activity": "2"
+"rosterstart_time": "Fri Jan 05 2018 11:18:15 GMT+0530 (Sri Lanka Standard Time)"
+"actualstart_time": "Fri Jan 05 2018 06:18:16 GMT+0530 (Sri Lanka Standard Time)"
+"rosterfinish_time": "Fri Jan 05 2018 11:18:18 GMT+0530 (Sri Lanka Standard Time)"
+"actualfinish_time": "Fri Jan 05 2018 11:18:19 GMT+0530 (Sri Lanka Standard Time)"
+"uniformsetup_status": null
+"uniformpackup_status": null
}
当我更新表时,我必须添加另一个与此相同的数组,但是当我添加数组时,不应该删除此数组
{#369 ▼
+"0": {#367 ▼
+"0": {#371 ▶}
+"2": {#372 ▼
+"secondaryleader": "sdfsdfsf"
+"role": "Meet-LSV"
+"activity": "2"
+"rosterstart_time": "Fri Jan 05 2018 11:18:15 GMT+0530 (Sri Lanka Standard Time)"
+"actualstart_time": "Fri Jan 05 2018 06:18:16 GMT+0530 (Sri Lanka Standard Time)"
+"rosterfinish_time": "Fri Jan 05 2018 11:18:18 GMT+0530 (Sri Lanka Standard Time)"
+"actualfinish_time": "Fri Jan 05 2018 11:18:19 GMT+0530 (Sri Lanka Standard Time)"
+"uniformsetup_status": null
+"uniformpackup_status": null
}
}
+"2": {#373 ▼
+"secondaryleader": "vat69"
+"role": "Meet-LSV"
+"activity": "2"
+"rosterstart_time": "Fri Jan 05 2018 11:18:15 GMT+0530 (Sri Lanka Standard Time)"
+"actualstart_time": "Fri Jan 05 2018 06:18:16 GMT+0530 (Sri Lanka Standard Time)"
+"rosterfinish_time": "Fri Jan 05 2018 11:18:18 GMT+0530 (Sri Lanka Standard Time)"
+"actualfinish_time": "Fri Jan 05 2018 11:18:19 GMT+0530 (Sri Lanka Standard Time)"
+"uniformsetup_status": null
+"uniformpackup_status": null
}
}
我的代码是
if($formArray->secondaryleader_details == ''||$formArray->secondaryleader_details == []){
$merge_data=$data['secondaryleader_details'];
$array= $merge_data;
}else{
$arr_length = count($formArray->secondaryleader_details);
$new_array = ($arr_length+1);
$merge_data=$data['secondaryleader_details'];
$new_arr_data = array($merge_data);
//$array=array_push($formArray->secondaryleader_details,$new_arr_data);
// foreach ($new_arr_data as $mg) {
$array=[$formArray->secondaryleader_details];
$array[$new_array]= $merge_data;
答案 0 :(得分:1)
您可以尝试:
$decoded_array = json_decode($array_from_db)
if(!$decoded_array){
$decoded_array = [];
}
if(is_object($decoded_array)){
$decoded_array = [$decoded_array];
}
$decoded_array[]=$new_data;
//save $decoded_array to database
答案 1 :(得分:0)
你能试试这个:
$decoded_array = json_decode($array_from_db)
$new_array = [];
array_push($decoded_array, $new_array)
$array_from_db = json_encode($decoded_array)
$array_from_db.save()
希望有所帮助:)