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;
}
}
?>
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
while($row = mysqli_fetch_array($result))
{
$source = $row['source'];
$items = array(
"id" => $id,
"source" => $source,
);
$_SESSION['items'][] = $items;
}
Multi - dimensional array
You probably want to store another array in $_SESSION['items'];
which makes it multi dimensional array.
what []
will do is that it stack the next fed values to the next available key
Means first value of $item
will be stored in $_SESSION['items'][0]
2nd value of $item
will be stored in $_SESSION['items'][1]
and so on ..
For this case
// Based on pr1nc3's answer
$_SESSION['items'] = array(); // create a new array
foreach($myResult as $row)
$_SESSION['items'][] = $row;
The []
is the magic here: it says to PHP "Take the array identified before []
and push this item onto it" - in fact, this is essentially a shorthand for the array_push
method:
$a = $b = array();
$c = array(1,2,3,4,5,6);
foreach($c as $value) {
$a[] = $value;
array_push($b, $value);
}
After the above, $a
and $b
will look the same in a var_dump
call.
From the doc:
array_push — Push one or more elements onto the end of array
...Has the same effect as:
$array[] = $var;
Let's apply that to your case :
$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.
Hope it helps !