在维护 json 的循环中打印 foreach 循环
Printing a foreach loop in a loop maintaining json
我有这段代码,因为我正在尝试使用 foreach 循环实现 json 代码
foreach ($single_google as $row) {
$data['step'][] = array(
"@type" => "HowToStep",
"url" => "https://example.com/kitchen#step1",
"name" => "Prepare the surfaces",
"itemListElement" => array(),
"image" => [
"@type" => "ImageObject",
"url" => "https://example.com/photos/1x1/photo-step1.jpg",
"height" => "406",
"width" => "305"
],
);
$all_step_google = json_decode($row["steps"]);
foreach ($all_step_google as $single_step_google) {
$data['itemListElement'][] = array(
"@type" => "HowToDirection",
"text" => "testing",
);
}
}
print_r(json_encode($data));
在编码结束时,尝试 运行 我得到了这个
{
"@type": "HowToStep",
"url": "https://example.com/kitchen#step1",
"name": "Prepare the surfaces",
"itemListElement": [],
"image": {
"@type": "ImageObject",
"url": "https://example.com/photos/1x1/photo-step1.jpg",
"height": "406",
"width": "305"
}
}
],
"itemListElement": [
{
"@type": "HowToDirection",
"text": "test."
}
而不是我实际需要的这个。
{
"@type": "HowToStep",
"url": "https://example.com/kitchen#step1",
"name": "Prepare the surfaces",
"itemListElement": [
{
"@type": "HowToDirection",
"text": "Test"
}
],
"image": {
"@type": "ImageObject",
"url": "https://example.com/photos/1x1/photo-step1.jpg",
"height": "406",
"width": "305"
}
}
],
我希望 "itemListElement" => array(), 在 "image" => [] 之前打印,但它在 foreach 循环之后继续打印。请帮助我,我迷路了。
您在第二个循环中使用 $data['itemListElement'][] = array(...
以便将新键分配给您的数据主数组,正确的代码应该是这样的
............
foreach ($all_step_google as $key => $single_step_google) {
$data['step'][$key]['itemListElement'][] = array(
"@type" => "HowToDirection",
"text" => "testing",
);
}
............
我有这段代码,因为我正在尝试使用 foreach 循环实现 json 代码
foreach ($single_google as $row) {
$data['step'][] = array(
"@type" => "HowToStep",
"url" => "https://example.com/kitchen#step1",
"name" => "Prepare the surfaces",
"itemListElement" => array(),
"image" => [
"@type" => "ImageObject",
"url" => "https://example.com/photos/1x1/photo-step1.jpg",
"height" => "406",
"width" => "305"
],
);
$all_step_google = json_decode($row["steps"]);
foreach ($all_step_google as $single_step_google) {
$data['itemListElement'][] = array(
"@type" => "HowToDirection",
"text" => "testing",
);
}
}
print_r(json_encode($data));
在编码结束时,尝试 运行 我得到了这个
{
"@type": "HowToStep",
"url": "https://example.com/kitchen#step1",
"name": "Prepare the surfaces",
"itemListElement": [],
"image": {
"@type": "ImageObject",
"url": "https://example.com/photos/1x1/photo-step1.jpg",
"height": "406",
"width": "305"
}
}
],
"itemListElement": [
{
"@type": "HowToDirection",
"text": "test."
}
而不是我实际需要的这个。
{
"@type": "HowToStep",
"url": "https://example.com/kitchen#step1",
"name": "Prepare the surfaces",
"itemListElement": [
{
"@type": "HowToDirection",
"text": "Test"
}
],
"image": {
"@type": "ImageObject",
"url": "https://example.com/photos/1x1/photo-step1.jpg",
"height": "406",
"width": "305"
}
}
],
我希望 "itemListElement" => array(), 在 "image" => [] 之前打印,但它在 foreach 循环之后继续打印。请帮助我,我迷路了。
您在第二个循环中使用 $data['itemListElement'][] = array(...
以便将新键分配给您的数据主数组,正确的代码应该是这样的
............
foreach ($all_step_google as $key => $single_step_google) {
$data['step'][$key]['itemListElement'][] = array(
"@type" => "HowToDirection",
"text" => "testing",
);
}
............