将变量分配给数组 php 中的值

发布于 2025-01-08 17:44:14 字数 648 浏览 0 评论 0原文

这就是我想要做的:

  1. 将一个单词拆分为单独的字符。输入的单词来自表格,并且对于每个用户来说可能不同。

  2. 为每个角色分配变量,以便我可以单独操作它们。

到目前为止,她是我的代码(不起作用)。如果这里有很多愚蠢的错误,请道歉,但我是 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:

  1. Split a word into separate charachters. The input word comes from a form and can differ from each user.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住我的手 2025-01-15 17:44:14

您不需要为每个字母创建单独的变量,因为数组中包含所有字母。然后你只需索引数组即可取出每个字母。

我将这样做。

//get the word from the form
$word = $_POST['input'];

//split word into characters 
$characters = str_split($word);


//suppose the word is "jim"
//this prints out 
// j
// i
// m

foreach($characters as $char)
    print $char . "\n"


//now suppose you want to change the first letter so the word now reads "tim"
//Access the first element in the array (ie, the first letter) using this syntax
$characters[0] = "t";

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.

//get the word from the form
$word = $_POST['input'];

//split word into characters 
$characters = str_split($word);


//suppose the word is "jim"
//this prints out 
// j
// i
// m

foreach($characters as $char)
    print $char . "\n"


//now suppose you want to change the first letter so the word now reads "tim"
//Access the first element in the array (ie, the first letter) using this syntax
$characters[0] = "t";
你是年少的欢喜 2025-01-15 17:44:14

我不认为这是一个好主意,但你可以这样做:

<?php
$input = 'Hello world!';
for($i = 0; $i < strlen($input); $i++) {
    ${'character' . $i} = $input[$i];
}

I dont think its a good idea, but heres how you do it:

<?php
$input = 'Hello world!';
for($i = 0; $i < strlen($input); $i++) {
    ${'character' . $i} = $input[$i];
}
寂寞陪衬 2025-01-15 17:44:14

你为什么想要那个?
你可以选择:

$word = 'test';
echo $word[2]; // returns 's'
echo $word{2}; // returns 's'
$word{2} = 'b';
echo $word{2}; //returns 'b'
echo $word; // returns 'tebt'
...

why do you want that?
you can just go with:

$word = 'test';
echo $word[2]; // returns 's'
echo $word{2}; // returns 's'
$word{2} = 'b';
echo $word{2}; //returns 'b'
echo $word; // returns 'tebt'
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文