-2

I've a multidimensionnal array like that, and I wan't to check if all of the "open_at" and "closed_at" values are NULL.

array:7 [▼
0 => array:2 [▼
 0 => array:2 [▼
  "open_at" => null
   "closed_at" => "11:03"
 ]
 1 => array:2 [▼
  "open_at" => "13:00"
  "closed_at" => "16:00"
 ]
]
1 => array:2 [▼
 0 => array:2 [▼
  "open_at" => "09:00"
  "closed_at" => "12:00"
 ]
 1 => array:2 [▼
   "open_at" => "12:30"
   "closed_at" => "17:00"
 ]
]
2 => array:2 [▼
0 => array:2 [▼
  "open_at" => "08:00"
  "closed_at" => "18:00"
]
1 => array:2 [▼
  "open_at" => null
  "closed_at" => null
]
]
3 => array:2 [▼
 0 => array:2 [▼
   "open_at" => null
   "closed_at" => null
 ]
 1 => array:2 [▼
   "open_at" => null
   "closed_at" => null
 ]
]
...

I've tried with multiple for and foreach loop like, with no success...

for ( $i = 0; $i <6 ; $i++) {
  for ($j = 0; $j < 2; $j++) {
        if(empty($hours[$i][$j]["open_at"])){
            $null="complete";
        }
        else{
            $null="empty";
        }
        return $null;
       }

    }

The array should be checked as empty only if all the "open_at" and "closed_at" values are set to NULL. As saw in the example above, the first values can be set to NULL, but the array should not be checked as empty in that case.

The goal is to don't execute the code bellow only if all the "open_at" and "closed_at" are set to NULL.

$hours = $request->get('hours');  

//check if empty here

foreach ($hours as $key => $period) {
foreach($period as $attribute => $value){
    $shops_hour = new Shops_hour();
    $shops_hour->shop_id=$shop->id;
    $shops_hour->day=$key;
    $shops_hour->period=$attribute;
    $shops_hour->open_at=$hours[$key][$attribute]["open_at"];
    $shops_hour->closed_at=$hours[$key][$attribute]["closed_at"];
    $shops_hour->save();
        }
    }

Thank you in advance,

Newo1t
  • 1
  • 1
  • This I am afraid is a specification and not a question. We help to fix code, we dont write it for you – RiggsFolly Jun 22 '18 at 15:13
  • loop through all items, set a flag `$foundOneWithaValue` if you did, then return that (or the opposite) _after_ the loop. – Jeff Jun 22 '18 at 15:16
  • You might want to look at PHP's [RecursiveArrayIterator](http://php.net/manual/en/class.recursivearrayiterator.php) – CD001 Jun 22 '18 at 15:44

2 Answers2

1

Using a recursive function that will return true if all values contained are null:

function all_null_recursive($arr)
{
    foreach ($arr as $item) {

        /* if the item is an array
           and the function itself found something different from null */
        if (is_array($item) && all_null_recursive($item) === false) {
            return false;

        // if the item is not an array and different from null
        } elseif (!is_array($item) && $item !== null) {
            return false;
        }
    }

    // always found null, everything's good
    return true;
}

Testing:
The 2 arrays to test with.

$foo = [
    0 => [
        0 => [
            "open_at" => null,
            "closed_at" => "11:03"
        ],
        1 => [
            "open_at" => "13:00",
            "closed_at" => "16:00"
        ],
    ],
    1 => [
        0 => [
            "open_at" => "09:00",
            "closed_at" => "12:00"
        ],
        1 => [
            "open_at" => "12:30",
            "closed_at" => "17:00"
        ],
    ],
    2 => [
        0 => [
            "open_at" => "08:00",
            "closed_at" => "18:00"
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    3 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ]
    ]
];

$foo_2 = [
    0 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    1 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    2 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    3 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ]
    ]
];

Testing:

var_dump(all_null_recursive($foo));    // bool(false) 
var_dump(all_null_recursive($foo_2));  // bool(true)
AymDev
  • 6,626
  • 4
  • 29
  • 52
0

The @jeff method work, thank you

        $foundOneWithaValue = "0";
    foreach ($hours as $key => $period) {
       foreach($period as $attribute => $value){
            if(!empty($hours[$key][$attribute]["open_at"]) || (!empty($hours[$key][$attribute]["closed_at"])) ) {
                $foundOneWithaValue ++;
            }
        }
    }  

    if($foundOneWithaValue != 0)
    {
        foreach ($hours as $key => $period) {
           foreach($period as $attribute => $value){
                $shops_hour = new Shops_hour();
                $shops_hour->shop_id=$shop->id;
                $shops_hour->day=$key;
                $shops_hour->period=$attribute;
                $shops_hour->open_at=$hours[$key][$attribute]["open_at"];
                $shops_hour->closed_at=$hours[$key][$attribute]["closed_at"];
                $shops_hour->save();
            }
        }
    }
Newo1t
  • 1
  • 1
  • you can/should change `$foundOneWithaValue="0"` to `... = false`, then instead of `++` -> `= true`, then just remove the `!=0`. The first one is a string, not the number 0. It works out in this situation, but wouldn't if you'd start with "1". – Jeff Jun 22 '18 at 17:53