0

I have this 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 I want add to my $families items a field called 'href', like 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 do this I iterate $families in a foreach loop:

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

But this not works for me.

How can I make this?

1
  • 1
    You need to specify how it is not working... Commented Sep 7, 2016 at 11:37

5 Answers 5

2

You've to repalce empty [] with the specific key. For this update foreach block to get key of the element and use that inside foreach loop.

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

Like this,

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

Demo: https://eval.in/636898

Sign up to request clarification or add additional context in comments.

Comments

1

just iterate over the array, and add a key ahref

 $newArray= array();
 foreach($families as $innerArray){
   $innerArray['ahref']='YOUR LINK HERE'; 
   $newArray[] = $innerArray;
 }

$families = $newArray ;//if you want to update families array

2 Comments

It should be $newArray[] = $innerArray; instead.
@AlokPatel yes, typo, actually I quickly tested the output on my machine and updated it here..
0

Do something like:

$href = array('href'=>'http://mipage/wall-art/AAER');
$combined_array = array_combine($families[0],$href);

Don't tested but you can try or modify as per your use

Comments

0

Please try this:

I think you also forgot to add index description in your str_slug call.

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

Comments

0

You can use the php function array_walk

Liek this :

array_walk($cat['families'], function(&$family){
   $family['href'] = 'http//mysite/'.str_slug($family).'/'.$family['family_id'];
 });

note the $family variable is passed by reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.