0

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 class="fa fa-pencil"></i>Edit</li>
<li><i class="fa fa-trash"></i>Remove</li>

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

Thank you in advance!

2
  • Anyway if you only have only two array elements, then it may be unusefull to use an array for it. But perhaps that's not the case. Commented Jan 28, 2022 at 21:14
  • I have multiple items. The exemple of my question was simplified for this case. Commented Jan 28, 2022 at 21:17

1 Answer 1

3

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',
    ),
);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, was that simple... Works perfect, thank you so much!

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.