Question

I'm looping through a bunch of data and I seem to be doing something that feels a bit repetitive.

if(!isset($productitems[$stop->route_id])){
    $productitems[$stop->route_id] = [];
}
if(!isset($productitems[$stop->route_id][$location_id])){
    $productitems[$stop->route_id][$location_id] = [];
}
if(!isset($productitems[$stop->route_id][$location_id][$week])){
    $productitems[$stop->route_id][$location_id][$week] = [];
}
if(!isset($productitems[$stop->route_id][$location_id][$week][$day])){
    $productitems[$stop->route_id][$location_id][$week][$day] = [];
}
if(!isset($productitems[$stop->route_id][$location_id][$week][$day][$task->product_id])){
    $productitems[$stop->route_id][$location_id][$week][$day][$task->product_id] = [];
}
if(!isset($productitems[$stop->route_id][$location_id][$week][$day][$task->product_id][(int)$task->refill_id])){
    $productitems[$stop->route_id][$location_id][$week][$day][$task->product_id][(int)$task->refill_id] = 0;
}

Is there a different approach to filling these multidimensional arrays without all the isset checks?

edit I know that just setting $productitems[$stop->route_id][$location_id][$week][$day][$task->product_id][(int)$task->refill_id] in php is possible, however php logs a warning and the project that I am working on uses Laravel which will throw an exception.

Was it helpful?

Solution

PHP automatically creates sub-keys if they don't exist (and you can avoid the notice by checking isset). If you wish, feel free to create a function that does it for you (minimizes the double-pasting of variables.

Code:

<?php
    error_reporting(E_ALL);

    function setDefault(&$variable, $default) {
        if (!isset($variable)) {
            $variable = $default;
        }
    }

    $foo = array(
        'foo' => 'oof'
    );

    setDefault($foo['sub']['arrays']['are']['pretty']['cool'], 0);

    print_r($foo);
?>

Outputs:

Array
(
    [foo] => oof
    [sub] => Array
        (
            [arrays] => Array
                (
                    [are] => Array
                        (
                            [pretty] => Array
                                (
                                    [cool] => 0
                                )

                        )

                )

        )

)

DEMO

3v4l shows that there's no notice in any PHP version from 4.3.0 to 5.5.6 compared to this that clearly spits out a notice.

If you don't want to use a function, feel free to just use the last if condition in your code:

<?php
    error_reporting(E_ALL);

    $foo = array(
        'foo' => 'oof'
    );

    if (!isset($foo['sub']['arrays']['are']['pretty']['cool'])) {
        $foo['sub']['arrays']['are']['pretty']['cool'] = 0;
    }

    print_r($foo);
?>

3v4l demo

OTHER TIPS

Well php does not require everything to be declared so i would say you don't need any of that. I just tried it on my local machine in command prompt and this is the output...

CODE:

<?php

    $productitems['a']['b']['c']['d']['e']['f'] = 65;
    var_dump($productitems);
?>

OUTPUT:

array(1) {
  ["a"]=>
  array(1) {
    ["b"]=>
    array(1) {
      ["c"]=>
      array(1) {
        ["d"]=>
        array(1) {
          ["e"]=>
          array(1) {
            ["f"]=>
            int(65)
          }
        }
      }
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top