doutan4831 2013-11-10 06:21
浏览 15

将项添加到数组

So my knowledge of php arrays is limited. I have read the PHP Arrays - Manual But it did not explain this in enough value for me to grasp it honestly.

I have the following in a if{} else{} statement with this being the 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;

Let's say I have the following 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

if this function gets called with item A, the array is created with an id of 1 and source of foo and that is all that is placed in the array. And the array is dumped out like:

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

Now what if the function gets called again for item B right now as it sets, the array will changed to the variables for item B Correct?

How would I be able to add item A and item B to the same array and define them as separate items.

So basically how could I do this:

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

And just build the array as an item is added. I am saving the array in a session, could this be helpful in retrieving and adding to the array each time the function is called?

Just for additional help my full shopping-functions.php file is included below for reference.

<?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条回答 默认 最新

  • drd43058 2013-11-10 06:24
    关注

    first do this

    $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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)