【问题标题】:How to loop through a mysql array and create another array with its content depending on a variable $i如何循环遍历 mysql 数组并根据变量 $i 创建另一个包含其内容的数组
【发布时间】:2023-03-05 05:03:01
【问题描述】:

我正在尝试在 php 中创建一个循环,该循环将通过 MySQL 数组并根据变量获取一些信息。

MySQL中的表是这样的:id,slot1,slot2,slot3,...slot12。

$max_player_slots = 5;

//***Create Inventory Array**//

    $inventory_info = "SELECT * from inventory where id = $player_id";
    $inventory_info2 = mysql_query($inventory_info) or die ("Couldn't get inventorys    stats");
    $inventory_info3 = mysql_fetch_array($inventory_info2);


//depending on max player slots insert that many values into &invSlots array

    for( $i = 1, $invSlots = array(); $i > $max_player_slots; $i++){

        $invSlots[$i] = $inventory_info3[$i]; //here is the problem i dont know how to insert the diferent values using $i

    }


        $json = $invSlots;
        $encoded = json_encode($json);
        die ($encoded);

我希望在此之后的 $invSlots 数组包含:

$invSlots = array(slot1, slot2, slot3, slot4, slot5);

但我只是得到一个错误。

我认为原因是因为 mysql 数组信息存储在关联数组中并且我试图以数字方式访问它?但我不知道如何解决。

【问题讨论】:

  • “但我只是得到一个错误。”...什么错误?
  • 我没有收到错误消息,它只是不起作用,似乎进入了无限循环。

标签: php mysql json loops


【解决方案1】:

不应该

for( $i = 1, $invSlots = array(); $i < $max_player_slots; $i++){

&lt; 而不是&gt;?

【讨论】:

  • 嗯,这很尴尬...抱歉浪费你的时间我检查了很多次,我都被屏蔽了。
  • 没问题 ;) 有时我也会遇到同样的问题,浪费时间解决这些简单的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
相关资源
最近更新 更多