0

I would like to create a PHP array, where some of the elements are to be replaced by elements of an array returned by a function. As example, I would like to create the following array:

$aArray = array(
    0x01, 
    0x10, 
    foo(1234), 
    0x03, 
    foo(445), 
    ...
)

Here, foo is a function returning a simple PHP array. What I get is, of course, something like

(
    [0] => 1
    [1] => 16
    [2] => Array
       (
         [1] => 2
         [2] => 4
       )
    [3] => 03
    [4] => Array
      ....

But what I actually want to do is to include the returned values from the function directly into the array, to create something like: ( [0] => 1 [1] => 16 [2] => 2 [3] => 4 [4] => 03 [5] => 3 ....

Is this possible to do in a simple way, without having to construct the array with array_merge?

P.S: The above example is not a working example, just showing what I want to do. The content of the function foo is also irrelevant.

hakre
  • 193,403
  • 52
  • 435
  • 836
Alex
  • 41,580
  • 88
  • 260
  • 469

4 Answers4

1

This is the closest thing that I can do to match your need

$aArray = array_flat(
    0x01, 
    0x10, 
    foo(1234), 
    0x03, 
    foo(445)
);

And here is code for array_flat.

function array_flat()
{
    $array  = func_get_args()
    $result = array();
    foreach ($array as $value) {
        is_array($value) || $value = array($value);                   
        foreach ($value as $dvalue) {
            $result[] = $dvalue;
        }
    }

    return $result;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
invisal
  • 11,075
  • 4
  • 33
  • 54
0

You can pass this array to the function by reference and do with it what you will inside the function.

public function test(&$arr) {
    $arr[] = 1234;
}

$arr = array(1, 2, 3);
test($arr);

print_r($arr); // array(1, 2, 3, 1234);

Or you can drop the references and return the array from the function normally:

public function test($arr) {
    $arr[] = 1234;
    return $arr;
}

$arr = array(1, 2, 3);
$arr = test($arr);

print_r($arr); // array(1, 2, 3, 1234);
Maxim Kumpan
  • 2,545
  • 2
  • 19
  • 23
0

You'll need to array_merge the separate arrays:

$aArray = array_merge(array(0x01, 0x10), foo(1234), array(0x03), foo(445));
deceze
  • 510,633
  • 85
  • 743
  • 889
  • `+` is a bad idea for arrays since it will not add the corresponding values for existing numeric keys. – Alma Do Aug 28 '13 at 10:22
  • Yup, just looked that up as I wasn't sure whether that applied to numeric keys too. `array_merge` it is then. – deceze Aug 28 '13 at 10:23
  • 1
    and `array_merge` does not fit OP's requirements (I guess correct answer is **No** - i.e. it's impossible without merging) – Alma Do Aug 28 '13 at 10:24
  • Ah yes, I overlooked that that's a "requirement". – deceze Aug 28 '13 at 10:25
  • @AlmaDoMundo: Depending how you use it, `array_merge` does fit, see http://stackoverflow.com/a/18485341/367456 – hakre Aug 28 '13 at 10:26
  • @hakre - that's even more complicated than single `array_merge`. I'm aware of that way, but it's still not a _syntax_ solution that OP wants – Alma Do Aug 28 '13 at 10:36
  • there is no syntax solution. there is only casting, I'd say OP is looking for casing because it will normalize all values at least to arrays so all can be treated equal. Like deeceze does here, casting the values all to array, then merging them as parameters. – hakre Aug 28 '13 at 10:39
0

If you need that in the array, you either need to manage that when adding or afterwards.

If you want to do this step by step as the data becomes available, you can push to the array each time. Here is an example with two anonymous functions that are pushing into your array each time invoked:

$pushArray = function(&$array) {
    return function($item) use (&$array) {
        is_array($item) || $item = array($item);
        foreach($item as $value) {
            $array[] = $value;
        }
    };
}

$array = array();
$push = $pushArray($array);
$push(0x01);
$push(0x10);
$push(foo(1234));
...

If you want to do it afterewards you can do this for example with casting and merging:

function arrayval($mixed) {
    settype($mixed, 'array');
    return $mixed;
}

$flat = call_user_func_array('array_merge', array_map('arrayval', $array));

Another one is by reducing the array later on which is properly more easy to understand:

$flat = array_reduce($array, function($result, $item) {
    return array_merge((array) $result, (array) $item);
});

For everything afterwards, more Q&A on this website already exists:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836