Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add items to an array

Tags:

arrays

php

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 the following?

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;
        }
    }
?>
like image 760
user2948950 Avatar asked Sep 12 '26 18:09

user2948950


1 Answers

From the documentation:

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.
like image 194
S.Thiongane Avatar answered Sep 14 '26 09:09

S.Thiongane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!