如何把三个值放入一个数组php,并把它foreach


How to put three values into one array php and put it foreach?

我想把三个值放到一个数组中,而不是两个。就像下面的代码,我怎么才能做到这一点?

$emo_word = array(
  "LOL !" => 'lol' => 1, 
  "COOL !" => 'cool'=> 2, 
  "CUTE !" => 'cute' =>3, 
  "LOVE IT !" => 'love_it'=>4
);  
foreach( $emo_word as $first=> $second => $third)
{
  //code here
}

语法错误:

$emo_word = array(
  "LOL !" => 'lol' => 1, 
  //---------------^
  "COOL !" => 'cool'=> 2, 
  "CUTE !" => 'cute' =>3, 
  "LOVE IT !" => 'love_it'=>4
);
foreach( $emo_word as $first=> $second => $third)
//-------------------------------------^
{
  //code here
}

您可能需要使用数组来代替。它应该是这样的?

$emo_word = array(
  1 => array("LOL !", 'lol')
  2 => array("COOL !", 'cool'),
  3 => array("CUTE !", 'cute'),
  4 => array("LOVE IT !", 'love_it')
);

这取决于你到底想做什么。如果您觉得这些LOL !和其他是标识符,请将它们保留为索引。

如果你想让它保持原来的样子,你可以这样做

$emo_word = array(
  "LOL !" => array('lol' => 1),
  "COOL !" => array('cool'=> 2,),
  "CUTE !" => array('cute' =>3),
  "LOVE IT !" => array('love_it'=>4)
);

访问LOL !然后得到像这样的1:

$emo_word['LOL !']['lol']

我想你可以使用list-命令:

$emo_world= array(
   array("LOL !", "lol", 1),
   array("COOL !", "cool", 2),
   array("CUTE !", "cute", 3),
);
foreach($emo_world as $world){
  list($first, $second, $third) = $world;
  echo "Here you go: ".$first." and ".$second." and ".$third; 
}

虽然我没有测试它,但这应该是要走的路。希望对你有帮助。

http://php.net/manual/de/function.list.php