Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 - Fatal编程技术网

从PHP数组中删除字符串

从PHP数组中删除字符串,php,arrays,Php,Arrays,我想搜索一个数组,其中包含“new”“York”“other”“thing”等词, 只把构成一个国家的词语组合起来 在本例中,我希望在数组中组合状态的匹配: 由此: <?php $states = array("new york", "Nevada"); $names = array("something", "green", "something", "yellow", "new", "york", "new","jersey"); 因此,更改名称数组以合并状态。假设您可以解决注释中

我想搜索一个数组,其中包含“new”“York”“other”“thing”等词, 只把构成一个国家的词语组合起来

在本例中,我希望在数组中组合状态的匹配:

由此:

<?php 
$states = array("new york", "Nevada");
$names = array("something", "green", "something", "yellow", "new", "york", "new","jersey");

因此,更改名称数组以合并状态。

假设您可以解决注释中的“新泽西”问题,下面将把一个单词数组映射到目标数组(在本例中为状态)。然后对生成的数组进行重复数据消除并重新编制索引

如果名称只需要匹配目标字符串中的整个单词,则可以将
stripos
测试更改为更高级的
preg_match

<?php 
$states = array("new york", "Nevada");
$names = array("something", "green", "something", "yellow", "new", "york", "new","jersey");

$result = array_map(function ($e) use ($states) {
    foreach ($states as $state) {
        // The moment a match is found, return it
        if (stripos($state, $e) !== false)
        {
            return $state;
        }
    }

    // If no match was found, fall back to the word in the original array
    return $e;
}, $names);

print_r(array_values(array_unique($result)));

在查看解决方案之前,请先看两个假设: 1.并发数组元素签入$names。 2.对$states进行检查

$states = array("new york", "Nevada");
$names  = array("something", "green", "something", "yellow", "new", "york", "new","jersey");
foreach($names as $k => $v) {
    $combined_names = $names[$k]." ".$names[$k+1];
    if (in_array($combined_names, $states)) {
        $names[] = $combined_names;
        $names[$k] = $names[$k+1] = '';
    } 
}
$result = array_values(array_filter($names));
输出:

Array
(
  [0] => something
  [1] => green
  [2] => something
  [3] => yellow
  [4] => new
  [5] => jersey
  [6] => new york
)

在您的示例中,它如何知道创建新泽西?如果
$states
数组包含所有50个州,它如何知道new是应用于纽约还是新泽西?states数组中是否应该有newjersey?我明白你的逻辑吗?这里有两件事需要澄清。1.我们应该只检查两个同时出现的数组值吗?e、 g:“纽约”和“纽约”。而不是“绿色”和“黄色”,因为它们不是同时出现的条目?2.根据什么逻辑我们得到纽约?$州没有新的york@ObjectManipulator各州确实有纽约
$States=数组(“纽约”、“内华达”)
对不起,我是说新的运动衫对不起,不,我只想在它是一个州的任何地方合并…所以如果所有州的名称相邻,就合并它们
$states = array("new york", "Nevada");
$names  = array("something", "green", "something", "yellow", "new", "york", "new","jersey");
foreach($names as $k => $v) {
    $combined_names = $names[$k]." ".$names[$k+1];
    if (in_array($combined_names, $states)) {
        $names[] = $combined_names;
        $names[$k] = $names[$k+1] = '';
    } 
}
$result = array_values(array_filter($names));
Array
(
  [0] => something
  [1] => green
  [2] => something
  [3] => yellow
  [4] => new
  [5] => jersey
  [6] => new york
)