PHP有一种方法可以从数组内部添加调用函数的元素

时间:2016-02-22 07:40:03

标签: php

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

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

所以输出就像

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

我尝试过使用array_push,array_merge但是没有用。

2 个答案:

答案 0 :(得分:1)

它不像你在问题中写的那样工作。函数_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时使用占位符,稍后使用函数array_splice()_create_days()返回的值替换为<{1}}:

$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@'之前添加或删除元素,代码仍可正常运行而无需任何调整。

答案 1 :(得分:0)

如果您在创建数组后愿意迭代数组,则可以使用特殊值来调用您想要调用的数组:

示例:

<?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
)

DEMO

注意:

如果$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);

DEMO