5 Replies - 286 Views - Last Post: 11 November 2019 - 04:47 PM Rate Topic: -----

#1 makamo66   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 104
  • Joined: 04-February 09

How to make an associative array with $key => $value pai

Posted 10 November 2019 - 05:59 PM

This is the cart array:
$cart = array( "1" => "2", "3" => "4", "5" => "6");
var_dump($cart);
array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" }
This is the cart items array:
$cart_items = array();
$cart_items["id"] = $id;
$cart_items["quantity"] = $quantity;
var_dump($cart_items);
array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } }

I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables?

Is This A Good Question/Topic? 0
  • +

Replies To: How to make an associative array with $key => $value pai

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2494
  • View blog
  • Posts: 7,551
  • Joined: 15-January 14

Re: How to make an associative array with $key => $value pai

Posted 11 November 2019 - 10:10 AM

It looks like you already are defining it using variables.
Was This Post Helpful? 0
  • +
  • -

#3 makamo66   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 104
  • Joined: 04-February 09

Re: How to make an associative array with $key => $value pai

Posted 11 November 2019 - 04:30 PM

When I use this:

$cart_items[$id] = $quantity;

or this:

$cart_items = array($id => $quantity);

I get the error: Illegal offset type
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2494
  • View blog
  • Posts: 7,551
  • Joined: 15-January 14

Re: How to make an associative array with $key => $value pai

Posted 11 November 2019 - 04:34 PM

It's probably talking about $id. Array indexes need to be either a string or a number, so $id is probably neither.
Was This Post Helpful? 0
  • +
  • -

#5 makamo66   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 104
  • Joined: 04-February 09

Re: How to make an associative array with $key => $value pai

Posted 11 November 2019 - 04:41 PM

$id is defined like this:

for($i=0; $i<=5; $i++){
if (isset($_REQUEST["element_id_$i"]) ) {
$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
$id = $_SESSION["element_id_$i"];
array_push($_SESSION["element_id"],$id);
}
$id = $_SESSION["element_id"];
}
and $quantity is similar.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2494
  • View blog
  • Posts: 7,551
  • Joined: 15-January 14

Re: How to make an associative array with $key => $value pai

Posted 11 November 2019 - 04:47 PM

So, what's the value of it? Use var_dump on it before trying to make the array to see what it actually is. If it's not what you expect, then work backwards to figure out where it goes wrong.

You're using array_push on it so it looks like you think it's an array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1