0

I am brand new to php.I have found questions that show how to remove key/value pairs from JSON files with php, but not array indexes.

I have worked out how to append values to arrays in a JSON file with json_decode(). But not how to remove values. I need to produce a function() that hunts for c and removes any value within an array in my JSON file. Below is a before and after of the expected outcome I need to produce with my php file.

// before
[["a", "c", "b"], ["c", "c"], [], ["c", "d"], ["d"], ["e"]]
// after
[["a", "b"], [], [], ["d"], ["d"], ["e"]]

Below is the function I have produced in order to add values to arrays in my JSON if this helps provide more context:

function appendClient($file, $post, $client) {
    $a = fopen($file, "r");
    $json = json_decode(fread($a, filesize($file)));
    $json[$post][] = $client;
    fclose($a);
    $a = fopen($file, "w");
    fwrite($a, json_encode($json));
    fclose($a);
}
DanMad
  • 1,643
  • 4
  • 23
  • 58

2 Answers2

1

Use array_filter

function removeClient($file, $post, $client) {
    $json = json_decode(file_get_contents($file));
    $json[$post] = array_filter($json[$post], function($x) use($client) {
        return $x != $client;
    });
    file_put_contents($file, json_encode($json));
}

This assumes all the elements of the array are either empty arrays or 1-element arrays containing the client name, as in the example you showed.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for this. The arrays aren't 1-element though. Admittedly they are in the example. Perhaps I could have been more clear. Will edit my OP. Presumably if I remove the 0 from your condition this should resolve the 1-element issue? – DanMad Feb 13 '18 at 07:22
  • No, then you need to use something like `array_filter` to search and remove all matches in the array. – Barmar Feb 13 '18 at 07:23
  • Can it be `["c", "d"]`? – Barmar Feb 13 '18 at 07:24
  • It could even be `[ ["c", "a", "b"], ["c"], [ ], ["c", "d"], ["d"], ["e"] ]` which would need to become `[ ["a", "b"], [ ], [ ], ["d"], ["d"], ["e"] ]`. – DanMad Feb 13 '18 at 07:27
  • `"c"` is always first in that example, but I'm guessing that's not guaranteed, either. See my answer using `array_filter`. – Barmar Feb 13 '18 at 07:30
  • This has done it! Thanks @Barmar. Also, Yes, `"c"` preceding all other elements in my arrays was coincidental and highlights I didn't think my example through well enough. Apologies. – DanMad Feb 13 '18 at 07:38
0

Take a look at array_filter and array_values functions.

[["a"],[],["b"],["c"]]

From the above input, I am assuming you are working with 2d array. Then, you can use the following function to do the job:

function removeValues($array, $value) {
    $result = [];

    foreach ($array as $row) {
        $filtered = array_filter($row, function($entry) use($value) {
            return $entry != $value;
        });

        // If you need to reset keys
        $filtered = array_values($filtered);

        $result[] = $filtered;
    }

    return $result;
}

Example:

$input  = [["a"],[],["b"],["c"]];
$output = removeValues($input, "c");
print_r($output);
Jomoos
  • 12,823
  • 10
  • 55
  • 92