Jak dodać elementy do pustej tablicy w PHP?

Jeśli zdefiniuję tablicę w PHP taką jak (nie definiuję jej rozmiaru):

$cart = array();

Czy po prostu dodaję do niego elementy używając następujących elementów?

$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;

Czy tablice w PHP nie mają metody add, na przykład cart.add(13)?

Author: Peter Mortensen, 2009-03-24

7 answers

Obie array_push i metoda, którą opisałeś, zadziała.

<?php
$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc
?>

Jest tym samym co:

<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);

// Or 
$cart = array();
array_push($cart, 13, 14);
?>
 625
Author: Bart S.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-31 07:16:51

Lepiej nie używać array_push i użyj tego, co zasugerowałeś. Funkcje po prostu dodać overhead.

//We don't need to define the array, but in many cases it's the best solution.
$cart = array();

//Automatic new integer key higher than the highest 
//existing integer key in the array, starts at 0.
$cart[] = 13;
$cart[] = 'text';

//Numeric key
$cart[4] = $object;

//Text key (assoc)
$cart['key'] = 'test';
 59
Author: OIS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-31 07:16:28

Z mojego doświadczenia wynika, że rozwiązanie jest dobre (Najlepsze), gdy klucze nie są ważne:

$cart = [];
$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;
 8
Author: fico7489,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-05 18:26:35

Możesz użyć array_push. Dodaje elementy na końcu tablicy, jak w stosie.

Mogłeś też zrobić to tak:

$cart = array(13, "foo", $obj);
 7
Author: andi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-24 10:03:22

Pamiętaj, że ta metoda nadpisuje pierwszą tablicę, więc używaj tylko wtedy, gdy jesteś pewien!

$arr1 = $arr1 + $arr2;

(Zobacz źródło )

 2
Author: T.Todua,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:29

Jeśli ktoś chce, aby elementy były dodawane z indeksowaniem elementów od zera, myślę, że to również zadziała:

// adding elements to an array with zero-based index
$matrix= array();
$matrix[count($matrix)]= 'element 1';
$matrix[count($matrix)]= 'element 2';
...
$matrix[count($matrix)]= 'element N';
 -1
Author: Gestix Team,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-23 09:19:34
 -2
Author: Assaf Lavie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-24 09:37:48