Reputation: 23
I wanted to create three arrays within one MainArray.
The last two Arrays of the three Arrays, belong to one Variable.
How can I separate the two arrays from each other using a comma?
A simple "," wont be accepted, because the MainArray doesn't accept it.
I used explode, implode, join, but nothing worked, the system makes a comma output but this comma isn't recognized as a coding comma to execute the separation of the two arrays.
$array0 = array("carparts"=>$carparts, "info"=>$info);
$i = 0;
while($i <= 1) {
$i++;
$array_season[$i] = array(
"carpartsseason" => $carpartsseason[$i],
"additional_s" => $info_s);
}
$MainArray = array(
$array0,
$array_season
);
As you can see, the comma behind the last $array0
works for the $MainArray
.
But how do I get a comma behind every array of the array_season
?
For example I added after while:
$display = '';
foreach($array_season as $new_output){
$display .= $new_output . ',';
}
recognize the comma behind $new_output
( . ','
).
Then changed $MainArray
:
$MainArray= array(
$array0,
$display
);
But without success. Same to join and implode.
(I worked on it for two days, but without results)
Upvotes: 1
Views: 144
Reputation: 9520
I think there is some confusion here over representations of arrays, the role of commas, and how to add and remove items from arrays.
Commas are used to separate different elements when declaring an array, e.g.:
$fruits = array( 'apple', 'banana', 'carrot' );
Each of the elements of that array is a string.
You can also add strings containing commas to an array:
$stuff = array( 'snow, falling', 'rain, pouring', 'sun, shining' );
The commas are within the elements of the array; they have no special powers over the structure of the array.
When you do this:
$display .= $new_output . ',';
you're adding a comma to the end of $new_output
, and you're adding the whole string to the existing string $display
. This won't form an array; when you later add $display
to your existing array, $display
is still just a long string with some commas in it, a bit like the strings in $stuff
above.
If you want to add items to an array, you can either use array_push
, or the more common shorthand notation:
# declare $display as an array
$display = array();
foreach($array_season as $new_output){
# add $new_output to the $display array
$display[] = $new_output;
}
I am not exactly clear on what structure you're looking for, but if you want to add the $array_season to your existing $MainArray
, you can do so as follows:
# no need to create $array0 unless you need it elsewhere
$MainArray = array("carparts" => $carparts, "info" => $info);
$i = 0;
while($i <= 1) {
$i++;
$MainArray[] = array(
"carpartsseason" => $carpartsseason[$i],
"additional_s" => $info_s);
}
This will push the array containing ('carpartsseason' => $carpartsseason[$i], 'additional_s' => $info_s)
directly on to $MainArray
.
Upvotes: 2
Reputation: 780984
Use array_merge
:
$MainArray = array_merge($array0, $array_season);
Upvotes: 0
Reputation: 50041
Create your 'MainArray':
$MainArray = array($array0);
Then in your while
loop append each of the season arrays to the end of it with $MainArray[] =
:
$i = 0;
while($i <= 1) {
$i++;
$MainArray[] = array(
"carpartsseason" => $carpartsseason[$i],
"additional_s" => $info_s);
}
Upvotes: 0