Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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,我有这样的数组 Array ( [keywords] => Array ( [1] => Hazmat - Fuel/Oil/Gas [2] => Marine Life Incident [3] => Bird - Avian Incident [4] => Missing Child / Child Abduction ) )

我有这样的数组

Array
(
    [keywords] => Array
        (
            [1] => Hazmat - Fuel/Oil/Gas
            [2] => Marine Life Incident
            [3] => Bird - Avian Incident
            [4] => Missing Child / Child Abduction
        )
)
我只想分解那些包含“-”的关键字,并将它们与剩余关键字一起存储在新数组中

像这样

Array
    (
        [newkeywords] => Array
            (
                [1] => Hazmat
                [2] => Fuel/Oil/Gas
                [3] => Bird
                [4] => Avian Incident
                [5] => Marine Life Incident
                [6] => Missing Child / Child Abduction

            )
    )
知道我如何检查每个包含破折号的数组值并分解它们吗。
提前谢谢

使用下面的代码

     $keywords= array("Hazmat - Fuel/Oil/Gas","Marine Life Incident","Bird - Avian Incident");
             foreach($keywords as $keyword){
               $isTwoArr=  explode("-", $keyword);
               if(count($isTwoArr)>1){
                   foreach($isTwoArr as $oneArr)
                      $newArr[]=$oneArr;
               }
               else{
                    $newArr[]=$keyword;
               }
            }
 print_r($newArr);

使用下面的代码

     $keywords= array("Hazmat - Fuel/Oil/Gas","Marine Life Incident","Bird - Avian Incident");
             foreach($keywords as $keyword){
               $isTwoArr=  explode("-", $keyword);
               if(count($isTwoArr)>1){
                   foreach($isTwoArr as $oneArr)
                      $newArr[]=$oneArr;
               }
               else{
                    $newArr[]=$keyword;
               }
            }
 print_r($newArr);

首先,声明新数组。 接下来,编写一个循环来检查现有数组中的每个元素。 在循环中使用
if
语句,使用
strpos()
查找数组中每个元素中“-”的位置。 如果找到“-”,请使用
array\u push($newarray,$newValues)
发送到新数组。 否则,将整个值推送到新数组

下面是一些模拟代码。这将使您了解所需代码的结构

<?php
$keywords = array(
    [1] => Hazmat - Fuel/Oil/Gas
    [2] => Marine Life Incident
    [3] => Bird - Avian Incident
    [4] => Missing Child / Child Abduction
);

$newkeywords = [];

for ($i=0; $i<sizeof($kewords); $i++) {
    if (strpos($keywords[i+1], "-") === true) {
        //split the string in two and push to new array
        //explode() creates a new array
        $keywordChunks = explode("-", $keywords[i]);
        array_push($newkeywords, $keywordChunks[0];
        array_push($newkeywords, $keywordChunks[1]);
    } else {
        //push the entire word to the new array
        array_push($newkeywords, $keywords[i]);
    }
}
?>

首先,声明新数组。
接下来,编写一个循环来检查现有数组中的每个元素。
在循环中使用
if
语句,使用
strpos()
查找数组中每个元素中“-”的位置。 如果找到“-”,请使用
array\u push($newarray,$newValues)
发送到新数组。 否则,将整个值推送到新数组

下面是一些模拟代码。这将使您了解所需代码的结构

<?php
$keywords = array(
    [1] => Hazmat - Fuel/Oil/Gas
    [2] => Marine Life Incident
    [3] => Bird - Avian Incident
    [4] => Missing Child / Child Abduction
);

$newkeywords = [];

for ($i=0; $i<sizeof($kewords); $i++) {
    if (strpos($keywords[i+1], "-") === true) {
        //split the string in two and push to new array
        //explode() creates a new array
        $keywordChunks = explode("-", $keywords[i]);
        array_push($newkeywords, $keywordChunks[0];
        array_push($newkeywords, $keywordChunks[1]);
    } else {
        //push the entire word to the new array
        array_push($newkeywords, $keywords[i]);
    }
}
?>

这是非常基本的PHP,只要正确阅读PHP手册就可以解决。这是非常基本的PHP,只要正确阅读PHP手册就可以解决。