0

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 )

3
  • 1
    Arrays of arrays are not any different than "normal" arrays. E.g. you can access the plotis entry of the first array with $arr[0]['plotis']. Commented Apr 26, 2014 at 9:49
  • Can anyone tell the method which uses least of resources because that array is quite large :/ and it incrases loading time by quite a bit. Commented Apr 26, 2014 at 10:04
  • @user3313750: Then a simple foreach might prove to be faster than array_* functions. Commented Apr 26, 2014 at 10:05

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you are fast tho :)
1

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.

Comments

0
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.