0

I have a function like this:

function colorscheme( $color, $url ) {
    return array( $color, $url );
}

How can I fetch them and display them like this (this is an example, it doesn't work):

function fetch_colorscheme() {
    foreach() {
         echo "<ul><li>$color</li><li>$url</li></ul>";
    }
}

Thank you!!

3
  • 2
    Your first function just returns a one-dimensional array, so just call the function and save the return value in a variable. Then you can access the array elements. Commented Sep 3, 2016 at 21:42
  • Honestly, your colorscheme() function is the most redundant thing I've ever seen. Commented Sep 3, 2016 at 21:57
  • ^Adding to what @Rizier123 said, here's a quick example, foreach(colorscheme('yourColor', 'yourURL') as $key => $value){ echo $key . ' => ' . $value . '<br />'; } Commented Sep 3, 2016 at 22:27

1 Answer 1

1

Try this:

$scheme = colorscheme('color', 'url');

echo '<ul>';
foreach ($scheme as $value) {
    echo '<li>' . $value . '</li>';
}
echo '</ul>';

Edit:

For multiple color schemes, you can do this:

$schemes = array(
    colorscheme('color 1', 'url 1'),
    colorscheme('color 2', 'url 2')
);
function fetch_colorscheme($schemes) {
    echo '<ul>';
    foreach ($schemes as $scheme) {
        foreach ($scheme as $value) {
            echo '<li>' . $value . '</li>';
        }
    }
    echo '</ul>';
}

I'm sorry, I'm not able to test this at the moment.

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

3 Comments

Try using sprintf or vsprintf when building markup text, eg: sprintf("<ul><li>%s</li><li>%s</li></ul>", $color, $url). It's more tidy IMHO.
That does it, but if someone adds multiple colorschemes, how would that work out?
And is it possible with just all function like in my example? thank you for your effort btw

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.