如何元素添加到在PHP空数组? [英] How to add elements to an empty array in PHP?

查看:208
本文介绍了如何元素添加到在PHP空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义PHP中的数组,如(我不定义它的大小):

If I define an array in PHP such as (I don't define its size):

$cart = array();

我简单地使用下面的添加元素呢?

Do I simply add elements to it using the following?

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

不要在PHP数组有一个附加的方法,例如, cart.add(13)

推荐答案

array_push 以及您所描述的方法会奏效。

Both array_push and the method you described will work.

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

是一样的:

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

// Or 
$cart = array();
array_push($cart, 13, 14);
?>

这篇关于如何元素添加到在PHP空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆