从JSON(PHP)中删除项目

时间:2018-02-09 10:15:16

标签: php json

我查看了很多帖子,用PHP从JSON中删除了一个项目,但不知何故,'unset'函数在我的代码中的任何地方都无效。 如何从我的JSON文件中删除'SPEC_Info'并使用我创建的代码来合并不同的JSON文件(URL)?

$sets = array();
foreach ($urls as $url)
{
    $json = file_get_contents($url);
    $data = json_decode($json);
    $json = substr($json, 1, -1);
    $sets = array_merge($sets, $data);
}

$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

$fp = fopen('dsales_watchjewel_combined_en.json', 'w');
fwrite($fp, $new);
fclose($fp);

3 个答案:

答案 0 :(得分:1)

$sets = array();
foreach ($urls as $url)
{
    $json = file_get_contents($url);
    $decoded = json_decode($json, true);
    oreach ($decoded as $key => $innerObject) { //based on the fact that your JSON contains multiple inner objects.
        if(isset($innerObject['SPEC_Info'])) {
            unset($decoded[$key]['SPEC_Info']);
        }
    }
    $sets[] = $decoded;
}
$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

$fp = fopen('combined.json', 'w');
fwrite($fp, $new);
close($fp);

答案 1 :(得分:0)

JSON是文字。也是$json

的内容

评估表达式$json['SPEC_Info']会触发警告(您不能在字符串中使用字符串偏移),将'SPEC_Info'转换为数字(0)并访问{{1}的第一个字符}。

unset()不能与$json一起使用。它没有任何意义。

JSON是某些数据结构的文本表示。为了使用它,您必须将JSON解码回原始数据结构。 PHP为此目的提供json_decode()。传递$json['SPEC_Info']作为其第二个参数,要求它返回数组,而不是对象(它们更容易操作)。

此答案仅处理&#34;为什么unset()不起作用&#34;部分问题。请参阅@Auris提供的this answer,了解如何正确处理JSON。

答案 2 :(得分:0)

这是我的最终代码,它结合了我的JSON文件+删除了多个项目。

$urls = array
( 'URL'
, 'URL'
);

$sets = array();
foreach ($urls as $url)
{
    $json = file_get_contents($url);
    $data = json_decode($json, true);
    foreach ($data as $key => $innerObject) { //based on the fact that your JSON contains multiple inner objects.
        if (isset(
            $innerObject['SPEC_Bullet point 1'],
            $innerObject['SPEC_Bullet point 2'],
            $innerObject['SPEC_Bullet point 3'],
            $innerObject['SPEC_Bullet point 4'],
            $innerObject['SPEC_Bullet point 5'],
            $innerObject['SPEC_Extra info'],
            $innerObject['SPEC_Keywords'],
            $innerObject['SPEC_Featured'],
            $innerObject['SPEC_Bullet point 1 TEXT'],
            $innerObject['SPEC_Bullet point 2 LIST'],
            $innerObject['SPEC_Bullet Point 1'],
            $innerObject['SPEC_Bullet point 2 TEXT'],
            $innerObject['SPEC_Bullet Point 1 TEXT'],
            $innerObject['SPEC_Bullet Point 2'],
            $innerObject['SPEC_Bullet point 3 LIST'],
            $innerObject['SPEC_Bullet Point 2 TEXT'],
            $innerObject['SPEC_Bullet point 3 TEXT'],
            $innerObject['SPEC_Bullet point 4 LIST'],
            $innerObject['SPEC_Bullet point 4 TEXT'],
            $innerObject['SPEC_Bullet Point 3'],
            $innerObject['SPEC_Bullet Point 3 TEXT'],
            $innerObject['SPEC_Bullet point 5 LIST'],
            $innerObject['SPEC_Bullet point 5 TEXT'],
            $innerObject['SPEC_Bullet Point 4 TEXT'],
            $innerObject['SPEC_Bullet Point 5'],
            $innerObject['SPEC_Bullet Point 5 TEXT'],
            $innerObject['SPEC_Bullet point 1 LIST']
        )) {
            unset(
                $data[$key]['SPEC_Bullet point 1'],
                $data[$key]['SPEC_Bullet point 2'],
                $data[$key]['SPEC_Bullet point 3'],
                $data[$key]['SPEC_Bullet point 4'],
                $data[$key]['SPEC_Bullet point 5'],
                $data[$key]['SPEC_Extra info'],
                $data[$key]['SPEC_Keywords'],
                $data[$key]['SPEC_Featured'],
                $data[$key]['SPEC_Bullet point 1 TEXT'],
                $data[$key]['SPEC_Bullet point 2 LIST'],
                $data[$key]['SPEC_Bullet Point 1'],
                $data[$key]['SPEC_Bullet point 2 TEXT'],
                $data[$key]['SPEC_Bullet Point 1 TEXT'],
                $data[$key]['SPEC_Bullet Point 2'],
                $data[$key]['SPEC_Bullet point 3 LIST'],
                $data[$key]['SPEC_Bullet Point 2 TEXT'],
                $data[$key]['SPEC_Bullet point 3 TEXT'],
                $data[$key]['SPEC_Bullet point 4 LIST'],
                $data[$key]['SPEC_Bullet point 4 TEXT'],
                $data[$key]['SPEC_Bullet Point 3'],
                $data[$key]['SPEC_Bullet Point 3 TEXT'],
                $data[$key]['SPEC_Bullet point 5 LIST'],
                $data[$key]['SPEC_Bullet point 5 TEXT'],
                $data[$key]['SPEC_Bullet Point 4 TEXT'],
                $data[$key]['SPEC_Bullet Point 5'],
                $data[$key]['SPEC_Bullet Point 5 TEXT'],
                $data[$key]['SPEC_Bullet point 1 LIST']
            );
        }
    }
    $json = substr($json, 1, -1);
    $sets = array_merge($sets, $data);
}

$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

$fp = fopen('combined.json', 'w');
fwrite($fp, $new);
fclose($fp);