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
Post a Comment