2

我对 PHP 数组的了解有限。我已经阅读了PHP Arrays - Manual,但它并没有以足够的价值解释这一点,让我无法诚实地掌握它。

我在一份if{} else{}声明中有以下内容,这是else{}

// Set up an array for the items

while($row = mysqli_fetch_array($result))
{
   $source = $row['source'];
}

$items = array(
   "id" => $id,
   "source" => $source,
);

$_SESSION['items'] = $items;

假设我有以下项目:

Item A with an ID of 1 and source of foo
Item B with an ID of 2 and source of boo
Item C with an ID of 3 and source of goo

如果使用 调用此函数item A,则将使用 id1和 source创建数组foo,这就是放置在数组中的所有内容。数组被丢弃,如下所示:

array(2) { ["id"]=> string(2) "25" ["source"]=> string(64) "https://www.alphahq.org/shop/views/assets/images/items/item2.jpg" }

现在,如果函数在item B设置时再次被调用,数组将更改为item B 正确的变量怎么办?

我如何能够将item A和添加item B到同一个数组并将它们定义为单独的项目?

那么基本上我该怎么做?

array {
  item A {
    id => 1
    source => foo
  }
  item B {
    id => 2
    source => boo
  }
}

并且只需在添加项目时构建数组。我将数组保存在会话中,这对每次调用函数时检索和添加到数组有帮助吗?

只是为了获得更多帮助,我的完整shopping-functions.php文件包含在下面以供参考。

<?php
    session_start();

    require('../../config/database-connect.php');

    // Start a new order for a customer
    $action = $_GET['action'];
    $id     = $_GET['id'];

    // First make sure the action is not blank
    if ($action == '') {

        echo 'Please select an action';
    }

    if ($action == 'add') {

        // Check if the id matches one in the database
        $result = mysqli_query($con,"SELECT id, source FROM items WHERE id='$id'");

        if (mysqli_num_rows($result) == 0) {

            echo 'That id is not valid!';
        }
        else {
            // Set up an array for the items

            while($row = mysqli_fetch_array($result))
            {
                $source = $row['source'];
            }

            $items = array(
                "id" => $id,
                "source" => $source,
            );

            var_dump($items);

            $_SESSION['items'] = $items;
        }
    }
?>
4

4 回答 4

1

文档中:

array_push — Push one or more elements onto the end of array

...具有相同的效果:

$array[] = $var;

让我们将其应用于您的案例:

$items = array(); // Has to be initialised first, somewhere in your code
// ....
// Then after your while loop
$new_item = array("id" => $id, "source" => $source);

array_push($items, $new_item); // Append $new_item inside $items as its last value.
于 2013-11-10T06:46:13.850 回答
0
while($row = mysqli_fetch_array($result))
{
    $source = $row['source'];
    $items = array(
        "id"     => $id,
        "source" => $source,
    );
    $_SESSION['items'][] = $items;
}

Multi - dimensional array

您可能想要存储另一个数组 $_SESSION['items'];,使其成为多维数组。

[]将下一个馈送值堆叠到下一个可用键。

这意味着 的第一个值$item将存储在 中$_SESSION['items'][0],第二个值$item将存储在 中$_SESSION['items'][1],依此类推...

于 2013-11-10T06:25:54.577 回答
0

对于这种情况

// Based on pr1nc3's answer
$_SESSION['items'] = array(); // Create a new array

foreach($myResult as $row)
    $_SESSION['items'][] = $row;

[]就是这里的魔力:它对 PHP 说“获取之前确定的数组[]并将此项推送到它上面” - 实际上,这本质上是该array_push方法的简写:

$a = $b = array();
$c = array(1, 2, 3, 4, 5, 6);

foreach($c as $value) {
    $a[] = $value;
    array_push($b, $value);
}

完成上述操作后,$a在通话$b中看起来会一样。var_dump

于 2013-11-10T06:33:51.290 回答
0

首先这样做:

$items = []; // It's a good practice to initialize first
while($row = mysqli_fetch_array($result))
{
    $items[] = [ // Append to end of array
       "id" => $id,
       "source" => $row['source'], // Key with source
    ];
}
 // Here if you var_dump($items) you will see the result
于 2013-11-10T06:24:43.467 回答