Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 搜索数组并替换其某些值_Php_Arrays_Search_Replace - Fatal编程技术网

Php 搜索数组并替换其某些值

Php 搜索数组并替换其某些值,php,arrays,search,replace,Php,Arrays,Search,Replace,我有一个preg_match_all,它从一个字符串生成一个URL数组。该阵列类似于: $url[0] = "http://www.siteone.com"; $url[1] = "http://www.sitetwo.com"; $url[2] = "http://www.sitethree.com/example1"; $url[3] = "http://www.sitefour.com"; $url[4] = "http://www.sitethree.com/example2"; $ur

我有一个preg_match_all,它从一个字符串生成一个URL数组。该阵列类似于:

$url[0] = "http://www.siteone.com";
$url[1] = "http://www.sitetwo.com";
$url[2] = "http://www.sitethree.com/example1";
$url[3] = "http://www.sitefour.com";
$url[4] = "http://www.sitethree.com/example2";
$url[5] = "http://www.sitefive";
$url[6] = "http://www.sitesix";
$url[7] = "http://www.siteseven";
$url[8] = "http://www.sitethree.com/example3";
但是,我需要能够在url数组中搜索,以在它包含http://www.sitethree.com 并将数组中的这个特定值设置为no value。因此,一旦应用此过程,阵列将如下所示:

$url[0] = "http://www.siteone.com";
$url[1] = "http://www.sitetwo.com";
$url[2] = "no value";
$url[3] = "http://www.sitefour.com";
$url[4] = "no value";
$url[5] = "http://www.sitefive";
$url[6] = "http://www.sitesix";
$url[7] = "http://www.siteseven";
$url[8] = "no value";

我在循环中尝试了许多preg_match_all和if语句的变体,但都没有得到。任何帮助都将不胜感激。

一个简单的方法是:

$url = array_map(function($v) {
    return strpos($v, 'http://www.sitethree.com') === false ? $v : 'no value';
}, $url);
foreach($url as $key => $value)
{
  if(stristr($value, "http://www.sitethree.com"))
  {
    $url[$key] = "no value";
  }
}
foreach($url as $key => $value)
{
  if(stristr($value, "http://www.sitethree.com"))
  {
    $url[$key] = "no value";
  }
}
foreach ($url as &$value) {
  if (strpos($value, 'http://www.sitethree.com') === 0) {
    $value = 'no value';
  }
}