数组替换值仅特定键

时间:2019-08-25 05:56:50

标签: php arrays json

我在json中有两个这样的数组(标签):

[
    {
      "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
    }
  ]

以及来自数据库的其他数据(数据):

{
    "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
      }
    ]
}

之后,我尝试使用此代码对数组进行替换以使标签具有数据精度:

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-04",
        "c_job": 11
      },
      {
        "dates": "2019-07-04",
        "c_job": 0
      }
    ],
    "IMPORT": [
      {
        "dates": "2019-07-03",
        "c_job": 11
      },
      {
        "dates": "2019-07-02",
        "c_job": 0
      },
      {
        "dates": "2019-07-03",
        "c_job": 0
      },
      {
        "dates": "2019-07-04",
        "c_job": 0
      }
    ]
  }

我想要的是这样的:

{
    "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
      }
    ]
  }

我尝试减少并替换数组

2 个答案:

答案 0 :(得分:0)

基本上,您需要对join()中的每个顶级数组进行操作,然后遍历0值数组(我将其称为$data),看看当前日期是否存在$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);

Demo on 3v4l.org

答案 1 :(得分: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;
            }
        }
    }
}

一个较短的函数,但是涉及更多的array_函数,这将创建一个以日期为键的模板数组,因此您可以使用输入数据中的日期直接更新数据... < / p>

// 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'])];