我有一个逻辑问题,我不知道为什么会发生或如何解决。我有一个要更改值并存储在数组中的JSON文件
{
"info": [
{
"actress": "Evan Rachel Wood",
"actress-id": "162",
"actress-slug": "evan-rachel-wood",
"week": {
"id": "464",
"first": "08/08/18",
"last": "20/08/18",
"movies": [
{
"id": "1930500",
"titulo": "At imperdiet convallis blandit nisl elementum.",
"post_status": "pending",
"category": "comedy"
},
{
"id": "1930499",
"titulo": "Porttitor pellentesque imperdiet rutrum.",
"post_status": "pending",
"category": "drama"
},
{
"id": "1930498",
"titulo": "Quam aenean cursus congue vivamus neque luctus libero quam.",
"post_status": "pending",
"category": "adventure"
},
{
"id": "1930497",
"titulo": "Sapien ultrices auctor congue enim donec conubia egestas vel senectus",
"post_status": "pending",
"category": "comedy"
},
{
"id": "1930444",
"titulo": "Imperdiet at inceptos condimentum non dictumst primis interdum",
"post_status": "pending",
"category": null
}
]
}
},
{
"actress": "Elisabeth Moss",
"actress-id": "217",
"actress-slug": "elisabeth-moss",
"week": {
"id": "263",
"first": "01/08/18",
"last": "10/08/18",
"movies": [
{
"id": "1930443",
"titulo": "Rhoncus aliquet proin ut curabitur sit vestibulum feugiat",
"post_status": "pending",
"category": "adventure"
},
{
"id": "1930442",
"titulo": "Mi himenaeos elementum conubia varius porta habitant hendrerit sociosqu",
"post_status": "pending",
"category": "drama"
},
{
"id": "1930440",
"titulo": "Duis massa maecenas platea purus vel arcu duis vulputate",
"post_status": "pending",
"category": "action"
},
{
"id": "1930393",
"titulo": "Elit semper massa fringilla molestie bibendum, hendrerit",
"post_status": "pending",
"category": "drama"
}
]
}
}
]
}
$values = json_decode(file_get_contents('json_file.json'), true);
$news = array();
foreach ($values["info"] as $key => $value){
for($i = 0; $i < count($value["week"]["movies"]); $i++){
if($value["week"]["movies"][$i]["post_status"] == "pending"):
$news["actress"] = $value["actress"];
$news["actress-id"] = $value["actress-id"];
$news["actress-slug"] = $value["actress-slug"];
$news["week"]["movies"][$i] = $value["week"]["movies"][$i];
$news["week"]["movies"][$i]["post_status"] = "draft";
endif;
//$news = $news;
}
var_dump($news);
}
array(4) {
["actress"]=>
string(16) "Evan Rachel Wood"
["actress-id"]=>
string(3) "162"
["actress-slug"]=>
string(16) "evan-rachel-wood"
["week"]=>
array(1) {
["movies"]=>
array(5) {
[0]=>
array(4) {
["id"]=>
string(7) "1930500"
["titulo"]=>
string(46) "At imperdiet convallis blandit nisl elementum."
["post_status"]=>
string(5) "draft"
["category"]=>
string(6) "comedy"
}
[1]=>
array(4) {
["id"]=>
string(7) "1930499"
["titulo"]=>
string(40) "Porttitor pellentesque imperdiet rutrum."
["post_status"]=>
string(5) "draft"
["category"]=>
string(5) "drama"
}
[2]=>
array(4) {
["id"]=>
string(7) "1930498"
["titulo"]=>
string(59) "Quam aenean cursus congue vivamus neque luctus libero quam."
["post_status"]=>
string(5) "draft"
["category"]=>
string(19) "adventure"
}
[3]=>
array(4) {
["id"]=>
string(7) "1930497"
["titulo"]=>
string(69) "Sapien ultrices auctor congue enim donec conubia egestas vel senectus"
["post_status"]=>
string(5) "draft"
["category"]=>
string(6) "comedy"
}
[4]=>
array(4) {
["id"]=>
string(7) "1930444"
["titulo"]=>
string(62) "Imperdiet at inceptos condimentum non dictumst primis interdum"
["post_status"]=>
string(5) "draft"
["category"]=>
NULL
}
}
}
}
array(4) {
["actress"]=>
string(14) "Elisabeth Moss"
["actress-id"]=>
string(3) "217"
["actress-slug"]=>
string(14) "elisabeth-moss"
["week"]=>
array(1) {
["movies"]=>
array(5) {
[0]=>
array(4) {
["id"]=>
string(7) "1930443"
["titulo"]=>
string(57) "Rhoncus aliquet proin ut curabitur sit vestibulum feugiat"
["post_status"]=>
string(5) "draft"
["category"]=>
string(9) "adventure"
}
[1]=>
array(4) {
["id"]=>
string(7) "1930442"
["titulo"]=>
string(71) "Mi himenaeos elementum conubia varius porta habitant hendrerit sociosqu"
["post_status"]=>
string(5) "draft"
["category"]=>
string(5) "drama"
}
[2]=>
array(4) {
["id"]=>
string(7) "1930440"
["titulo"]=>
string(56) "Duis massa maecenas platea purus vel arcu duis vulputate"
["post_status"]=>
string(5) "draft"
["category"]=>
string(6) "action"
}
[3]=>
array(4) {
["id"]=>
string(7) "1930393"
["titulo"]=>
string(56) "Elit semper massa fringilla molestie bibendum, hendrerit"
["post_status"]=>
string(5) "draft"
["category"]=>
string(5) "drama"
}
}
}
}
array(4) {
["actress"]=>
string(16) "Evan Rachel Wood"
["actress-id"]=>
string(3) "162"
["actress-slug"]=>
string(16) "evan-rachel-wood"
["week"]=>
array(1) {
["movies"]=>
array(5) {
[0]=>
array(4) {
["id"]=>
string(7) "1930500"
["titulo"]=>
string(46) "At imperdiet convallis blandit nisl elementum."
["post_status"]=>
string(5) "draft"
["category"]=>
string(6) "comedy"
}
[1]=>
array(4) {
["id"]=>
string(7) "1930499"
["titulo"]=>
string(40) "Porttitor pellentesque imperdiet rutrum."
["post_status"]=>
string(5) "draft"
["category"]=>
string(5) "drama"
}
[2]=>
array(4) {
["id"]=>
string(7) "1930498"
["titulo"]=>
string(59) "Quam aenean cursus congue vivamus neque luctus libero quam."
["post_status"]=>
string(5) "draft"
["category"]=>
string(19) "adventure"
}
[3]=>
array(4) {
["id"]=>
string(7) "1930497"
["titulo"]=>
string(69) "Sapien ultrices auctor congue enim donec conubia egestas vel senectus"
["post_status"]=>
string(5) "draft"
["category"]=>
string(6) "comedy"
}
[4]=>
array(4) {
["id"]=>
string(7) "1930444"
["titulo"]=>
string(62) "Imperdiet at inceptos condimentum non dictumst primis interdum"
["post_status"]=>
string(5) "draft"
["category"]=>
NULL
}
}
}
}
array(4) {
["actress"]=>
string(14) "Elisabeth Moss"
["actress-id"]=>
string(3) "217"
["actress-slug"]=>
string(14) "elisabeth-moss"
["week"]=>
array(1) {
["movies"]=>
array(5) {
[0]=>
array(4) {
["id"]=>
string(7) "1930443"
["titulo"]=>
string(57) "Rhoncus aliquet proin ut curabitur sit vestibulum feugiat"
["post_status"]=>
string(5) "draft"
["category"]=>
string(9) "adventure"
}
[1]=>
array(4) {
["id"]=>
string(7) "1930442"
["titulo"]=>
string(71) "Mi himenaeos elementum conubia varius porta habitant hendrerit sociosqu"
["post_status"]=>
string(5) "draft"
["category"]=>
string(5) "drama"
}
[2]=>
array(4) {
["id"]=>
string(7) "1930440"
["titulo"]=>
string(56) "Duis massa maecenas platea purus vel arcu duis vulputate"
["post_status"]=>
string(5) "draft"
["category"]=>
string(6) "action"
}
[3]=>
array(4) {
["id"]=>
string(7) "1930393"
["titulo"]=>
string(56) "Elit semper massa fringilla molestie bibendum, hendrerit"
["post_status"]=>
string(5) "draft"
["category"]=>
string(5) "drama"
}
[4]=>
array(4) {
["id"]=>
string(7) "1930444"
["titulo"]=>
string(62) "Imperdiet at inceptos condimentum non dictumst primis interdum"
["post_status"]=>
string(5) "draft"
["category"]=>
NULL
}
}
}
}
在最后一个数组的电影数组中,不应有属于第一个数组的键[4]。我看不出问题出在哪里。我的逻辑使事情成真或如何纠正它有什么问题。
答案 0 :(得分:1)
也许是这个吗?
$values = json_decode(file_get_contents('json_file.json'), true);
$news = array();
foreach ($values["info"] as $key => $value){
$news[$key]["actress"] = $value["actress"];
$news[$key]["actress-id"] = $value["actress-id"];
$news[$key]["actress-slug"] = $value["actress-slug"];
$movies_array = array();
foreach ($value["week"]["movies"] as $key2 => $value2) {
if($value2["post_status"] == "pending"):
$movies_array[] = array('id' => $value2['id'], 'titulo' => $value2['titulo'], 'post_status' => 'draft', 'category' => $value2['category']);
endif;
}
$news[$key]["week"]["movies"] = $movies_array;
}
var_dump($news);