I’m getting an error when using a variable reference. Am I missing something obvious?
basically…
$required = array();
$optional = array();
foreach($things as $thing){
$list =& $thing->required ? $required : $optional;
$list[] = $thing;
}
(looping thru list of things, if the thing’s required property is true, pass that thing to the list of required things, other pass it to the list of optional things…)
tyia
From the looks of it, it seems like you’re trying to separate things that are required or optional into different arrays.
If you insist on doing it on a single line, you could do this:
The problem with your code is
$list =& $thing->required ? $required : $optional;. PHP is ignoring the? $required : $optionalpart is assigning $this->required to$list. When you try to add to the array on the following it,$listis a scalar and no longer an array so it’s failing. The only way that I can think to solve that problem is to go with one of the solutions above or to create function that return the array by reference.Reference: From http://php.net/manual/en/language.operators.comparison.php: