0

I have an array like this

Array ( [0] => Array ( [keyword] => nothing [id] => 44 ) [1] => Array ( [keyword] => everything [id] => 45 ) [2] => Array ( [keyword] => dogs [id] => 46 ) )

I basically want to create a link like this:

<a href="mypage.php?pid=ID">KEYWORD</a>

So I was thinking about using array_walk, but I dont think this will work on a multidimensional array, how do I go about getting it to iterate through my array and output the ID and keyword for each one of my entries in the array?

1 Answer 1

1

Use a foreach loop:

$array = array(
    array(
        "keyword" => "anything",
        "id"      => 44
    ),
    array(
        "keyword" => "everything",
        "id"      => 45
    ),
    array(
        "keyword" => "dogs",
        "id"      => 46
    ),
);

foreach ($array as $link) {
    $keyword = $link["keyword"];
    $id      = $link["id"];
    echo "<a href='mypage.php?pid=$id'>$keyword</a>" . PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

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.