我一直在阅读几个问题和答案,但我找不到解决问题的方法。我正在尝试更新CMS中生成的json文件,并将其存储在mysql中以供计数器使用。是一个多维数组,我需要更新的部分是key => 15下的这个嵌套部分:
array (size=39)
'admin_label' => string 'C04' (length=3)
'number' => string '194' (length=3)
'number_addtext' => string '+' (length=1)
'duration' => string '2400' (length=4)
'counter_title' => string 'PAGES ACTIVE' (length=14)
所以我只是json解码它并以这种方式更新值:
$json[15]['columns'][1]['addons'][2]['settings']['number'] = $val1;
$json[15]['columns'][2]['addons'][2]['settings']['number'] = $val2;
$json[15]['columns'][3]['addons'][2]['settings']['number'] = $val3;
$json[15]['columns'][4]['addons'][2]['settings']['number'] = $val4;
然后我编码并更新数据库。但是如果父键发生更改,脚本将无法正常工作。
我想找到一种方法来搜索价值' C04'作为参考或甚至计数器标题' PAGES ACTIVE'然后更新密钥'数字'的值那个嵌套数组,这种方式,如果父键更改脚本将工作正常。这可能吗?像这样:
function findarray($item, $key)
{
if($key == 'admin_label' && $item == 'C02') {
// find next key named number and update that value
} else if($key == 'admin_label' && $item == 'C03') {
// find next key named number and update that value
} else if($key == 'admin_label' && $item == 'C04') {
// find next key named number and update that value
}
}
array_walk_recursive($json, 'findarray');
最好的问候
答案 0 :(得分:1)
根据您的JSON结构您可以尝试这样的事情:
CREATE TABLE Table
(
id VARCHAR(100) PRIMARY KEY NOT NULL,
text VARCHAR(100),
link VARCHAR(254) DEFAULT "",
torf BOOLEAN NOT NULL DEFAULT 0
);