Home > Software design >  Get multiple values of an Array inside PHP function
Get multiple values of an Array inside PHP function

Time:01-29

This is my code:

function menu_MORE (Array  $ITEMS){
 foreach ($ITEMS as $ITEM) {
  echo "<li><i class='$ITEM[icon]'></i>$ITEM[link]</li>";
}

$ITEMS = array('link' => array('Edit','Remove'),
               'icon' => array('fa fa-pencil','fa fa-trash'), );

menu_MORE($ITEMS);

I want the output to be:

<li><i ></i>Edit</li>
<li><i ></i>Remove</li>

I think i need a second rule/parameter but I can't figured it out how.

Thank you in advance!

CodePudding user response:

Your array is er... "inverted" -- you're doing this, where each key has multiple values:

$ITEMS = array(
    'link' => array('Edit','Remove'),
    'icon' => array('fa fa-pencil','fa fa-trash')
);

You want to do this, where you have multiple entries of items, each with a single value:

$ITEMS = array(
    array(
        'link' => 'Edit',
        'icon' => 'fa fa-pencil',
    ),
    array(
        'link' => 'Remove',
        'icon' => 'fa fa-trash',
    ),
);
  •  Tags:  
  • Related