(?m)%{GREEDYDATA}
будет соответствовать любому многострочному журналу, включая ваши. Пожалуйста, проверьте здесь
Это решение более многословно, чем могло бы быть написано, но выгода от этой конструкции кодирования заключается в простоте обслуживания и порядке элементов подмассива. Если вы внимательно посмотрите, вы увидите, что каждый элемент в выходных данных всегда находится в одном и том же порядке, независимо от того, из какого входного массива он получен.
Поскольку в этом решении используется только языковая конструкция (foreach()
), оно будет работать в КАЖДОЙ версии php.
Код: ( Демо )
foreach ($array1 as $row) {
$result[$row['id_question']]['id'] = $row['id'];
$result[$row['id_question']]['id_question'] = $row['id_question'];
$result[$row['id_question']]['type'] = null;
$result[$row['id_question']]['answer'] = $row['answer'];
$result[$row['id_question']]['pi'] = $row['pi'];
$result[$row['id_question']]['ca'] = $row['ca'];
$result[$row['id_question']]['pic'] = $row['pic'];
$result[$row['id_question']]['deadline'] = $row['deadline'];
$result[$row['id_question']]['new_deadline'] = $row['new_deadline'];
$result[$row['id_question']]['reason'] = $row['reason'];
$result[$row['id_question']]['notes'] = $row['notes'];
$result[$row['id_question']]['last_root'] = null;
$result[$row['id_question']]['exist_good'] = null;
$result[$row['id_question']]['exist_not_good'] = null;
$result[$row['id_question']]['not_exist'] = null;
$result[$row['id_question']]['n_a'] = null;
}
foreach ($array2 as $row) {
if (isset($result[$row['id']])) { // default elements already declared, just overwrite the nulls
// $row['question'] is omitted from data
$result[$row['id']]['type'] = $row['type'];
$result[$row['id']]['last_root'] = $row['last_root'];
$result[$row['id']]['exist_good'] = $row['exist_good'];
$result[$row['id']]['exist_not_good'] = $row['exist_not_good'];
$result[$row['id']]['not_exist'] = $row['not_exist'];
$result[$row['id']]['n_a'] = $row['n_a'];
} else {
$result[$row['id']]['id'] = $row['question']; // no id, so use question
$result[$row['id']]['id_question'] = null;
$result[$row['id']]['type'] = $row['type'];
$result[$row['id']]['answer'] = null;
$result[$row['id']]['pi'] = null;
$result[$row['id']]['ca'] = null;
$result[$row['id']]['pic'] = null;
$result[$row['id']]['deadline'] = null;
$result[$row['id']]['new_deadline'] = null;
$result[$row['id']]['reason'] = null;
$result[$row['id']]['notes'] = null;
$result[$row['id']]['last_root'] = $row['last_root'];
$result[$row['id']]['exist_good'] = $row['exist_good'];
$result[$row['id']]['exist_not_good'] = $row['exist_not_good'];
$result[$row['id']]['not_exist'] = $row['not_exist'];
$result[$row['id']]['n_a'] = $row['n_a'];
}
}
var_export($result);
Вывод:
array (
39 =>
array (
'id' => '7ac648ce-18aa-11e9-b673-005056be36b2',
'id_question' => '39',
'type' => 'NON DOS',
'answer' => '1',
'pi' => 'very good',
'ca' => NULL,
'pic' => NULL,
'deadline' => NULL,
'new_deadline' => NULL,
'reason' => NULL,
'notes' => NULL,
'last_root' => '1',
'exist_good' => '1',
'exist_not_good' => '0',
'not_exist' => '1',
'n_a' => '0',
),
40 =>
array (
'id' => '8653ef3d-18aa-11e9-b673-005056be36b2',
'id_question' => '40',
'type' => 'NON DOS',
'answer' => '3',
'pi' => 'no problem',
'ca' => NULL,
'pic' => NULL,
'deadline' => NULL,
'new_deadline' => NULL,
'reason' => NULL,
'notes' => NULL,
'last_root' => '1',
'exist_good' => '1',
'exist_not_good' => '0',
'not_exist' => '1',
'n_a' => '0',
),
38 =>
array (
'id' => 'Kebersihan Meja Counter dan Meja Fincoy ',
'id_question' => NULL,
'type' => 'NON DOS',
'answer' => NULL,
'pi' => NULL,
'ca' => NULL,
'pic' => NULL,
'deadline' => NULL,
'new_deadline' => NULL,
'reason' => NULL,
'notes' => NULL,
'last_root' => '1',
'exist_good' => '1',
'exist_not_good' => '0',
'not_exist' => '1',
'n_a' => '0',
),
42 =>
array (
'id' => 'Sales Guide',
'id_question' => NULL,
'type' => 'NON DOS',
'answer' => NULL,
'pi' => NULL,
'ca' => NULL,
'pic' => NULL,
'deadline' => NULL,
'new_deadline' => NULL,
'reason' => NULL,
'notes' => NULL,
'last_root' => '1',
'exist_good' => '1',
'exist_not_good' => '0',
'not_exist' => '1',
'n_a' => '0',
),
...
)
Здесь вы можете использовать ksort()
для упорядочения строк данных id
, array_values()
переиндексировать вывод или преобразовать в json с помощью json_encode()
.
Чтобы упростить ваш код, я постарался сделать это за несколько шагов. Это сохраняет движущиеся данные несколько раз (комментарии в коде) ...
$json = json_decode($string, true);
$json2 = json_decode($string2, true);
// Create $result with an array indexed by id_question
$result = array_column($json, null, "id_question");
foreach($json2 as $key)
{
// If question doesn't exist
if(!isset($result[$key['id']]))
{
// Set values from new question
$row=$key;
$row["id_question"]=$key['id'];
$row["id"]=$key['question'];
// Remove this element as not needed in new array
unset ( $row['question']);
$row["answer"]=null;
$row["pi"]=null;
$row["ca"]=null;
$row["pic"]=null;
$row["new_deadline"]=null;
$row["reason"]=null;
$row["notes"]=null;
$row["deadline"]=null;
// Add to result
$result[]=$row;
}
else
{
// Question is already present, just add in new details
$result[$key['id']]["type"]=$key['type'];
$result[$key['id']]["last_root"]=$key['last_root'];
$result[$key['id']]["exist_good"]=$key['exist_good'];
$result[$key['id']]["exist_not_good"]=$key['exist_not_good'];
$result[$key['id']]["not_exist"]=$key['not_exist'];
$result[$key['id']]["n_a"]=$key['n_a'];
}
}
// Use array_values() to remove keys (the question ID) and encode result
$json=json_encode(array_values($result));
print_r($json);