php如何判断数组["a","b","c"]中是否含有"a"?
<?php
$arr_1=array(
"index/logo.png",
"movie/play.png",
"movie/stop.png",
"game/war_p.png",
"game/starcaft_p.png",
"music/fly.png"
);

$arr_2=array(
"stop.png",
"fly.png"
);

//怎样将$arr_2在$arr_1出现过的图片复制到"images/"目录下?

//有没有判断某数组当中是否含有某子元素的函数?
//比如"hello"是否存在于 array("good","bad","hello")中.
?>

解决方案 »

  1.   

    array_intersect计算数组交集in_array("hello", array("good","bad","hello"))//结果 true,找不到返回false
      

  2.   

    PHP 自带函数 in_array();
      

  3.   


    //php如何判断数组["a","b","c"]中是否含有"a"?
    $array = array('a','b','c');
    if(in_array('a',$array)){
        echo '存在';
    } else {
        echo '不存在';
    }
      

  4.   

    array_search --  在数组中搜索给定的值,如果成功则返回相应的键名 
      

  5.   

    楼主的问题描述和程序代码中的问题不一样。程序代码中的问题显然不是 in_array() 能解决的。不过,这个问题即使没有标准函数直接解决,写一小段代码也应该很简单吧?
      

  6.   

    in_array  这个就很好了 不需要其他的
      

  7.   

    in_array()
    array_search()
    两个都可以。
      

  8.   

    不知道我的代码符合楼主的要求不···<?php
    $arr_1=array(
            "index/logo.png",
            "movie/play.png",
            "movie/stop.png",
            "game/war_p.png",
            "game/starcaft_p.png",
            "music/fly.png"
    );
        
        $arr_2=array(
            "stop.png",
            "fly.png"
        );
        
        //添加下面代码
    foreach($arr_1 as $key){

    foreach($arr_2 as $kkey){
    if(strstr($key, $kkey)){
    echo '需要复制'.$key.'这个图片<br />';
    }
    }
        }?>
      

  9.   


    #define the string url of the destination dir
    define('DES_DIR',$_SERVER['DOCUMENT_ROOT'].'/image');$arr_1=array(
      "index/logo.png",
      "movie/play.png",
      "movie/stop.png",
      "game/war_p.png",
      "game/starcaft_p.png",
      "music/fly.png"
    );
        
      $arr_2=array(
      "stop.png",
      "fly.png"
      );#回调函数作为array_callback_function()的参数#callback function 
    function index_remove(&$value){
        #不知道参数记错没有 很久没用过这个函数了
        $value = preg_replace('/^(.*)\/[a-zA-Z_]?\.[png|jpg|jpeg|gif]?$/si','',$value);
    }#end function index_remove$arr_3 = array_walk($arr_1,index_remove);foreach($arr_2 as $find_element_in_arr2)
    {
        if(in_array($arr_3,$find_element_in_arr2))
        pinrtf('正在复制文件$s到目标文件夹$s',$find_element_in_arr2,DES_DIR);
        #确保你有足够的写入权限到DES_DIR文件夹
         @copy(dirname($find_element_in_arr2).'/'.$find_element_in_arr2,DES_DIR);
    }