1

我有一个名为 $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++;
}

有任何想法吗?多谢你们。

4

3 回答 3

3

您正在尝试遍历内部 foreach 中的字符串。尝试

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

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


}
于 2013-07-19T15:19:53.797 回答
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
                                )
                        )
                )
        )
)
于 2013-07-19T15:23:17.157 回答
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++;
}
于 2013-07-19T15:32:24.420 回答