PHP上有两个值在数组上

I am trying to make mini shop.
How to add one key, two values to an array?

I tried this:

array(
"My Item" => "images/photo.jpg" => "9,99",
"T-Shirt" => "images/tshirt.jpg" => "19,99");

(I do not want use database)

Like this:

array(
    "My Item" => array("images/photo.jpg", "9,99"),
    "T-Shirt" => array("images/tshirt.jpg", "19,99")
);

like this:

$array = array();
$array[] = array('1st', '1.1');
$array[] = array('2nd', '1.2');
$array[] = array('3rd', '1.3');

The code in your question is invalid array syntax. This would be valid:

array(
"My Item" => array("images/photo.jpg", "9,99"),
"T-Shirt" => array("images/tshirt.jpg", "19,99"));

Remember that this is equivalent to

array(
"My Item" => array(0 => "images/photo.jpg", 1 => "9,99"),
"T-Shirt" => array(0 => "images/tshirt.jpg", 1 => "19,99"));

It may be easier for you to use this array in subsequent code if you use string keys instead. This is optional, of course.

array(
"My Item" => array('image' => "images/photo.jpg", 'price' => "9,99"),
"T-Shirt" => array('image' => "images/tshirt.jpg", 'price' => "19,99"));

Since PHP 5.4 is there a great short syntax:

$articles = [
   'art1' => ['images/photo.jpg', 9.99],
   'art2' => ['images/tshirt.jpg', 19.99]
];

Read more: http://php.net/manual/en/language.types.array.php

Generally:

  • Choose handy key names (I prefer no spaces)
  • Store prices as number without quotes, so you can format them easy