doujing1858 2014-10-02 17:23
浏览 12
已采纳

检查数组值是否存在然后显示第二个值

I get array like:

Array ( [0] => url=website1.com/etcetcetc&itag=43&quality=medium&something=nill
[1] => url=website2.com/etcetcetc&itag=18&quality=medium&something=nill
[2] => url=website3.com/etcetcetc&itag=5&quality=small&something=nill
[3] => url=website4.com/etcetcetc&itag=36&quality=small&something=nill
[4] => url=website5.com/etcetcetc&itag=17&quality=small&something=nill )

and after using foreach($links as $link), I get these arrays like:

Array ( [url] => website.com1/etcetcetc [itag] => 43 [quality] => medium [something] => nill )
Array ( [url] => website.com2/etcetcetc [itag] => 18 [quality] => medium [something] => nill )
Array ( [url] => website.com3/etcetcetc [itag] => 5 [quality] => small [something] => nill )
Array ( [url] => website.com4/etcetcetc [itag] => 36 [quality] => small [something] => nill )
Array ( [url] => website.com5/etcetcetc [itag] => 17 [quality] => small [something] => nill )

How can I show selected links from it?

I want to show links with itag : 43 , 18 , 36. It checks if URL with one of those is available . If available, then show it's URL. It can be 1, 2 or 3 links, meaning if the link with itag 43 and 18 is available, then it shows the 2 URLs and if all 3 itag links are available then it shows those 3 links.

The url is different every time, so I can't save url in php, meaning it always has to take URL/link from array.

Currently I am showing it like:

echo '<a href="'.$r['url'].'">quality: '.$r['itag'].'</a>';

------- edit ------

here is the script/code in which i want to do that (it was made by one of my friend but he is not there now a days )

foreach($links as $link) { 
    echo '<a href="'.$r['url'].'">quality: '.$r['itag'].'</a><br />';
}
  • 写回答

3条回答 默认 最新

  • douya1061 2014-10-02 17:46
    关注

    You could just iterate through the array and if it has the number you're looking for, echo it, like this:

    $array = array(
        array( 'url' => 'website.com1/etcetcetc', 'itag' => 43, 'quality' => 'medium', 'something' => 'nill' ),
        array( 'url' => 'website.com2/etcetcetc', 'itag' => 18, 'quality' => 'medium', 'something' => 'nill' ),
        array( 'url' => 'website.com3/etcetcetc', 'itag' => 5, 'quality' => 'small', 'something' => 'nill' ),
        array( 'url' => 'website.com4/etcetcetc', 'itag' => 36, 'quality' => 'small', 'something' => 'nill' ),
        array( 'url' => 'website.com5/etcetcetc', 'itag' => 17, 'quality' => 'small', 'something' => 'nill' )
    );
    
    $r = array();
    
    function test($array, $number) {
        foreach($array as $r)
            if ($r['itag'] == $number) {
                echo '<a href="'.$r['url'].'">quality: '.$r['itag'].'</a>';
            }
    }
    
    test($array, 43);
    test($array, 18);
    test($array, 36);
    

    You can test it here: http://3v4l.org/E0Pck

    EDIT: Following your edit, here is how you can resolve this: really the only thing you need to do is to define a number variable containing the itag you're looking for, then check against it inside the foreach loop, as follows:

    $number = 22;
    
    foreach($links as $link) { 
        parse_str($link,$r); 
        if ($r['itag'] == $number)
            echo '<a href="'.$r['url'].'">quality: '.$r['itag'].'</a><br />';
    }
    

    Or if you would like to use an array as input, so you don't have to do it with each number, just wrap it inside another foreach loop, looping through the elements of your new $numbers array, as follows:

    $numbers = array(17, 22, 36);
    
    foreach($numbers as $number) {
        foreach($links as $link) { 
            parse_str($link,$r); 
            if ($r['itag'] == $number)
                echo '<a href="'.$r['url'].'">quality: '.$r['itag'].'</a><br />';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?