In PHP, for adding a value to (the end of) an array, there’s the array_push function.
But, would it behave like array_append from this code?
function array_append(&$arr,...$vals){
foreach($vals as $val)
$arr[count($arr)]=$val;
}
Also, would count behave like elem_quantity from this code?
function elem_quantity($a,$m=0){
if(!is_array($a)) return 1;
$o=0;
foreach($a as $e)
if($m)
$o+=elem_quantity($a);
else
$o++;
return $o;
}
Thanks for reaching out.
Edit: As @deceze have pointed out:
- (The previous)
array_appendwouldn’t work in some cases. Would thisarray_appendbehave likearray_push?
function array_append(&$arr,...$vals){
$i=0;
foreach($arr as $k => $v)
if(is_numeric($k)&&$k>$i) $i=$k;
foreach($vals as $val)
$arr[$i++]=$val;
}
- Also,
elem_quantityshould pass$mwhen it recursively calls itself, so instead of
$o+=elem_quantity($a);
there should be:
$o+=elem_quantity($a,$m);
or
$o+=elem_quantity($a,1);