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

StefanK
  • 23
  • 6
  • 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. – tadman Feb 06 '18 at 17:08
  • 1
    What "nesting loop"? If you're having trouble with that, please include that code as well. – tadman Feb 06 '18 at 17:09
  • I did start to code based on: https://stackoverflow.com/questions/2304570/how-to-delete-object-from-array-inside-foreach-loop I don't know, if this would even be the right way ... – StefanK Feb 06 '18 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. – tadman Feb 06 '18 at 17:13

3 Answers3

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));
Marco
  • 7,007
  • 2
  • 19
  • 49
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']);
Martin
  • 22,212
  • 11
  • 70
  • 132
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']);
});
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • 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! – StefanK Feb 06 '18 at 17:36