2

I want to add multiple values from inside of array calling a function:

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

So output be like

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

I've tried with array_push, array_merge but didn't work.

10
  • I know that i can split the array, so '$header = array("name", "surname"); $header[] = _create_days(); $header[] = "total";' But I have a big array, so I want to save some space Commented Feb 22, 2016 at 7:41
  • what space to save? you already have the solution.. don't worry about the space.. there's nothing to save Commented Feb 22, 2016 at 7:48
  • what exactly are you trying to achieve? what space? Commented Feb 22, 2016 at 7:48
  • Please let us know what your _create_days() function looks like ? Commented Feb 22, 2016 at 7:52
  • I generate array for php-excel, so I have multidimensional array that is like 1200 lines declaration, so it's like not readable now and after splinting it, it will be very hard to read Commented Feb 22, 2016 at 7:53

3 Answers 3

1

It doesn't work the way you wrote it in the question. The function _create_days() is called and it returns a value that replaces the function call. Even if the function returns multiple values in an array, the array as a whole is placed in the outer array you want to build.

The solution is to write the function _create_days() to return an array of values and merge this returned array into the outer array using array_merge() for example:

function _create_days()
{
    return array(
        '2016-01-01',
        '2016-01-02',
    );
}

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

Another option is to use a placeholder when you build $header and replace it later with the values returned by _create_days() using the function 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()
);

In order to make the code flexible I used the function array_search() to find the position of the placeholder in $headers. This way, if you add or remove elements before '@days@' the code still works without any adjustment.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're willing to iterate through the array after you've created it, you can simply use special values for callbacks that you want to call:

Example:

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

Output:

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

DEMO


Note:

If you have any actual arrays in the $header array this makes things a little more difficult, as we can't check if you're using an array. For that you can simply create an instance of a class:

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

Comments

0

From PHP7.4, just unpack/spread your function's return value directly into the result array with (... -- the spread operator).

Code: (Demo)

function _create_days() {
    return ['2016-01-01', '2016-01-02'];
}

$header = [
    'name',
    'surname',
    ..._create_days(),
    'total',
];

var_export($header);

Output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.