Add a new field to array elements in a foreach PHP - Laravel -


i have variable:

$families = array(     array(         'expand'      => '',         'family_id'   => 'aaer',         'active'      => true,         'description' => 'wall art',         'group_id'    => 5     ),     array(         'expand'      => '',         'family_id'   => 'eerr',         'active'      => true,         'description' => 'personalised mugs',         'group_id'    => 4     ), ); 

and want add $families items field called 'href', this:

$families = array(     array(         'href'        => 'http://mipage/wall-art/aaer',         'expand'      => '',         'family_id'   => 'aaer',         'active'      => true,         'description' => 'wall art',         'group_id'    => 5     ),     array(         'href'        => 'http://mipage/personalised-mug/eeer',         'expand'      => '',         'family_id'   => 'eerr',         'active'      => true,         'description' => 'personalised mugs',         'group_id'    => 4     ), ); 

to iterate $families in foreach loop:

foreach($cat['families'] $cat_fam) {     $cat['families'][]['href']  = 'http//mysite/'.str_slug($cat_fam).'/'.$cat_fam['family_id']; } 

but not works me.

how can make this?

you've repalce empty [] specific key. update foreach block key of element , use inside foreach loop.

$cat['families'][$key] points individual element of families array.

like this,

foreach($cat['families'] $key=>$cat_fam) {     $cat['families'][$key]['href']  = 'http//mysite/'.str_slug($cat_fam).'/'.$cat_fam['family_id']; } 

demo: https://eval.in/636898


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -