简体   繁体   English

如何在 PHP 中修复此阵列?

[英]How can I fix this array in PHP?

I have this array of website names (text strings):我有这个网站名称数组(文本字符串):

#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',
);

And I have this array of variables:我有这个变量数组:

#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],
);

How I can do a while to automate this without losing the variable at the start of each line of the $sites array?我怎样才能在不丢失 $sites 数组每一行开头的变量的情况下自动执行此操作? like this:像这样:

$i = 0;
while ($i <= count($sitearray) - 1) {
    $sitenamesarray[$i] = $sitepqnt = $sitearray[$i];
    echo $sitenamesarray[$i].'<br />';

    $i++;
}
echo $sitepqnt;

But I can't change the $sitepqnt variable to the new variable.但我无法将 $sitepqnt 变量更改为新变量。 and if I would use those name variables in another code, it would be like using this: $sitearray[15] but if I added another variable inside $sitearray, I would have to change all of those $sitearray[15] to another number, how can I make an array of website names while keeping the custom variables for them (like $sitepqnt)?如果我在另一个代码中使用这些名称变量,就像使用这个:$sitearray[15] 但如果我在 $sitearray 中添加另一个变量,我将不得不将所有这些 $sitearray[15] 更改为另一个数字,如何在为它们保留自定义变量的同时创建一组网站名称(如 $sitepqnt)?

You can go thru the array and create a variable.您可以通过数组 go 并创建一个变量。

$sitearray = [
    'pocb', 'diario', 'mlp', 'watch', 'music', 'games', 'fb', 'tt',
    'tasks', 'years', '2018', '2019', '2020', 'stories', 'nw', 'pequenata',
    'spaceliving', 'nazzevo', 'ls', 'mf', 'wt', 'stake2', 'textmaker', 'thingsido'
];

foreach($sitearray as $value) {
    ${"site$value"} = $value;
}

print_r($GLOBALS);

And then the variables are in the current scope.然后变量在当前 scope 中。 (shortened output) (缩短输出)

[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

So if you echo $sitethingsido;因此,如果您echo $sitethingsido; you will get你会得到

thingsido东西多

If I understood the question right, one possible way would be something like this:如果我理解正确的问题,一种可能的方法是这样的:

<?php

$sitearray = array(
    'pocb',
    'diario',
    'mlp',
    'watch',
    'music',
    'games',
    'fb',
    'tt',
    'tasks',
    'years',
    '2018',
    '2019',
    '2020',
    'stories',
    'nw',
    'pequenata',
    'spaceliving',
    'nazzevo',
    'ls',
    'mf',
    'wt',
    'stake2',
    'textmaker',
    'thingsido',
);
$sites = array();

$i = 0;
while ($i <= count($sitearray) - 1) {
    // Generate variable & key name
    $site_key = 'site_' . $sitearray[$i];
    // Fill $sites array
    $sites[$site_key] = $sitearray[$i];
    // Create auto-generated variables like '$site_textmaker'
    $$site_key = $sitearray[$i];

    $i++;
}

var_dump($sites); /* -->
array(24) {
    ["site_pocb"] => string(4) "pocb"
    ["site_diario"] => string(6) "diario"
    ["site_mlp"] => string(3) "mlp"
    ["site_watch"] => string(5) "watch"
    ["site_music"] => string(5) "music"
    ["site_games"] => string(5) "games"
    ["site_fb"] => string(2) "fb"
    ["site_tt"] => string(2) "tt"
    ["site_tasks"] => string(5) "tasks"
    ["site_years"] => string(5) "years"
    ["site_2018"] => string(4) "2018"
    ["site_2019"] => string(4) "2019"
    ["site_2020"] => string(4) "2020"
    ["site_stories"] => string(7) "stories"
    ["site_nw"] => string(2) "nw"
    ["site_pequenata"] => string(9) "pequenata"
    ["site_spaceliving"] => string(11) "spaceliving"
    ["site_nazzevo"] => string(7) "nazzevo"
    ["site_ls"] => string(2) "ls"
    ["site_mf"] => string(2) "mf"
    ["site_wt"] => string(2) "wt"
    ["site_stake2"] => string(6) "stake2"
    ["site_textmaker"] => string(9) "textmaker"
    ["site_thingsido"] => string(9) "thingsido"
}
*/

var_dump($sites['site_' . $sitearray[2]]); // -> string(3) "mlp"

var_dump($site_textmaker); // -> string(9) "textmaker"

?>

site_ prefix (instead of site used in the question) was used for better readability. site_前缀(而不是问题中使用的site )用于更好的可读性。 You can freely change it.您可以自由更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM