1

I have an array that looks like this:

PHP Syntax (Toggle Plain Text)
    array
      0 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '00:00' (length=5)
          2 => string '34.83' (length=5)
          3 => string '12/08/2011' (length=10)
          4 => string '00:00' (length=5)
          5 => string '34.83' (length=5)
      1 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '01:00' (length=5)
          2 => string '32.07' (length=5)
          3 => string '12/08/2011' (length=10)
          4 => string '01:00' (length=5)
          5 => string '32.07' (length=5)
      2 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '02:00' (length=5)
          2 => string '28.69' (length=5)
          3 => string '12/08/2011' (length=10)
          4 => string '02:00' (length=5)
          5 => string '28.69' (length=5)
      3 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '03:00' (length=5)
          2 => string '28.17' (length=5)
          3 => string '12/08/2011' (length=10)
          4 => string '03:00' (length=5)
          5 => string '28.17' (length=5)

What I'm trying to do is average each 2/5 element with eachother into > a new array with the Date/Time. so have something like

array
0 => 
array
0 => string '12/08/2011' (length=10)
1 => string '00:00' (length=5)
2 => string 'AVERAGE OF 2&5' (length=5)
PHP Syntax (Toggle Plain Text)
      1 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '01:00' (length=5)
          2 => string 'AVERAGE OF 2&5' (length=5)
      2 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '02:00' (length=5)
          2 => string 'AVERAGE OF 2&5' (length=5)
      3 => 
        array
          0 => string '12/08/2011' (length=10)
          1 => string '03:00' (length=5)
          2 => string 'AVERAGE OF 2&5' (length=5)

Any help would be appreciated, i've been trying ot figure this out for a couple of hours now and can't wrap my head around achieving this? ... Do i need to explode the elements to their own array, average then reinsert?

thank you.

2
  • Explode what? Those look to be numbers already, so there should be no need to do any further exploding. Commented Dec 7, 2011 at 18:45
  • Sorry i had the Titel wrong-I'm looking to Average each 2/5 value of the original array and insert that value to the corresponding key... Commented Dec 7, 2011 at 18:56

2 Answers 2

2

If you have php5.3+ you can do this in two lines (and one if you like difficult to read function calls) using a closure with array_map:

$f = function($x) { return array($x[0], $x[1], ($x[2]+$x[5])/2); };
$new = array_map($f, $arr);
print_r($new);

The closure returns a new array with the two values you requested and averages the values of keys 2 and 5.


If you don't have PHP5.3, use a regular function:

function dan_is_awesome($x) { return array($x[0], $x[1], ($x[2]+$x[5])/2); };
$new = array_map('dan_is_awesome', $arr);
print_r($new);

array_map() takes as its first argument a callback function that it then performs on each element of the input array.


UPDATE

Stack Overflow IS NOT a substitute for logical thinking. "Can you do this for me" is not a legitimate question. However ...

If you needed to accomodate the requirements listed in the comment below (averaging keys 2/5/7/10/12/15 ... and so on to the end of the array) you could do:

function dan_is_awesome($x)
{
  $avg_vals = array($x[2], $x[5]);

  $i = 10;
  while ($i < count($x)) {
    if ($i % 10 == 0     // divisible by 10
      || $i-5 % 10 == 0  // divisible by 5
      || $i % 2 == 0     // divisible by 2
    ) { 
      $avg_vals[] = $x[$i];
    }
    ++$i;
  }

  $avg = array_sum($avg_vals) / count($avg_vals);

  return array($x[0], $x[1], $avg);
};

$new = array_map('dan_is_awesome', $arr);
print_r($new);
Sign up to request clarification or add additional context in comments.

7 Comments

Ya'll both just saved me loads of time, I coudln't comprehend it for some reason. Anyway thank you.
what if the array is is 132 values, and i need value 2,5 and then every 5th after that .. so 7/10 12/15 17/20 and so on up to 132
@tgr0ss Updated code with example of averaging values as you requested
Nice function name. I'd agree at this moment.
Yeah you see that function name in a lot of serious programming textbooks, so i figured, "When in Rome" ... :)
|
0

I prefer rdlowrey's methods, but this is likely easier to understand for most people.

foreach ($arr as $key => $subArray) {
    $ave = ($subArray[2] + $subArray[5]) / 2;
    $arr[$key] = array($subArray[0], $subArray[1], $ave);
}

3 Comments

what if the array is is 132 values, and i need value 2,5 and then every 5th after that .. so 7/10 12/15 17/20 and so on up to 132
What did you try? Read the code, and try to understand it. THe modification needed should be quite clear to you.
I tried to do a for loop around it with $i=2;$i<132;i+=5 and one for n=5 " " .. Which then gave me undefined offset in five before i even did anything. .. otherwise I'd do one giant query with a + $subArray[2] +""[5] +""[7]+""[10] and so on?

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.