PHP 有没有办法添加从数组内部调用函数的元素


PHP is there a way to add elements calling a function from inside of array

我想从调用函数的数组内部添加多个值:

$header = array(
  'name',
  'surname',
  _create_days(),
  'total'
);

所以输出是这样的

$header = array(
  'name',
  'surname',
  '2016-01-01',
  '2016-01-02',
  ....
  'total'
);

我尝试过array_push,array_merge但没有奏效。

它不像你在问题中写的那样工作。调用函数_create_days(),并返回替换函数调用的值。即使该函数在一个数组中返回多个值,整个数组也会放置在要构建的外部数组中。

解决方案是编写函数_create_days()返回一个值数组,并使用array_merge()将返回的数组合并到外部数组中,例如:

function _create_days()
{
    return array(
        '2016-01-01',
        '2016-01-02',
    );
}
$header = array_merge(
    array(
        'name',
        'surname',
    ),
    _create_days(),
    array(
        'total',
    )
);

另一种选择是在构建$header时使用占位符,稍后将其替换为_create_days()使用函数 array_splice() 返回的值:

$header = array(
    'name',
    'surname',
    '@days@',           // this is the placeholder
    'total',
);
// Replace the placeholder with the values returned by _create_days()
array_splice(
    $header,
    array_search('@days@', $header),
    1,
    _create_days()
);

为了使代码灵活,我使用函数array_search()来查找占位符在$headers中的位置。这样,如果您在'@days@'之前添加或删除元素,代码仍然无需任何调整即可工作。

如果您愿意在创建数组后循环访问数组,则只需对要调用的回调使用特殊值:

例:

<?php
function _create_days() {
    return [
        '2016-01-01',
        '2016-01-02',
    ];
}
$header = [
  'name',
  'surname',
  ['_create_days'], // We know it's a callback because it's an array
  'total',
];
$headerNew = [];
foreach ($header as $value) {
    if (is_array($value)) {
        // If the length of $value is above 1 then it's a class callback, else just set it to the first item
        $callback = count($value) > 1 ? $value : $value[0];
        // Get the actual array from the callback
        $value = call_user_func($callback);
        // Merge the $headerNew array with the new values
        $headerNew = array_merge($headerNew, $value);
    } else {
        // It's not a callback, just use the actual value
        $headerNew[] = $value;
    }
}
print_r($headerNew);

输出:

Array
(
    [0] => name
    [1] => surname
    [2] => 2016-01-01
    [3] => 2016-01-02
    [4] => total
)

演示


注意:

如果您在$header数组中有任何实际数组,这会让事情变得更加困难,因为我们无法检查您是否正在使用数组。为此,您可以简单地创建一个类的实例:

<?php
// (_create_days function)
class ValueCallback {
    protected $callback;
    public function __construct($callback) {
        if (is_array($callback)) {
            $callback = count($callback) > 1 ? $callback : $callback[0];
        }
        $this->callback = $callback;
    }
    public function getValue() {
        return call_user_func($this->callback);
    }
}
$header = [
  'name',
  'surname',
  new ValueCallback('_create_days'),
  'total',
];
$headerNew = [];
foreach ($header as $value) {
    if ($value instanceof ValueCallback) {
        // Get the actual array from the callback
        $value = $value->getValue();
        // Merge the $headerNew array with the new values
        $headerNew = array_merge($headerNew, $value);
    } else {
        // It's not a callback, just use the actual value
        $headerNew[] = $value;
    }
}
print_r($headerNew);

演示

从 PHP7.4 开始,只需使用 ( ... -- 展开运算符)将函数的返回值直接解压缩/传播到结果数组中。

代码:(演示)

function _create_days() {
    return ['2016-01-01', '2016-01-02'];
}
$header = [
    'name',
    'surname',
    ..._create_days(),
    'total',
];
var_export($header);

输出:

array (
  0 => 'name',
  1 => 'surname',
  2 => '2016-01-01',
  3 => '2016-01-02',
  4 => 'total',
)