我怎样才能在 PHP 中修复这个数组?

我有这个网站名称数组(文本字符串):


#Array of website names

$sitearray = array(

'pocb',

'diario',

'mlp',

'watch',

'music',

'games',

'fb',

'tt',

'tasks',

'years',

'2018', #10

'2019',

'2020',

'stories',

'nw',

'pequenata',

'spaceliving',

'nazzevo',

'ls',

'mf',

'wt',

'stake2',

'textmaker',

'thingsido',

);

我有这个变量数组:


#Websites array

$array = $sitearray;

$sites = array(

$sitepocb = $array[0],

$sitediario = $array[1],

$sitemlp = $array[2],

$sitewatch = $array[3],

$sitemusic = $array[4],

$sitegames = $array[5],

$sitefb = $array[6],

$sitett = $array[7],

$sitetasks = $array[8],

$siteyears = $array[9],

$site2018 = $array[10], 

$site2019 = $array[11], 

$site2020 = $array[12],

$sitestories = $array[13],

$sitenw = $array[14],

$sitepqnt = $array[15],

$sitesl = $array[16],

$sitenazzevo = $array[17],

$sitels = $array[18],

$sitemf = $array[19],

$sitewt = $array[20],

$sitestake2 = $array[21],

$sitetextmaker = $array[22],

$sitethingsido = $array[23],

);

我如何才能在不丢失 $sites 数组每一行开头的变量的情况下自动执行此操作?像这样:


$i = 0;

while ($i <= count($sitearray) - 1) {

    $sitenamesarray[$i] = $sitepqnt = $sitearray[$i];

    echo $sitenamesarray[$i].'<br />';


    $i++;

}

echo $sitepqnt;

但我无法将 $sitepqnt 变量更改为新变量。如果我要在另一个代码中使用这些名称变量,就像使用这个:$sitearray[15] 但是如果我在 $sitearray 中添加另一个变量,我将不得不将所有这些 $sitearray[15] 更改为另一个数字,我怎样才能制作一个网站名称数组,同时为它们保留自定义变量(如 $sitepqnt)?


繁花不似锦
浏览 94回答 2
2回答

蓝山帝景

您可以遍历数组并创建一个变量。$sitearray = [&nbsp; &nbsp; 'pocb', 'diario', 'mlp', 'watch', 'music', 'games', 'fb', 'tt',&nbsp; &nbsp; 'tasks', 'years', '2018', '2019', '2020', 'stories', 'nw', 'pequenata',&nbsp; &nbsp; 'spaceliving', 'nazzevo', 'ls', 'mf', 'wt', 'stake2', 'textmaker', 'thingsido'];foreach($sitearray as $value) {&nbsp; &nbsp; ${"site$value"} = $value;}print_r($GLOBALS);然后变量在当前范围内。(缩短输出)[sitepocb] => pocb[sitediario] => diario[sitemlp] => mlp[sitewatch] => watch[sitemusic] => music[sitegames] => games[sitefb] => fb[sitett] => tt[sitetasks] => tasks[siteyears] => years[site2018] => 2018[site2019] => 2019[site2020] => 2020[sitestories] => stories[sitenw] => nw[sitepequenata] => pequenata[sitespaceliving] => spaceliving[sitenazzevo] => nazzevo[sitels] => ls[sitemf] => mf[sitewt] => wt[sitestake2] => stake2[sitetextmaker] => textmaker[sitethingsido] => thingsido所以如果你echo $sitethingsido;会得到事物

吃鸡游戏

如果我正确理解了这个问题,一种可能的方法是这样的:<?php$sitearray = array(&nbsp; &nbsp; 'pocb',&nbsp; &nbsp; 'diario',&nbsp; &nbsp; 'mlp',&nbsp; &nbsp; 'watch',&nbsp; &nbsp; 'music',&nbsp; &nbsp; 'games',&nbsp; &nbsp; 'fb',&nbsp; &nbsp; 'tt',&nbsp; &nbsp; 'tasks',&nbsp; &nbsp; 'years',&nbsp; &nbsp; '2018',&nbsp; &nbsp; '2019',&nbsp; &nbsp; '2020',&nbsp; &nbsp; 'stories',&nbsp; &nbsp; 'nw',&nbsp; &nbsp; 'pequenata',&nbsp; &nbsp; 'spaceliving',&nbsp; &nbsp; 'nazzevo',&nbsp; &nbsp; 'ls',&nbsp; &nbsp; 'mf',&nbsp; &nbsp; 'wt',&nbsp; &nbsp; 'stake2',&nbsp; &nbsp; 'textmaker',&nbsp; &nbsp; 'thingsido',);$sites = array();$i = 0;while ($i <= count($sitearray) - 1) {&nbsp; &nbsp; // Generate variable & key name&nbsp; &nbsp; $site_key = 'site_' . $sitearray[$i];&nbsp; &nbsp; // Fill $sites array&nbsp; &nbsp; $sites[$site_key] = $sitearray[$i];&nbsp; &nbsp; // Create auto-generated variables like '$site_textmaker'&nbsp; &nbsp; $$site_key = $sitearray[$i];&nbsp; &nbsp; $i++;}var_dump($sites); /* -->array(24) {&nbsp; &nbsp; ["site_pocb"] => string(4) "pocb"&nbsp; &nbsp; ["site_diario"] => string(6) "diario"&nbsp; &nbsp; ["site_mlp"] => string(3) "mlp"&nbsp; &nbsp; ["site_watch"] => string(5) "watch"&nbsp; &nbsp; ["site_music"] => string(5) "music"&nbsp; &nbsp; ["site_games"] => string(5) "games"&nbsp; &nbsp; ["site_fb"] => string(2) "fb"&nbsp; &nbsp; ["site_tt"] => string(2) "tt"&nbsp; &nbsp; ["site_tasks"] => string(5) "tasks"&nbsp; &nbsp; ["site_years"] => string(5) "years"&nbsp; &nbsp; ["site_2018"] => string(4) "2018"&nbsp; &nbsp; ["site_2019"] => string(4) "2019"&nbsp; &nbsp; ["site_2020"] => string(4) "2020"&nbsp; &nbsp; ["site_stories"] => string(7) "stories"&nbsp; &nbsp; ["site_nw"] => string(2) "nw"&nbsp; &nbsp; ["site_pequenata"] => string(9) "pequenata"&nbsp; &nbsp; ["site_spaceliving"] => string(11) "spaceliving"&nbsp; &nbsp; ["site_nazzevo"] => string(7) "nazzevo"&nbsp; &nbsp; ["site_ls"] => string(2) "ls"&nbsp; &nbsp; ["site_mf"] => string(2) "mf"&nbsp; &nbsp; ["site_wt"] => string(2) "wt"&nbsp; &nbsp; ["site_stake2"] => string(6) "stake2"&nbsp; &nbsp; ["site_textmaker"] => string(9) "textmaker"&nbsp; &nbsp; ["site_thingsido"] => string(9) "thingsido"}*/var_dump($sites['site_' . $sitearray[2]]); // -> string(3) "mlp"var_dump($site_textmaker); // -> string(9) "textmaker"?>site_前缀(而不是site在问题中使用)用于提高可读性。您可以自由更改它。
打开App,查看更多内容
随时随地看视频慕课网APP