Take the 2-minute tour ×

I have an array like this:

    [0] => Array
            [id] => 68
            [type] => onetype
            [type_id] => 131
            [name] => name1

    [1] => Array
            [id] => 32
            [type] => anothertype
            [type_id] => 101
            [name] => name2

I need to remove some arrays from it if the users has permissions or not to see that kind of type. I am thinking on doing it with a for each, and do the needed ifs inside it to remove or let it as it.

My question is: What's the most efficent way to do this? The array will have no more than 100 records. But several users will request it and do the filtering over and over.

share|improve this question
You want to remove one or a group of arrays ??? –  Baba Nov 1 '12 at 23:36
Could you give us an example of the permissions boolean logic please? –  Flosculus Nov 1 '12 at 23:36
What are the removal criteria? You can use a loop, or perhaps array_filter() with a custom callback function. –  Michael Berkowski Nov 1 '12 at 23:36
Use a temporary array to store the filtered results –  craig1231 Nov 1 '12 at 23:37
It's also worth telling us where your array's coming from - if it's coming out of a database then that's the obvious place for optimisation, and if not, there may be other options depending on where exactly the data originates –  Joe Nov 1 '12 at 23:43
add comment

migrated from serverfault.com Nov 1 '12 at 23:34


3 Answers

up vote 0 down vote accepted

use this 1 simple and easy

foreach ($display_related_tags as $key => $tag_name) {
    if($tag_name == $found_tag['name']) {
share|improve this answer
Thanks for all the comments. This is the way I was thinking of using, and after some small research using the suggestions on the comments I choose this way. –  user104531 Nov 2 '12 at 14:28
add comment

Use in_array() function so that you could find the array that you would want to remove.

Then use unset() function to unset the array or variable that you would want to remove from your existing array.

On this way, you don't need to loop your array over and over.

share|improve this answer
add comment

I think you understand the basics of PHP and stripping the array.

What you could do after stripping the array store it in a session for re-use after a page-refresh or loading of a different page. That way, you only have to do it once.

See: http://www.php.net/manual/en/function.session-start.php

share|improve this answer
add comment

Your Answer


