我正在循环PHP数组,结果如下:
david
darwin
sara
我想用这些结果作为数组参数调用一个函数。必须是这样的:
$det = $obj->getDetails(array(
'david',
'darwin',
'sara',
));
我该怎么做?
答案 0 :(得分:2)
答案1:
<?php
// If you have a string.
$string = 'david darwin sara';
$array = explode(' ', $string);
$det= $obj->getDetails($array);
答案2:
<?php
// If you have an array at the beginning and let's say
// your array is like this.
$users = array(
array('name' => 'david', 'age' => 35, 'something' => ....),
array('name' => 'darwin', 'age' => 30, 'something' => ....),
array('name' => 'sara', 'age' => 45, 'something' => ....)
);
$array = [];
foreach($users as $item){
$array[] = $item['name'];
}
$det = $obj->getDetails($array);
答案 1 :(得分:-1)
可能类似以下内容:
$det = array();
foreach ($results as $key => $name) {
$det[$key] = $obj->getDetails($name);
}