Reputation: 2651
I have this function below:
public function set_partial($array)
{
if (is_array($array)) {
foreach ($array as $each) {
self::$_partials[$each[0]] = array('view' => $each[1], 'data' => $each[2]);
}
}
}
In self::$_partials, 'data' isn't required. So how do I keep my code simple while allowing data to be null? Right now, if data isn't provided, then I get an offset error.
Upvotes: 0
Views: 21
Reputation: 17598
If you want to avoid offset errors, you can do something like this:
public function set_partial($array)
{
if (is_array($array)) {
foreach ($array as $each) {
$view = !empty($each[1]) ? $each[1] : ''; // replace '' with whatever default value you want to use
$data = !empty($each[2]) ? $each[2] : '';
self::$_partials[$each[0]] = array('view' => $view, 'data' => $data);
}
}
}
Upvotes: 0
Reputation: 16828
You can check to see if each[2]
isset. If it is, then set the variable, otherwise make it null:
<?php
public function set_partial($array){
if(is_array($array)){
foreach ($array as $each) {
self::$_partials[$each[0]] = array('view' => $each[1], 'data' => (isset($each[2])?$each[2]:NULL));
}
}
}?>
Upvotes: 1