Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用PHP编辑JSON?_Php_Json - Fatal编程技术网

如何用PHP编辑JSON?

如何用PHP编辑JSON?,php,json,Php,Json,我想删除整个文本,使其像 “文本”:“ 但是我的代码不起作用,它保存的是相同的原始json文件。您必须使用带符号的pass-by-reference { "books": [ { "book": "1 Nephi", "chapters": [ { "chapter": 1, "reference": "1 Nep

我想删除整个文本,使其像 “文本”:“


但是我的代码不起作用,它保存的是相同的原始json文件。

您必须使用带符号的pass-by-reference

{
    "books": [
        {
            "book": "1 Nephi",
            "chapters": [
                {
                    "chapter": 1,
                    "reference": "1 Nephi 1",
                    "verses": [
                        {
                            "reference": "1 Nephi 1:1",
                            "text": "I, Nephi, having been born of goodly parents, therefore I was taught somewhat in all the learning of my father; and having seen many afflictions in the course of my days, nevertheless, having been highly favored of the Lord in all my days; yea, having had a great knowledge of the goodness and the mysteries of God, therefore I make a record of my proceedings in my days.",
                            "verse": 1
                        },
                        {
                            "reference": "1 Nephi 1:2",
                            "text": "Yea, I make a record in the language of my father, which consists of the learning of the Jews and the language of the Egyptians.",
                            "verse": 2
                        },
                        {
                            "reference": "1 Nephi 1:3",
                            "text": "And I know that the record which I make is true; and I make it with mine own hand; and I make it according to my knowledge.",
                            "verse": 3
                        },
您的代码应该是:-

foreach($chapters['verses'] as &$verses){
   $verses['text'] = "";      
}

您必须使用带符号的按参考传递

{
    "books": [
        {
            "book": "1 Nephi",
            "chapters": [
                {
                    "chapter": 1,
                    "reference": "1 Nephi 1",
                    "verses": [
                        {
                            "reference": "1 Nephi 1:1",
                            "text": "I, Nephi, having been born of goodly parents, therefore I was taught somewhat in all the learning of my father; and having seen many afflictions in the course of my days, nevertheless, having been highly favored of the Lord in all my days; yea, having had a great knowledge of the goodness and the mysteries of God, therefore I make a record of my proceedings in my days.",
                            "verse": 1
                        },
                        {
                            "reference": "1 Nephi 1:2",
                            "text": "Yea, I make a record in the language of my father, which consists of the learning of the Jews and the language of the Egyptians.",
                            "verse": 2
                        },
                        {
                            "reference": "1 Nephi 1:3",
                            "text": "And I know that the record which I make is true; and I make it with mine own hand; and I make it according to my knowledge.",
                            "verse": 3
                        },
您的代码应该是:-

foreach($chapters['verses'] as &$verses){
   $verses['text'] = "";      
}
您必须使用

案例1:如果json结构在每种情况下始终相同,则

foreach($obj['books'] as &$books){
   foreach ($books['chapters'] as &$chapters){
     foreach($chapters['verses'] as &$verses){
          $verses['text'] = "";      
        }
   }
 }
输出:-

案例2:如果json结构可以更改,那么

foreach($obj['books'][0]['chapters'][0]['verses'] as &$verses){//& => passing by reference concept
  $verses['text'] = '';
}
输出:-

您必须使用

案例1:如果json结构在每种情况下始终相同,则

foreach($obj['books'] as &$books){
   foreach ($books['chapters'] as &$chapters){
     foreach($chapters['verses'] as &$verses){
          $verses['text'] = "";      
        }
   }
 }
输出:-

案例2:如果json结构可以更改,那么

foreach($obj['books'][0]['chapters'][0]['verses'] as &$verses){//& => passing by reference concept
  $verses['text'] = '';
}

输出:-

您不认为,您将$obj作为其值进行json_编码的问题没有改变吗?您不认为,您将$obj作为其值进行json_编码的问题没有改变吗?您需要在每个
foreach
赋值中传递引用,否则,您将永远不会对顶级对象进行更改。看到另一个了吗answer@Phil谢谢你指出这一点,我忘了说,解决方案已经更新了。@Phil很棒的观察。@AlivetoDie..:)你需要在每个
foreach
赋值中传递引用,否则你将永远无法更改顶级对象。看到另一个了吗answer@Phil谢谢你指出这一点,我忘了说,解决方案已经更新了。@Phil很好的观察。@AlivetoDie.)