Reputation: 21
So i have array which is something like this
Array (323)
0 => Array (2)
plotis => "2000"
aukstis => "1909"
1 => Array (2)
plotis => "2100"
aukstis => "1909"
2 => Array (2)
plotis => "2200"
aukstis => "1909"
3 => Array (2)
plotis => "2300"
aukstis => "1909"
4 => Array (2)
plotis => "2400"
aukstis => "1909"
5 => Array (2)
plotis => "2500"
aukstis => "1909"
and so on
I need to make 2 arrays 1 should have all plotis value and other aukstis value . But the problem is its first time i see array in array ( new to php )
Upvotes: 0
Views: 39
Reputation: 1221
$plotis = Array();
$aukstis = Array();
for($i=0; $i<count($mainArray); $i++)
{
$plotis[] = $mainArray[$i]['plotis'];
$aukstis[] = $mainArray[$i]['aukstis'];
}
print_r($plotis); //to display plotis array
print_r($aukstis); //to display aukstis array
Upvotes: 0
Reputation: 76646
What you have is a multi-dimensional array. To access the values inside the array, you need to loop through them. For this purpose, we can use the very handy foreach
construct.
The basic syntax is as follows:
foreach (array_expression as $key => $value {
# code ...
}
In this case, the code would be:
$plotis = $aukstis = array(); // Initialize both of them as empty arrays
foreach ($array as $sub) {
$plotis[] = $sub['plotis']; // Push the values to
$aukstis[] = $sub['aukstis']; // respective arrays
}
Of course, this can be shortened down to fewer lines of code with array_map()
and the like, but since you said you're a beginner, I thought it'd be a good idea to use a plain simple foreach
so you could understand it easier.
Upvotes: 1
Reputation: 68476
You can use array_map
for this..
$plotis_arr = array_map(function ($v){ return $v['plotis'];},$yourarray);
$aukstis_arr = array_map(function ($v){ return $v['aukstis'];},$yourarray);
Upvotes: 1