0

I get some data from the database, which is saved in "$Data". $Data looks like this:

        [Data] => Array
            (
                [0] => Array
                    (
                        [PrinterID] => 3
                        [PrinterName] => PRT03_EDV
                        [isDefaultPrinter] => 1
                        [isMapped] => 0
                    )

                [1] => Array
                    (
                        [PrinterID] => 1
                        [PrinterName] => PRT01_Zentral
                        [isDefaultPrinter] => 0
                        [isMapped] => 1
                    )

                [2] => Array
                    (
                        [PrinterID] => 2
                        [PrinterName] => PRT02_BH
                        [isDefaultPrinter] => 0
                        [isMapped] => 0
                    )

I need to verify, that there is no array in $Data, where "isDefaultPrinter == True" and "isMapped == False". Programatically:

if ( $Data["isDefaultPrinter"] == true and $Data["isMapped"] == false ) {
  // Remove from Array
}

I did start to code this on my own based on this and my result was a terrible looking nested loop, which did not work :-(
I am a beginner and I wanted to ask, if there is an nice and easy way to do this?

Thank you

4
  • Before you develop some really bad habits, please, please use lower case for things like if, and, and true. Although PHP is case insensitive, this isn't standard and looks really broken. Commented Feb 6, 2018 at 17:08
  • 1
    What "nesting loop"? If you're having trouble with that, please include that code as well. Commented Feb 6, 2018 at 17:09
  • I did start to code based on: stackoverflow.com/questions/2304570/… I don't know, if this would even be the right way ... Commented Feb 6, 2018 at 17:12
  • Do try and keep your questions self-contained. Not everyone is prepared to open a bunch of tabs to try and understand all the parts in your question. It helps especially for those on mobile where they can't really juggle sources. Commented Feb 6, 2018 at 17:13

3 Answers 3

1

Here's a version using array_filter, which is sort of built for this. See http://php.net/manual/en/function.array-filter.php

$valid = array_filter($Data, function($var) {
    return !($var['isDefaultPrinter'] && !$var['isMapped']);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I am sure the other answers work as well, but I like this most. It seems to be the thing I was looking for and keeps the code clean. Many thanks!
1

Use foreach to loop over the data array:

foreach ($Data['Data'] as $entry) {
    // Examine every entry
    if ($entry['isDefaultPrinter'] && !$entry['isMapped']) {
        // $entry does not meet criteria
    }
}

You can write a function that verifies that every entry meets your criteria:

function validateData($Data) {
    foreach ($Data['Data'] as $entry) {
        // Examine every entry
        if ($entry['isDefaultPrinter'] && !$entry['isMapped']) {
            // $entry does not meet criteria
            return false;
        }
    }

    // Everything is OK
    return true;
}

var_dump(validateData($Data));

Comments

1

You can use unset to remove the row of the array that doesn't fit your "keep" criteria. You need to know the array key to remove it tidily; this foreach with the $key value as well:

foreach ($var['Data'] as $key => $entry) {
    // Examine every entry
    if ($entry['isDefaultPrinter'] && !$entry['isMapped']) {
        // $entry does not meet criteria
        unset($var['Data'][$key]);
    }
}

print_r($var); //output remaining values. 

Once completed - and if you wish - you can then reindex the [outer] array using array_values():

$var['Data'] = array_values($var['Data']);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.