Php 向数组中添加项
我对PHP数组的了解有限。我读过这本书,但它没有足够的价值来解释这一点,使我无法诚实地理解它 我在if{}else{}语句中有以下内容,这是else{} 假设我有以下几项:Php 向数组中添加项,php,arrays,Php,Arrays,我对PHP数组的了解有限。我读过这本书,但它没有足够的价值来解释这一点,使我无法诚实地理解它 我在if{}else{}语句中有以下内容,这是else{} 假设我有以下几项: 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 如果使用项A调用此函数,则将创建id为1且源为foo的数组,这就是放置在数组中
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
如果使用项A调用此函数,则将创建id为1且源为foo的数组,这就是放置在数组中的全部内容。数组被转储,如下所示:
array(2) { ["id"]=> string(2) "25" ["source"]=> string(64) "https://www.alphahq.org/shop/views/assets/images/items/item2.jpg" }
现在,如果在设置时再次调用项B的函数,数组将更改为项B的变量,该怎么办
如何将项目A和项目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;
}
}
?>
首先要做到:
$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
您可能希望在$_会话['items']中存储另一个数组;这使它成为一个多维数组
[]将下一个fed值堆叠到下一个可用键
这意味着$item的第一个值将存储在$\u会话['items'][0]中,$item的第二个值将存储在$\u会话['items'][1]中,依此类推……
// Based on pr1nc3's answer
$_SESSION['items'] = array(); // Create a new array
foreach($myResult as $row)
$_SESSION['items'][] = $row;
[]是这里的魔法:它告诉PHP获取在[]之前标识的数组并将此项推送到它上面-事实上,这本质上是array_push方法的简写:
在上述操作之后,$a和$b在var_转储调用中的外观相同。来自:
array_push-将一个或多个元素推到数组的末尾
…具有与以下相同的效果:
$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.
这是可以的,但是您还需要将$items初始化为$items=array;为了安全起见,这看起来是可行的,虽然我讨厌被填鸭式的喂养,但我不会只是复制和粘贴。请在您的答案中解释您的代码。提前谢谢你!:这看起来是可行的,尽管我讨厌被填鸭式的喂养,我不只是去复制和粘贴。请在您的答案中解释您的代码。提前谢谢你!:我添加了一些解释,但为了更好地理解,我建议您在google上学习更好的多维数组教程。有许多可用的,这似乎不是建设。我添加了一个foreach方法来回显每个数组。请点击,然后将65改为25,你会明白我的意思。你是什么意思?我在代码中添加了:foreach$_SESSION['items'],因为$key{echo.$id.等于源:.$source.},我只看到脚本要获取的url中当前的id。而不是以前输入的所有值的列表。当您只输入一次数组时,如何期望数组中有多个值。。检查我更新的答案,你可能会得到你做错了什么的提示。
$a = $b = array();
$c = array(1, 2, 3, 4, 5, 6);
foreach($c as $value) {
$a[] = $value;
array_push($b, $value);
}
$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.