Php 数组仅替换特定键的值
我有两个数组,在json中类似这样(标签): 数据库中的其他数据如下(数据): 之后,我尝试使用以下代码使用label替换array以提高数据精度:Php 数组仅替换特定键的值,php,arrays,json,Php,Arrays,Json,我有两个数组,在json中类似这样(标签): 数据库中的其他数据如下(数据): 之后,我尝试使用以下代码使用label替换array以提高数据精度: foreach($data as $key => $value) { $datafinal[$key] = array_replace($label,$value); } 输出为(datafinal): 我想要的是这样的东西: { "EXPORT": [ { "da
foreach($data as $key => $value) {
$datafinal[$key] = array_replace($label,$value);
}
输出为(datafinal):
我想要的是这样的东西:
{
"EXPORT": [
{
"dates": "2019-07-01",
"c_job": 12
},
{
"dates": "2019-07-02",
"c_job": 8
},
{
"dates": "2019-07-03",
"c_job": 0
},
{
"dates": "2019-07-04",
"c_job": 11
}
],
"IMPORT": [
{
"dates": "2019-07-01",
"c_job": 0
},
{
"dates": "2019-07-02",
"c_job": 0
},
{
"dates": "2019-07-03",
"c_job": 11
},
{
"dates": "2019-07-04",
"c_job": 0
}
]
}
我尝试了数组reduce和replace基本上你需要做的是对
$data
中的每个顶级数组,检查0值数组(我称之为$blank
),看看当前$data
数组中是否存在日期。如果确实如此,复制该值,否则使用空白值:
$datafinal = array();
foreach ($data as $key => $value) {
foreach ($blank as $bkey => $bvalue) {
if (($dkey = array_search($bvalue['dates'], array_column($value, 'dates'))) !== false) {
$datafinal[$key][$bkey] = $value[$dkey];
}
else {
$datafinal[$key][$bkey] = $bvalue;
}
}
}
echo json_encode($datafinal, JSON_PRETTY_PRINT);
输出:
{
"EXPORT": [
{
"dates": "2019-07-01",
"c_job": 12
},
{
"dates": "2019-07-02",
"c_job": 8
},
{
"dates": "2019-07-03",
"c_job": 0
},
{
"dates": "2019-07-04",
"c_job": 11
}
],
"IMPORT": [
{
"dates": "2019-07-01",
"c_job": 0
},
{
"dates": "2019-07-02",
"c_job": 0
},
{
"dates": "2019-07-03",
"c_job": 11
},
{
"dates": "2019-07-04",
"c_job": 0
}
]
}
我选择了旧的、简单的嵌套的
foreach
循环,尽可能简单快捷(代码中的注释)
而主代码
$template = json_decode('[
{
"dates": "2019-07-01",
"c_job": 0
},
{
"dates": "2019-07-02",
"c_job": 0
},
{
"dates": "2019-07-03",
"c_job": 0
},
{
"dates": "2019-07-04",
"c_job": 0
}
]', true);
$update = json_decode('{
"EXPORT": [
{
"dates": "2019-07-01",
"c_job": 12
},
{
"dates": "2019-07-02",
"c_job": 8
},
{
"dates": "2019-07-04",
"c_job": 11
}
],
"IMPORT": [
{
"dates": "2019-07-03",
"c_job": 11
}
]
}', true);
// Create blank output array with all entries
$output = ["EXPORT" => $template, "IMPORT" => $template];
// Loop over update data (from database) - you may need to tweak this for your use case
foreach ( $update as $type => $updateItem ) {
// Loop over each set of update values (a row of dates and c_job)
foreach ( $updateItem as $updateItem ) {
// Locate in empty output array
foreach ( $output[$type] as &$item ) {
// Same date - update
if ( $updateItem['dates'] == $item['dates']) {
$item['c_job'] = $updateItem['c_job'];
// Stop looking as already updated
break;
}
}
}
}
一个较短但涉及更多的数组
函数,这将创建以日期为键的模板数组,因此您可以使用输入数据中的日期直接更新数据
// Create template array using dates as the array key
$template = array_column($template, null, 'dates');
$output = ["EXPORT" => $template, "IMPORT" => $template];
foreach ( $update as $type => $export ) {
foreach ( $export as $export ) {
// Use the combination of type (EXPORT) and dates from the update data
// to directly update the output (assumes empty value created)
$output[$type][$export['dates']]['c_job'] = $export['c_job'];
}
}
// Re-index data to remove dates as keys
$output = ["EXPORT" => array_values($output['EXPORT']),
"IMPORT" => array_values($output['IMPORT'])];
“[我]已[尝试]阵列缩减和替换” — 请回答您的问题并分享您的代码。
$template = json_decode('[
{
"dates": "2019-07-01",
"c_job": 0
},
{
"dates": "2019-07-02",
"c_job": 0
},
{
"dates": "2019-07-03",
"c_job": 0
},
{
"dates": "2019-07-04",
"c_job": 0
}
]', true);
$update = json_decode('{
"EXPORT": [
{
"dates": "2019-07-01",
"c_job": 12
},
{
"dates": "2019-07-02",
"c_job": 8
},
{
"dates": "2019-07-04",
"c_job": 11
}
],
"IMPORT": [
{
"dates": "2019-07-03",
"c_job": 11
}
]
}', true);
// Create blank output array with all entries
$output = ["EXPORT" => $template, "IMPORT" => $template];
// Loop over update data (from database) - you may need to tweak this for your use case
foreach ( $update as $type => $updateItem ) {
// Loop over each set of update values (a row of dates and c_job)
foreach ( $updateItem as $updateItem ) {
// Locate in empty output array
foreach ( $output[$type] as &$item ) {
// Same date - update
if ( $updateItem['dates'] == $item['dates']) {
$item['c_job'] = $updateItem['c_job'];
// Stop looking as already updated
break;
}
}
}
}
// Create template array using dates as the array key
$template = array_column($template, null, 'dates');
$output = ["EXPORT" => $template, "IMPORT" => $template];
foreach ( $update as $type => $export ) {
foreach ( $export as $export ) {
// Use the combination of type (EXPORT) and dates from the update data
// to directly update the output (assumes empty value created)
$output[$type][$export['dates']]['c_job'] = $export['c_job'];
}
}
// Re-index data to remove dates as keys
$output = ["EXPORT" => array_values($output['EXPORT']),
"IMPORT" => array_values($output['IMPORT'])];