将变量分配给数组 php 中的值
这就是我想要做的:
将一个单词拆分为单独的字符。输入的单词来自表格,并且对于每个用户来说可能不同。
为每个角色分配变量,以便我可以单独操作它们。
到目前为止,她是我的代码(不起作用)。如果这里有很多愚蠢的错误,请道歉,但我是 PHP 新手。
<?php
$word = $_POST['input'];
//split word into charachters
$arr1 = str_split($word);
//assigning a variable to each charchter
$bokstaver = array();
while($row = $arr1)
{
$bokstaver[] = $row[];
}
$int_count = count($bokstaver);
$i=0;
foreach ($bokstaver as $tecken) {
$var = 'tecken' . ($i + 1);
$$var = $tecken;
$i++;
}
?>
我希望最终得到与输入中的字符数一样多的 $tecken 变量(名称为 $tecken、t$tecken1、$tecken2 等)。
一如既往,非常感谢所有帮助!
This is what I want to do:
Split a word into separate charachters. The input word comes from a form and can differ from each user.
Assign variables to each charachter so that i can manipulate them separately.
Her's my code so far (which doesn't work). Apoligize if ther's a lot of stupid mistakes here, but I am new to PHP.
<?php
$word = $_POST['input'];
//split word into charachters
$arr1 = str_split($word);
//assigning a variable to each charchter
$bokstaver = array();
while($row = $arr1)
{
$bokstaver[] = $row[];
}
$int_count = count($bokstaver);
$i=0;
foreach ($bokstaver as $tecken) {
$var = 'tecken' . ($i + 1);
$var = $tecken;
$i++;
}
?>
I'd like to end up with as many $tecken variables (With the names $tecken, t$tecken1, $tecken2 etc) as the number of charachters in the input.
All help much appreciated, as always!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要为每个字母创建单独的变量,因为数组中包含所有字母。然后你只需索引数组即可取出每个字母。
我将这样做。
You don't need to create separate variables for each letter because you have all the letters in an array. Then you just index into the array to get out each letter.
Here is how I would do it.
我不认为这是一个好主意,但你可以这样做:
I dont think its a good idea, but heres how you do it:
你为什么想要那个?
你可以选择:
why do you want that?
you can just go with: