php - push an associative arrays as a one associative array -


i have desired associative array format (refer below)

$url = array (     "a1" => array(         'k1' => "content of k1 in a1 array",         'k2' => "content of k2 in a1 array"     ),     "a2" => array(         'k1' => "content of k1 in a2 array",         'k2' => "content of k2 in a2 array"     ), ); 

and in laravel thing, retrieved records in database table , loop on , value , push unto array.

$k = kk::with('kk_names')->get(); $k_array = array(); foreach($k $item){     $sr = array($item->name => array("k1" => $item->items[0]->description,"k2" => $item->items[1]->description)) array_push($k_array,$sr); } 

it did works associative array format (refer above associative array format) not 1 want, gives me instead.

$url = array (     array("a1" => array(         'k1' => "content of k1 in a1 array",         'k2' => "content of k2 in a1 array"     )),     array("a2" => array(         'k1' => "content of k1 in a2 array",         'k2' => "content of k2 in a2 array"     )), ); 

any ideas, please?

array_push push element onto end of array. doing -

array_push(array(...), array($item->name => ...)); 

which give -

array (    array($item->name => ...),    array($item->name => ...) ) 

but according desired output, want $item->name index , array('k1' ...) elements.

you can -

foreach($k $item) {     $k_array[$item->name] = array(         "k1" => $item->items[0]->description,         "k2" => $item->items[1]->description     ); } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -