-1

我几乎可以将数据添加到我的文件中。但我正在尝试找出确切的语法..任何帮助都非常感谢..

我的 php 脚本

$inp = file_get_contents('jsontrail.json');

$tempArray = json_decode($inp, true);
//adding to the matchid index of matches array
$matchid=3;
$arrne['games']['matches'][$matchid]['winner'] ="winner";
$arrne['games']['matches'][$matchid]['player1id'] ="player1id";
$arrne['games']['matches'][$matchid]['player2id'] ="player2id";
array_push($tempArray,$arrne );
$jsonData = json_encode($tempArray);
file_put_contents('jsontrail.json', $jsonData);
print_r( $inp);

我的有效 json 文件

{
"players": [
    {
        "name": "Moldova",
        "image": "/Images/Moldova.jpg",
        "roll_over_image": "tank.jpg"
    },
    /* repeat */
],
"games": [
    {
        "matches": [
            {
                "player1id": 0,
                "player2id": 1,
                "winner": "*"
            },
            /* repeat */
        ]
    },
    {
        "matches": [
            {
                "player1id": 0,
                "player2id": 1,
                "winner": "*"
            },
            /* repeat */
        ]
    }
]
}

我正在尝试将数据添加到已经存在的数组中,

在游戏->first_match_array->mymatch_id_input->push_data_here 中。

执行后,这就是我在屏幕上得到的

{
    "players":
    [
        {
            "name":"Moldova",
            "image":"\/Images\/Moldova.jpg",
            "roll_over_image":"tank.jpg"
        },
        /* repeat */
    ]
    ,
    "games":
    [
        {
            "matches":
            [
                {
                    "player1id":0,
                    "player2id":1,
                    "winner":"*"
                },
                /* repeat */
            ]
        },
        {
            "matches":
            [
                {
                    "player1id":0,
                    "player2id":1,
                    "winner":"*"
                },
                /* repeat */
            ]
        }
    ],
    "0":
    {
        "games":
        {
            "matches":
            [
                {
                    "3":
                    {
                        "winner":"winner",
                        "player1id":"player1id",
                        "player2id":"player2id"
                    }
                }
            ]
        }
    }
}

上面是用 array_push 得到的。我用 array_merge 得到的下一个代码

{
    "players":
    [
        {
            "name":"Moldova",
            "image":"\/Images\/Moldova.jpg",
            "roll_over_image":"tank.jpg"
        },
        /* repeat */
    ],
    "games":
    [
        {
            "matches":
            [
                {
                    "player1id":0,
                    "player2id":1,
                    "winner":"*"
                },
                /* repeat */
            ]
        },
        {
            "matches":
            [
                {
                    "player1id":0,
                    "player2id":1,
                    "winner":"*"
                },
                /* repeat */
            ]
        }
    ],
    "0":
    {
        "games":
        {
            "matches":
            [
                {
                    "3":
                    {
                        "winner":"winner",
                        "player1id":"player1id",
                        "player2id":"player2id"
                    }
                }
            ]
        }
    },
    "1":
    {
        "games":
        {
            "matches":
            [
                {
                    "3":
                    {
                        "winner":"winner",
                        "player1id":"player1id",
                        "player2id":"player2id"
                    }
                }
            ]
        }
    }
}

请帮忙,看起来很简单,但找不到确切的代码来做这件事..请帮忙..

4

2 回答 2

0

你搞乱了关键顺序,

$arrne['games']['matches'][$matchid]['winner']

应该

$arrne['games'][$matchid]['matches']['winner']

你缺少一个数字索引,

$arrne['games']['matches'][$matchid]['winner']

应该是

$arrne['games'][]['matches'][$matchid]['winner']

或者是

$arrne['games'][$matchid]['matches'][]['winner']

因为在您的原始 JSON 中,您有

"games": [
    {
        "matches": [
        /* ... */
        ]
    },
    {
        "matches": [
        /* ... */
        ]
    }

所以结构是games->[index]->matches->[index],不是games->matches->[index]

另外,简单array_merge是行不通的,你需要array_merge_recursive

示例代码:

$json=<<<JSON
...
JSON;
$tempArray=json_decode($json,true);
print_r($tempArray);
$matches=array(
    "games"=>array(
        3=>array(
            "matches"=>array(
                array(
                    "winner"=>"winner",
                    "player1id"=>"player1id",
                    "player2id"=>"player2id"
                )
            )
        )
    )
);
print_r(array_merge_recursive($tempArray,$matches));

现场演示 现场演示

编辑

$tempArray["games"]并且$tempArray["games"][index]["matches"]都是数字数组,因此array_merge将追加,而不是覆盖。如果需要修改现有值而不是添加新值,直接修改like

$tempArray["games"][index]["matches"][$matchid]["winner"]="winner";
于 2013-04-25T09:52:43.443 回答
0

问题是你格式错误的 json 字符串,它没有正确构造,试试这个

$str = '{
"players": [
{
    "name": "Moldova",
    "image": "/Images/Moldova.jpg",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Georgia",
    "image": "/Images/georgia.gif",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Belarus",
    "image": "/Images/Belarus.gif",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Armenia",
    "image": "/Images/armenia.png",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Kazahstan",
    "image": "/Images/kazahstan.gif",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Russia",
    "image": "/Images/russia.gif",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Ukraine",
    "image": "/Images/ukraine.png",
    "roll_over_image": "tank.jpg"
},
{
    "name": "Uzbekistan",
    "image": "/Images/uzbekistan.gif",
    "roll_over_image": "tank.jpg"
}
],
"games": [
{
    "matches": [
    {
        "player1id": 0,
        "player2id": 1,
        "winner": "*"
    },
    {
        "player1id": 2,
        "player2id": 3,
        "winner": "*"
    },
    {
        "player1id": 4,
        "player2id": 5,
        "winner": "*"
    },
    {
        "player1id": 6,
        "player2id": 7,
        "winner": "*"
    },
    {
        "player1id": 0,
        "player2id": 2,
        "winner": "*"
    },
    {
        "player1id": 4,
        "player2id": 6,
        "winner": "*"
    },
    {
        "player1id": 0,
        "player2id": 4,
        "winner": 4
    }
    ]
},
{
    "matches": [
    {
        "player1id": 0,
        "player2id": 1,
        "winner": "*"
    },
    {
        "player1id": 2,
        "player2id": 3,
        "winner": "*"
    },
    {
        "player1id": 4,
        "player2id": 5,
        "winner": "*"
    },
    {
        "player1id": 6,
        "player2id": 7,
        "winner": "*"
    },
    {
        "player1id": 0,
        "player2id": 2,
        "winner": "*"
    },
    {
        "player1id": 4,
        "player2id": 6,
        "winner": "*"
    },
    {
        "player1id": 0,
        "player2id": 4,
        "winner": 0
    }
    ]
}]}';

如果您使用它,它将起作用。

于 2013-04-25T09:16:27.107 回答