I use the following code to fill all empty keys in sub-arrays with ``:
$array = array(
'note' => array('test', 'test1'),
'year' => array('2011','2010', '2012'),
'type' => array('conference', 'journal', 'conference'),
);
foreach ($array['type'] as $k => $v) {
foreach($array as $element => $a) {
$iterator = $array[$element];
if(!isset($iterator[$k])){
$iterator[$key] = '';
}
}
}
print_r($array);
The problem is that it is not actually changing the elements in $array but in temporary variable $iterator.
I know that this is a simple question but I would like to find out the best and fastest solution.
You don’t need the $iterator variable, you can do just:
I would also recommending switching the inner and outer loops, so it’s more readable and more efficient.