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);
}
5
  • use unset(array[index]) Commented Feb 13, 2018 at 6:40
  • Use array_splice() Commented Feb 13, 2018 at 6:41
  • I've added the function I am using to add to my JSON file. How might either of these methods apply to this? Commented Feb 13, 2018 at 6:42
  • You can also use array_filter() to remove all elements that don't pass a condition. Commented Feb 13, 2018 at 6:43
  • Check this post: stackoverflow.com/questions/6661530/… Commented Feb 13, 2018 at 6:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
No, then you need to use something like array_filter to search and remove all matches in the array.
Can it be ["c", "d"]?
It could even be [ ["c", "a", "b"], ["c"], [ ], ["c", "d"], ["d"], ["e"] ] which would need to become [ ["a", "b"], [ ], [ ], ["d"], ["d"], ["e"] ].
"c" is always first in that example, but I'm guessing that's not guaranteed, either. See my answer using array_filter.
|
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.