0

i have array and i want print #5 of array

i write this with FOR but not work currently

this is my code and i want to get result with FOREACH

$tagsfinal =Array
(
[0] =say
[1] =hello
[2] =nice
[3] =mans
[4] =wars
[5] =rody
[6] =low
[7] =high
)
for($i=0; $i<5; $i++){
    print '<a href="https://example.com/'.$tagsfinal[$i].'" class="ivkeys">' . $tagsfinal[$i] . '</a>';
}

**i want this : **

 out=<a href="https://example.com/say" class="ivkeys">
 <a href="https://example.com/hello" class="ivkeys">
 <a href="https://example.com/nice" class="ivkeys">
 <a href="https://example.com/mans" class="ivkeys">
 <a href="https://example.com/wars" class="ivkeys">

Anyone know of a good alternative or better way to write this code?

thanks for your helps..

0

1 Answer 1

1

Solution with loop foreach

Iterate all the array and print only the values before key $printlimit. $tk is the key, and $tv is the value, of each element.

$printlimit = 5;
foreach ($tagsfinal as $tk => $tv)
{
    if ($tk < $printlimit)
    {
        print '<a href="https://example.com/'.$tv.'" class="ivkeys">' . $tv . '</a>';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

your solutions not work just for($i=0; $i<5; $i++){ print '<a href="https://example.com/'.$tagsfinal[$i].'" class="ivkeys">' . $tagsfinal[$i] . '</a>'; } work currently !
See my question again ! UPDATED

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.