如何将数组结果作为函数的数组参数插入?

时间:2018-05-07 11:24:13

标签: php

我正在循环PHP数组,结果如下:

david
darwin
sara

我想用这些结果作为数组参数调用一个函数。必须是这样的:

$det = $obj->getDetails(array(
    'david',
    'darwin',
    'sara',
));

我该怎么做?

2 个答案:

答案 0 :(得分:2)

答案1:

<?php
    // If you have a string.
    $string = 'david darwin sara';
    $array = explode(' ', $string);
    $det= $obj->getDetails($array);

数组输出:https://eval.in/1000612

答案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);

数组输出:https://eval.in/1000614

答案 1 :(得分:-1)

可能类似以下内容:

$det = array();
foreach ($results as $key => $name) {
  $det[$key] = $obj->getDetails($name);
}