-1

I was wondering if theres a way to filter arrays in PHP via doing something like this:

if (!in_array("something",$something)

Now I'd like to get all the values that are NOT matched by the search criterion.

For instance if I have an array like this:

$options = array("Preview", "Live", "Completed");

and I pass the value for search: "Preview", I'd like to return these two elements in the array "Live" and "Completed" as a result of my array filtering...

Is there some way I could do this?

Edit:

We have an initial array like this:

$options = array("Preview", "Live", "Completed");

Initial input to the search of array is "Preview" string.

Output array that I'd like it to be is:

$newArray = array("Live", "Completed");

EDIT EDIT (WHAT IM ACTUALLY TRYING TO ACHIEVE):

Hi guys this is what I'm actually trying to do:

<?php foreach($active_brand as $brand?>
 <select>
                            <?php if (in_array($brand['State'], $options)) { ?>
                                <option selected="selected"  value="<?=$brand['id']?>"><?=$brand['State']?></option>
                            <?php }?>
                            <?php for($i=1;$i<count($options);$i++) { ?>
                                <option  value="<?=$brand['id']?>"><?=array_diff($options, array($brand['State']))[$i]?></option>
                            <?php }?>
                        </select>
<?php }?>

As you can see guys, I have a column in my DB which is Called a "State"... It's basically a varchar where I keep my string stored... Brand state can only be the 3 following states:

Preview => Live => Completed

The first thing I do is set the current value to select list like following:

<?php if (in_array($brand['State'], $options)) { ?>
                                    <option selected="selected"  value="<?=$brand['id']?>"><?=$brand['State']?></option>
                                <?php }?>

Now I have defined an array like this in my phtml file like this:

$options = array("Preview", "Live", "Completed");

Now I'm simply trying to filter out the states that are not in the array and place them in my select tag like this:

<?php for($i=1;$i<count($options);$i++) { ?>
                            <option  value="<?=$brand['id']?>"><?=array_diff($options, array($brand['State']))[$i]?></option>
                        <?php }?>

So that the structure in my select list is like following:

Current state of brand => defaultly selected => add the two missing states to select list (but not to add the current one that is set in DB)...

Can someone help me out?

4
  • Can you please add "example input" and "example output"? Commented Jun 3, 2016 at 7:51
  • Hi Will I edited my question , can you look into it now ? Commented Jun 3, 2016 at 7:53
  • Please show us what you already tried and what is not working Commented Jun 3, 2016 at 8:08
  • @AlexandreCartapanis I've edited my question, can you look into it? Commented Jun 3, 2016 at 8:26

3 Answers 3

2

You can use array_diff:

$notIn = array_diff($options, array('Preview'));

Nice thing about it, is that you can filter with multiple values if you need.

Update:

I think, a little bit would be more clear to do the following:

<?php foreach($active_brand as $brand) { ?>
<select>
    <?php foreach($options as $option) { ?>
        <option <?php if($brand['State'] == $option) { ?>selected="selected"<?php } ?> value="<?=$brand['id']?>"><?php echo $option; ?></option>
    <?php } ?>
</select>
<?php } ?>
Sign up to request clarification or add additional context in comments.

3 Comments

is there any way I can get a single value thats NOT matched by the search criterion while doing a for loop at the same time ? This seems like a really good solution, just missing what I wrote here... Could this be done ?
Can you give some more details what you need. I don't quite understand. Also, update your question with that info, because the code above does exactly what you have described: $options = array("Preview", "Live", "Completed"); -> $newArray = array("Live", "Completed"); with search critera "Preview"
Hi I've edited my question... Can you look into it please?
0

Try:

function removeAllValuesMatching(array $arr, $value) {
    $keys = array_keys($arr, $value);
    foreach ($keys as $key) {
        unset($arr[$key]);
    }
    return $arr;
}

$options = array("Preview", "Live", "Completed");

removeAllValuesMatching($options, "Preview"); // pass array and value to match

Comments

0

You can use array_filter()

$array = array('Preview', 'Live', 'Completed');
$blacklist = 'Preview';
$result = array_filter($array, function($value) use ($blacklist) {
    if($value !== $blacklist){
        return $value;
    }
});
var_dump($result);
exit;

You can adjust it, so $blacklist can be an array if you need to.

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.