将数据添加到数组中的每个元素 - > JSON

时间:2016-10-20 08:37:22

标签: php mysql arrays json

我将数据从MySQL数据库提取到一个关联数组中。

我需要向数组中的每个元素添加一行额外的数据,subjecttring,然后转换为JSON。

但由于某种原因,我只能将其添加到数组中的第一个元素,或者作为数组中的新的最终元素。

代码:

$sql_build_profile="SELECT SourceOperation, FC, Register, RegisterID FROM DeviceProfiles WHERE DeviceID = '".$q."' AND RegisterID IN (".$ParameterArray.") ORDER BY RegisterID ASC";
$result_build_profile = mysqli_query($con,$sql_build_profile);
$json_array = array();
//Time to build the output
while($row1 = mysqli_fetch_assoc($result_build_profile)) {

$topicString = $UniqueString."/".$row1['RegisterID'];
$json_array[] = $row1;
foreach ($result_build_profile as $key => $result_build_profile) {
    $result_build_profile[$key]['topic'] = $topicString;
}
echo json_encode($result_json);
}

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

$sql_build_profile="SELECT SourceOperation, FC, Register, RegisterID FROM DeviceProfiles WHERE DeviceID = '".$q."' AND RegisterID IN (".$ParameterArray.") ORDER BY RegisterID ASC";
$result_build_profile = mysqli_query($con,$sql_build_profile);

$json_array = array();

//Time to build the output
while($row1 = mysqli_fetch_assoc($result_build_profile)) {

    $topicString = $UniqueString."/".$row1['RegisterID'];
    $row1['topic'] = $topicString;

    $json_array[] = $row1;
}

echo json_encode($json_array);