如何在foreach循环中制作数组 - php?

arrayOne + foreach loop = arrayTwo


<?php /* version +7 */

$input = array("teamA","teamB","teamC");

foreach($input as &$value){

    $assign = "50"; /* The data just temp */

    $data = array($value=>$assign);

}

echo $data["teamA"];

?>


胡子哥哥
浏览 88回答 2
2回答

繁星coding

我想你正在寻找这个:$input = array("teamA","teamB","teamC");$data = [];foreach($input as $value){&nbsp; &nbsp; $assign = "50"; /* The data just temp */&nbsp; &nbsp; $data[$value] = $assign;}echo $data["teamA"];如果对于所有键都相同:$assign$data = array_fill_keys($input, 50);

温温酱

<?php$input = array("teamA","teamB","teamC");$data = array();foreach($input as $value){&nbsp; &nbsp; $assign = "50";&nbsp; &nbsp; $data[$value] = $assign;}echo $data["teamA"];?>将新的键值对分配给数组,可以通过以下方式访问:,其中键由初始给定数组的值组成$data[$value] = $assign;$input = array("teamA","teamB","teamC")
打开App,查看更多内容
随时随地看视频慕课网APP