PHP - dynamically generated elements of an array()? -


1. array

i have functon returns array of arrays:

function show_array() {    $myarray = array(         array(             'foo' => 'bar',             'bar' => 'foo',             'aaa' => 'bbb'),         array(             'foo' => 'bar2',             'bar' => 'foo',             'aaa' => 'bbb'),         array(             'foo' => 'bar3',             'bar' => 'foo',             'aaa' => 'bbb'),         array(             'foo' => 'bar4',             'bar' => 'foo',             'aaa' => 'bbb'),         //i want add additional elements here using foreach     );    return $myarray; } 

2. elements add dynamically

as stated in comment above want add additional elements $myarray based on foreach loop, here's simple function returns nothing, shows want insert there:

$addtomyarray = array('one','two','three');  foreach($addtomyarray $newelement) {     array(         'foo' => $newelement,         'bar' => 'foo',         'aaa' => 'bbb',     ); } 

3. desired result

so in end show_array() should return:

        array(             array(                 'foo' => 'bar',                 'bar' => 'foo',                 'aaa' => 'bbb'),             array(                 'foo' => 'bar2',                 'bar' => 'foo',                 'aaa' => 'bbb'),             array(                 'foo' => 'bar3',                 'bar' => 'foo',                 'aaa' => 'bbb'),             array(                 'foo' => 'bar4',                 'bar' => 'foo',                 'aaa' => 'bbb'),             //added stuff             array(                 'foo' => 'one,                 'bar' => 'foo',                 'aaa' => 'bbb'),             array(                 'foo' => 'two,                 'bar' => 'foo',                 'aaa' => 'bbb'),             array(                 'foo' => 'three,                 'bar' => 'foo',                 'aaa' => 'bbb'),         ); 

i trying return new options $myarray[], array_push on them , array_merge, nothing seems work, unable place loops within $myarray array (what obvious). show_array() never returns generated elements.

how should done?

did mean similar this?

function show_array() {    $myarray = array(         array(             'foo' => 'bar',             'bar' => 'foo',             'aaa' => 'bbb'),         array(             'foo' => 'bar2',             'bar' => 'foo',             'aaa' => 'bbb'),         array(             'foo' => 'bar3',             'bar' => 'foo',             'aaa' => 'bbb'),         array(             'foo' => 'bar4',             'bar' => 'foo',             'aaa' => 'bbb'),     //i want add additional elements here using foreach    );      $addtomyarray = array('one','two','three');      foreach($addtomyarray $newelement) {         $myarray[] = array(             'foo' => $newelement,             'bar' => 'foo',             'aaa' => 'bbb',         );     }     return $myarray; } 

or one:

function compilearray($values) {     myarray = array();     foreach($values $newelement) {         $myarray[] = array(             'foo' => $newelement,             'bar' => 'foo',             'aaa' => 'bbb',         );     }     return $myarray; }  $result = array_merge(show_array(),compilearray(array('one','two','three'))); 

or, adding further flexibility:

function createelementarray($value) {     return array(         'foo'=>$value,         'bar'=>'foo',         'aaa'=>'bbb'     ); }  $result = array_merge(     show_array(),     array_map(createelementarray, array('one','two','three')) ); 

in order have code of single array encapsulated function interchangeable.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -