如何访问存储数组的两个全局变量?我想同时输出两个数组。
应该是"12345";然后是";abcde">
$number = [1,2,3,4,5];
$str = ["a", "b", "c", "d", "e"];
function twoMassive(){
$names = ["number", "str"];
foreach($names as $items){
yield $GLOBALS[$items]; // according to the plan, the elements of the $number
// array should come out first, and then $str
}
};
foreach(twoMassive() as $items){
echo $items;
}
您可以使用yield from
从数组中生成所有值:
<?php
$number = [1,2,3,4,5];
$str = ["a", "b", "c", "d", "e"];
function twoMassive(){
$names = ["number", "str"];
foreach($names as $items){
yield from $GLOBALS[$items]; // according to the plan, the elements of the $number
// array should come out first, and then $str
}
};
foreach(twoMassive() as $items){
echo $items;
}
示例