将项添加到数组

时间:2013-11-10 06:21:04

标签: php arrays

所以我对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调用此函数,则会使用id 1foo的源创建数组,这就是放在数组中的所有内容。并且数组被抛出:

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 Aitem 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 个答案:

答案 0 :(得分:0)

首先这样做

$items = array(); // it's a good practice to initialize first
while($row = mysqli_fetch_array($result))
{
    $items[] = array( // these [] means it will append to last part of array
       "id" => $id,
       "source" => $row['source'], // key with source
    );
}
 // here if you var_dump($items) you will see the result

答案 1 :(得分: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]中,依此类推......

答案 2 :(得分:0)

对于这种情况

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

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

[]在这里是神奇的:它告诉PHP“在[]之前识别出的数组并将此项目推到它上面” - 事实上,这实际上是{{1方法:

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电话中看起来会相同。

答案 3 :(得分:0)

来自doc

array_push — Push one or more elements onto the end of array
...Has the same effect as:

$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.

希望它有所帮助!