Php 如何数组列出未知数量的值
我想这样做:Php 如何数组列出未知数量的值,php,Php,我想这样做: list($a, $b, $c) = array('a', 'b', 'c'); my_function($a, $b, $c); 但数组中的值数目未知 my_function(some_function($array)); 有什么想法吗?如果将数组作为参数传递给函数,则可以使用foreach循环迭代任意长度的数组 someFunction($returnedArray){ foreach($returnedArray as $value){ echo $value
list($a, $b, $c) = array('a', 'b', 'c');
my_function($a, $b, $c);
但数组中的值数目未知
my_function(some_function($array));
有什么想法吗?如果将数组作为参数传递给函数,则可以使用foreach循环迭代任意长度的数组
someFunction($returnedArray){
foreach($returnedArray as $value){
echo $value
}
}
foreach将逐个元素,将当前索引的值分配给本例中的$value。您可以将数组传递给您的函数,这样您就可以获得所有变量。如果阵列具有键,则可以使用 另一个更常见的选择是在数组中循环。使用数组,可以一次引用一个数组的每个值。可以这样做:
// Build data array. Include keys as these will become the variable name later.
$data = array('a','b','c');
// Call your function with data
my_function($data);
// Your function to parse data...
function my_function($data = NULL)
{
// Verify you got data..
if(is_null($data)){ return FALSE; }
// Loop through data to operate
foreach($data as $item)
{
// Now you may echo values
echo $item;
}
}
只是不要使用列表功能
将数组传递给函数。我的名字是A,b,c,。。。。如果使用哈希数组,效果会更好。而且提取也不是一个好主意
请看:而且我认为Machavity是正确的,但是你也可以看看重复的call\u user\u func\u数组是正确的答案,而且这个问题比我的解释更好!我对此投了反对票,所以我觉得有义务说出原因。仅仅说提取不是一个好主意,用户就可以搜索并找到这个线程,而不必解释为什么他们不应该使用它。相反,考虑解释提取和/或不提供与其他人相同答案的风险。1。2.可以吗?
// Build data array. Include keys as these will become the variable name later.
$data = array('a','b','c');
// Call your function with data
my_function($data);
// Your function to parse data...
function my_function($data = NULL)
{
// Verify you got data..
if(is_null($data)){ return FALSE; }
// Loop through data to operate
foreach($data as $item)
{
// Now you may echo values
echo $item;
}
}