拆分字符串 - php

时间:2013-07-19 15:08:02

标签: php arrays string foreach explode

我有一个名为$ result的数组,如下所示:

array(1) 
    { ["response"]=> array(3) 
        { [0]=> array(1) 
            { ["list"]=> array(2) 
                { ["category"]=> string(6) "(noun)" 
                ["synonyms"]=> string(27) "chelonian|chelonian reptile" } 
            } 
          [1]=> array(1) 
            { ["list"]=> array(2) 
                { ["category"]=> string(6) "(verb)" 
                  ["synonyms"]=> string(57) "capsize|turn turtle|overturn|turn over|tip over|tump over" } 
            } 
          [2]=> array(1) 
            { ["list"]=> array(2) 
                { ["category"]=> string(6) "(verb)" 
                  ["synonyms"]=> string(29) "hunt|run|hunt down|track down" } 
            } 
         } 
    }

我正在尝试访问[“synonyms”]元素,并将每个单词拆分并将其存储在自己的字符串中,或​​者可能是所有单词的数组。您可以看到单词由|符号分隔。

我尝试过以下代码,但没有显示(结果没有显示,所以爆炸无效):

$i=0;

foreach ($result["response"] as $value) 
{ 
 foreach ($value["list"]["synonyms"] as $temp)
  {
  $alternative[$i] = explode ("|", $temp);
  $i++;
  }

}

//OUTPUT THE RESULTS

$j=0;

foreach ($alternative as $echoalternative)
{
echo $j.": ".$echoalternative;
$j++;
}

有什么想法吗?谢谢你们。

3 个答案:

答案 0 :(得分:3)

您正在尝试迭代内部foreach中的字符串。尝试

foreach ($result["response"] as $value) 
{ 

  $alternative[$i] = explode ("|", $value["list"]["synonyms"]);
  $i++;


}

答案 1 :(得分:3)

要创建一维数组(假设原始数组中有三个组),可以执行以下操作:

$out = array();
foreach ($arr['response'] as $key => $value){
    $syns = explode('|', $value['list']['synonyms']);
    foreach ($syns as $key2 => $value2){
        $out[$key][] = $value2;
    }
}

要访问索引为0的同义词组的单维数组,请执行以下操作:

var_dump($out[0]);

Array(
    [0] => chelonian
    [1] => chelonian reptile
)

如果您只想显示同义词,可以执行以下操作:

foreach ($arr['response'] as $key => $value){
    $syns = explode('|', $value['list']['synonyms']);
    foreach ($syns as $key2 => $value2){
        echo $value2.', ';
    }
    echo "<br />";
}

输出:

chelonian, chelonian reptile,
capsize, turn turtle, overturn, turn over, tip over, tump over,
hunt, run, hunt down, track down, 

但是,如果要将其包含在原始数组中,可以执行以下操作:

array_walk_recursive($arr, function (&$e, $k){
    if (preg_match('#[\w\|]+#', $e)){
        $e = explode('|', $e);
    }
});
var_dump($arr);

输出:

Array(
    [response] => Array
            [0] => Array(
                    [list] => Array(
                            [category] => Array(
                                    [0] => (noun)
                                )
                            [synonyms] => Array(
                                    [0] => chelonian
                                    [1] => chelonian reptile
                                )
                        )
                )
            [1] => Array(
                    [list] => Array(
                            [category] => Array(
                                    [0] => (verb)
                                )
                            [synonyms] => Array(
                                    [0] => capsize
                                    [1] => turn turtle
                                    [2] => overturn
                                    [3] => turn over
                                    [4] => tip over
                                    [5] => tump over
                                )
                        )
                )
            [2] => Array(
                    [list] => Array(
                            [category] => Array(
                                    [0] => (verb)
                                )
                            [synonyms] => Array(
                                    [0] => hunt
                                    [1] => run
                                    [2] => hunt down
                                    [3] => track down
                                )
                        )
                )
        )
)

答案 2 :(得分:2)

以下重构代码应解决问题

$i=0;

foreach ($result["response"] as $value) 
{ 
// print_r($value);
$temp = $value["list"]["synonyms"];
// echo $temp;
 // foreach ($value["list"]["synonyms"] as $temp)
  // {
  $alternative[$i] = explode ("|", $temp);
  $i++;
  // }

}

//OUTPUT THE RESULTS

$j=0;

foreach ($alternative as $echoalternative)
{
print_r($echoalternative);
echo $j.": ".$echoalternative;
$j++;
}