gpt4 book ai didi

php - 字符串和数组的操作

转载 作者:行者123 更新时间:2023-11-29 17:58:23 24 4
gpt4 key购买 nike

我想问一下php中数组的操作。
我正在从数据库中的一个表中捕获两个单元格,它们保存格式类似于“1;3;6;”的字符串和整个第二个表 - 第一列中有数字,第二列有名称。我使用 explode() 将第一个表中的字符串分成两个数组函数,并将第二个表放入接下来的两个数组中,每个数组有一列。因此,当 print_r()用于这三个数组,我会看到:
错误报告(E_ALL); ini_set('display_errors', 1);

first table
"amount" string transformed into array called $ship_storage_parted_amount Array ( [0] => 5 [1] => 10 [2] => )

"ware id" string transformed into array called $ship_cargo_bay_parted Array ( [0] => 1 [1] => 2 [2] => )

second table transfomed into two arrays $ware_id and $ware_name
table contains 7 rows at all
Array ( [0] => Array ( [ware_id] => 1 [name] => Energized energy cell )

[1] => Array ( [ware_id] => 2 [name] => Depleted energy cell )

[2] => Array ( [ware_id] => 3 [name] => Vegetables )

[3] => Array ( [ware_id] => 4 [name] => Meat ) )

我想实现下一件事:根据 ID 与第一个表中存储的内容相结合,在网站上显示第二个表中适当的相同名称。
因此,它应该看起来像:

Energized energy cell x5
Depleted energy cell x10

当我试图这样向他们展示时:
echo $ware_name[$ship_cargo_bay-1]." x".$ship_storage_parted_amount[$ware_position];
它产生了这样的输出:

Energized energy cell x

哪里$ship_cargo_bay包含所有捕获的字符串“ware id”和 $ware_position是金额计数器。金额计数器指向第三个数组元素,该元素为空;但是当我用 for() 重新组织打印时
for($i=0;$i}lowerthan{$ware_counter;$i++)
{echo $ware_name[$ship_cargo_bay_parted[$i]]." x".$ship_storage_parted_amount[$i];}

我现在有

x5 x10 x x

我的贫乏知识就到此为止了。据我所知,第一次打印尝试根本不起作用,但为什么我在第二次尝试中得到这样的输出超出了我的理解。

最佳答案

终于找到原因了,我犯错的地方。在仔细检查每个变量的名称,打印出所有内容到底包含什么之后,我发现我不知何故拼错了变量名称。

for($i=1;$i<$ware_counter;$i++)
{
echo $ware_name[$ship_storage_parted[$i]]." x ".$ship_storage_parted_amount[$i]."<br/>";
}

这几行正是我所需要的。因此我把它放在这里 - 也许有人会遇到类似的问题。

关于php - 字符串和数组的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48611081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com