How to add elements to an empty array in PHP?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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;
Don't arrays in PHP have an add method, for example, cart.add(13)?
php arrays variables
add a comment |
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;
Don't arrays in PHP have an add method, for example, cart.add(13)?
php arrays variables
add a comment |
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;
Don't arrays in PHP have an add method, for example, cart.add(13)?
php arrays variables
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;
Don't arrays in PHP have an add method, for example, cart.add(13)?
php arrays variables
php arrays variables
edited Apr 14 '14 at 8:20
Peter Mortensen
13.9k1987114
13.9k1987114
asked Mar 24 '09 at 9:35
AquinasTubAquinasTub
3,00051814
3,00051814
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
Both array_push and the method you described will work.
$cart = array();
$cart = 13;
$cart = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Is the same as:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
161
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the$cart = 13method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.
– Mattygabe
Jan 15 '11 at 5:10
55
Is it just me or does the$cart=...syntax, at first glance, look like a variable assignment and not an implicit array_push?
– Brad Hein
Feb 5 '14 at 16:36
4
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
3
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
11
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
|
show 6 more comments
It's better to not use array_push and just use what you suggested. The functions just add 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';
10
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
3
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
1
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
add a comment |
Based on my experience, you solution is fine(best) when keys are not important:
$cart = ;
$cart = 13;
$cart = "foo";
$cart = obj;
add a comment |
You can use array_push.
It adds the elements to the end of the array, like in a stack.
You could have also done it like this:
$cart = array(13, "foo", $obj);
add a comment |
REMEMBER, this method overwrites first array, so use only when you are sure!
$arr1 = $arr1 + $arr2;
(see source)
1
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
2
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
add a comment |
Your first option works very fine for me.
$cart = array();
cart = 1:
$cart = 'foo':
$cart = 3;
2
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
add a comment |
$products_arr["passenger_details"]=array();
array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"isuru.eshan@gmail.com"));
echo "<pre>";
echo json_encode($products_arr,JSON_PRETTY_PRINT);
echo "</pre>";
//OR
$countries = array();
$countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
foreach ($countries as $country){
echo "<pre>";
echo print_r($country);
echo "</pre>";
}
add a comment |
When one wants elements to be added with zero-based element indexing, I guess this will work as well:
// 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';
add a comment |
It's called array_push: http://il.php.net/function.array-push
2
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f676677%2fhow-to-add-elements-to-an-empty-array-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Both array_push and the method you described will work.
$cart = array();
$cart = 13;
$cart = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Is the same as:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
161
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the$cart = 13method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.
– Mattygabe
Jan 15 '11 at 5:10
55
Is it just me or does the$cart=...syntax, at first glance, look like a variable assignment and not an implicit array_push?
– Brad Hein
Feb 5 '14 at 16:36
4
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
3
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
11
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
|
show 6 more comments
Both array_push and the method you described will work.
$cart = array();
$cart = 13;
$cart = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Is the same as:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
161
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the$cart = 13method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.
– Mattygabe
Jan 15 '11 at 5:10
55
Is it just me or does the$cart=...syntax, at first glance, look like a variable assignment and not an implicit array_push?
– Brad Hein
Feb 5 '14 at 16:36
4
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
3
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
11
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
|
show 6 more comments
Both array_push and the method you described will work.
$cart = array();
$cart = 13;
$cart = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Is the same as:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
Both array_push and the method you described will work.
$cart = array();
$cart = 13;
$cart = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Is the same as:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
edited Nov 22 '18 at 8:58
Yoram de Langen
3,97721728
3,97721728
answered Mar 24 '09 at 9:42
Bart S.Bart S.
7,53811526
7,53811526
161
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the$cart = 13method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.
– Mattygabe
Jan 15 '11 at 5:10
55
Is it just me or does the$cart=...syntax, at first glance, look like a variable assignment and not an implicit array_push?
– Brad Hein
Feb 5 '14 at 16:36
4
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
3
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
11
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
|
show 6 more comments
161
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the$cart = 13method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.
– Mattygabe
Jan 15 '11 at 5:10
55
Is it just me or does the$cart=...syntax, at first glance, look like a variable assignment and not an implicit array_push?
– Brad Hein
Feb 5 '14 at 16:36
4
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
3
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
11
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
161
161
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the
$cart = 13 method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.– Mattygabe
Jan 15 '11 at 5:10
As stated in the PHP documentation, if you're only pushing a single element every time (like in a loop) or a single element once, it's best to use the
$cart = 13 method not only because it's less characters to do the same operation, but it also doesn't impose the performance overhead of a function call, which array_push() would. Edit: But, great answer. Effectively the same, and majority of uses won't even notice a performance difference, but helps to know those nuances.– Mattygabe
Jan 15 '11 at 5:10
55
55
Is it just me or does the
$cart=... syntax, at first glance, look like a variable assignment and not an implicit array_push?– Brad Hein
Feb 5 '14 at 16:36
Is it just me or does the
$cart=... syntax, at first glance, look like a variable assignment and not an implicit array_push?– Brad Hein
Feb 5 '14 at 16:36
4
4
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
It definitely does to me. I wouldn't mind an explanation of why its not an assignment.
– limeandcoconut
May 20 '14 at 4:05
3
3
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
$cart = 13; is faster. has less characters and looks better.
– Gal Bracha
Jul 28 '14 at 6:54
11
11
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
I'll just offer my alternative viewpoint that it's VERY confusing for other language programmers to read the syntax of cart =..., I've got experience with a lot of languages and I'd never guess that's what it does.
– Erti-Chris Eelmaa
Oct 13 '16 at 18:02
|
show 6 more comments
It's better to not use array_push and just use what you suggested. The functions just add 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';
10
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
3
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
1
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
add a comment |
It's better to not use array_push and just use what you suggested. The functions just add 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';
10
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
3
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
1
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
add a comment |
It's better to not use array_push and just use what you suggested. The functions just add 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';
It's better to not use array_push and just use what you suggested. The functions just add 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';
edited Jul 31 '15 at 7:16
kamal pal
3,96451939
3,96451939
answered Mar 24 '09 at 9:47
OISOIS
8,2282239
8,2282239
10
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
3
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
1
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
add a comment |
10
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
3
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
1
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
10
10
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
"If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated = statements" php.net/manual/en/function.array-push.php#84959
– Ollie Glass
Dec 18 '10 at 17:15
3
3
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
Absolutely correct if your use-case is adding a single item or items one at a time. If all values are known at the same time, it's probably best just to use the array_push notation depending on how many items must be added the extra characters from re-typing the array name each time may be more of a performance hindrance than the function call over-head. As always, judgment should be exercised when choosing. Good answers!
– Mattygabe
Jan 15 '11 at 5:13
1
1
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
This answer is the most complete.
– Lokiare
May 24 '18 at 16:19
add a comment |
Based on my experience, you solution is fine(best) when keys are not important:
$cart = ;
$cart = 13;
$cart = "foo";
$cart = obj;
add a comment |
Based on my experience, you solution is fine(best) when keys are not important:
$cart = ;
$cart = 13;
$cart = "foo";
$cart = obj;
add a comment |
Based on my experience, you solution is fine(best) when keys are not important:
$cart = ;
$cart = 13;
$cart = "foo";
$cart = obj;
Based on my experience, you solution is fine(best) when keys are not important:
$cart = ;
$cart = 13;
$cart = "foo";
$cart = obj;
edited May 5 '18 at 18:26
answered Oct 14 '15 at 13:15
fico7489fico7489
3,01022447
3,01022447
add a comment |
add a comment |
You can use array_push.
It adds the elements to the end of the array, like in a stack.
You could have also done it like this:
$cart = array(13, "foo", $obj);
add a comment |
You can use array_push.
It adds the elements to the end of the array, like in a stack.
You could have also done it like this:
$cart = array(13, "foo", $obj);
add a comment |
You can use array_push.
It adds the elements to the end of the array, like in a stack.
You could have also done it like this:
$cart = array(13, "foo", $obj);
You can use array_push.
It adds the elements to the end of the array, like in a stack.
You could have also done it like this:
$cart = array(13, "foo", $obj);
edited Mar 24 '09 at 10:03
answered Mar 24 '09 at 9:37
andiandi
8,87394245
8,87394245
add a comment |
add a comment |
REMEMBER, this method overwrites first array, so use only when you are sure!
$arr1 = $arr1 + $arr2;
(see source)
1
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
2
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
add a comment |
REMEMBER, this method overwrites first array, so use only when you are sure!
$arr1 = $arr1 + $arr2;
(see source)
1
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
2
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
add a comment |
REMEMBER, this method overwrites first array, so use only when you are sure!
$arr1 = $arr1 + $arr2;
(see source)
REMEMBER, this method overwrites first array, so use only when you are sure!
$arr1 = $arr1 + $arr2;
(see source)
edited May 23 '17 at 11:47
Community♦
11
11
answered May 3 '15 at 17:05
T.ToduaT.Todua
32k12136141
32k12136141
1
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
2
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
add a comment |
1
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
2
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
1
1
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
Why the downvote, can someone explain why this is bad? is it insecure?
– Sandy
Jan 18 '17 at 14:14
2
2
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
@SandyBeach it's not an answer
– mateos
Mar 25 '17 at 6:39
add a comment |
Your first option works very fine for me.
$cart = array();
cart = 1:
$cart = 'foo':
$cart = 3;
2
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
add a comment |
Your first option works very fine for me.
$cart = array();
cart = 1:
$cart = 'foo':
$cart = 3;
2
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
add a comment |
Your first option works very fine for me.
$cart = array();
cart = 1:
$cart = 'foo':
$cart = 3;
Your first option works very fine for me.
$cart = array();
cart = 1:
$cart = 'foo':
$cart = 3;
answered Oct 12 '18 at 14:10
Emmanuel DavidEmmanuel David
246
246
2
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
add a comment |
2
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
2
2
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
Line 4 will overwrite the entire array.
– Blacksilver
Oct 12 '18 at 14:32
add a comment |
$products_arr["passenger_details"]=array();
array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"isuru.eshan@gmail.com"));
echo "<pre>";
echo json_encode($products_arr,JSON_PRETTY_PRINT);
echo "</pre>";
//OR
$countries = array();
$countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
foreach ($countries as $country){
echo "<pre>";
echo print_r($country);
echo "</pre>";
}
add a comment |
$products_arr["passenger_details"]=array();
array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"isuru.eshan@gmail.com"));
echo "<pre>";
echo json_encode($products_arr,JSON_PRETTY_PRINT);
echo "</pre>";
//OR
$countries = array();
$countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
foreach ($countries as $country){
echo "<pre>";
echo print_r($country);
echo "</pre>";
}
add a comment |
$products_arr["passenger_details"]=array();
array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"isuru.eshan@gmail.com"));
echo "<pre>";
echo json_encode($products_arr,JSON_PRETTY_PRINT);
echo "</pre>";
//OR
$countries = array();
$countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
foreach ($countries as $country){
echo "<pre>";
echo print_r($country);
echo "</pre>";
}
$products_arr["passenger_details"]=array();
array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"isuru.eshan@gmail.com"));
echo "<pre>";
echo json_encode($products_arr,JSON_PRETTY_PRINT);
echo "</pre>";
//OR
$countries = array();
$countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
foreach ($countries as $country){
echo "<pre>";
echo print_r($country);
echo "</pre>";
}
edited Nov 22 '18 at 6:42
answered Nov 22 '18 at 6:37
Isuru EshanIsuru Eshan
33
33
add a comment |
add a comment |
When one wants elements to be added with zero-based element indexing, I guess this will work as well:
// 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';
add a comment |
When one wants elements to be added with zero-based element indexing, I guess this will work as well:
// 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';
add a comment |
When one wants elements to be added with zero-based element indexing, I guess this will work as well:
// 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';
When one wants elements to be added with zero-based element indexing, I guess this will work as well:
// 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';
answered Jun 23 '14 at 9:19
Gestix TeamGestix Team
171
171
add a comment |
add a comment |
It's called array_push: http://il.php.net/function.array-push
2
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
add a comment |
It's called array_push: http://il.php.net/function.array-push
2
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
add a comment |
It's called array_push: http://il.php.net/function.array-push
It's called array_push: http://il.php.net/function.array-push
answered Mar 24 '09 at 9:37
Assaf LavieAssaf Lavie
43.1k28127184
43.1k28127184
2
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
add a comment |
2
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
2
2
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
Answers with links are not helpful unless they also quote the relevant data in case the linked page disappears.
– Lokiare
May 24 '18 at 16:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f676677%2fhow-to-add-elements-to-an-empty-array-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown