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

时间:2014-10-02 17:23:25

标签: php arrays

我得到的数组如下:

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 )

并且在使用foreach($links as $link)之后,我得到了这些数组:

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 )

如何显示选定的链接?

我想显示与itag的链接:43,18,36。它会检查其中一个网址是否可用。如果可用,请显示其URL。它可以是1,2或3个链接,这意味着如果有itag 43和18的链接可用,那么它会显示2个网址,如果所有3个itag链接都可用,则会显示这3个链接

网址每次都不同,所以我无法在网址中保存网址,这意味着它总是必须从数组中获取网址/链接。

目前我正在展示它:

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

-------编辑------

这里是我想要做的脚本/代码(它是由我的一个朋友制作的,但他现在不在那里一天)

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

3 个答案:

答案 0 :(得分:1)

您可以遍历数组,如果它有您要查找的数字,请回复它,如下所示:

$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);

您可以在此处进行测试:http://3v4l.org/E0Pck

编辑:编辑后,您可以通过以下方式解决此问题:您唯一需要做的就是定义一个包含您itag的数字变量寻找,然后在foreach循环中检查它,如下所示:

$number = 22;

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

或者如果您想使用数组作为输入,那么您不必对每个数字执行此操作,只需将其包含在另一个foreach循环中,循环遍历新的{ {1}}数组,如下:

$numbers

答案 1 :(得分:0)

   $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' ) ); 

$no = '43';// whatever u want

foreach($os as $r)
{
   if (in_array(($r['itag'] == $no), $os)) {
   echo '<a href="'.$r['url'].'">quality: '.$r['itag'].'</a>';
   }
}

答案 2 :(得分:0)

if statement循环后使用foreach将解决您的问题。

foreach($links as $link){ //foreach loop as you said

    if($link['itag'] == '43' || $link['itag'] == '18' || $link['itag'] == '36'){ //check if the itag is 43 or 18 or 36

    //your echo here

    }

}