1

I have an array which looks like this:

array(10) {
  [0]=>
  string(10) "2012-11-03"
  [1]=>
  string(1) "1"
  [2]=>
  string(10) "2012-11-04"
  [3]=>
  string(1) "3"
  [4]=>
  string(10) "2012-11-05"
  [5]=>
  string(1) "2"
  [6]=>
  string(10) "2012-11-06"
  [7]=>
  string(1) "7"
  [8]=>
  string(10) "2012-11-07"
  [9]=>
  string(1) "4"
}

I would like to get a new multidimensional array from this which would have 5 elements, where each element would look like this: $date => $number.

array(5) {
      [0]=> array(2012-11-03 => 1)
      [1]=> array(2012-11-04 => 3)
      [2]=> array(2012-11-05 => 2)
      [3]=> array(2012-11-06 => 7)
      [4]=> array(2012-11-07 => 4)
    }

I would like to use the dates as keys to te values that come after them. (I would eventually like to plot these values on a line chart, where x axis has the date and y has the value)

What kind of a (foreach?) loop can I write to do this?

I am getting this array from the following lines of code:

$data = "$start_date\n$value\n";
file_put_contents($id . '.csv', $data, FILE_APPEND);
$data = file_get_contents($id . '.csv');
$data_array = explode("\n", trim($data, "\n"));
var_dump($data_array); exit;
d12n
  • 841
  • 2
  • 10
  • 20
  • Not quite clear on what you're looking for. What would the resultant array in this case? – Patrick White Nov 13 '12 at 16:57
  • 1
    GordonM: Damn, I always forget to accept the answers by clicking on the tick button, I just upvote them and thank the users. You are right, I should pay more attention to that. PWhite: I suck at formulating questions in the headline, sorry! I will edit my question to make it more clear. – d12n Nov 13 '12 at 17:00

2 Answers2

6

I assume that your array contains an even number of elements, as expected.

You can use the following snippet:

<?php

$newArray = [];
for ($i = 0; $i < count($data_array); $i += 2) {
    $newArray[$data_array[$i]] = $data_array[$i + 1];
}

^ Above code says that we start at index 0 of $data_array, which is a date. It makes a key in newArray from the even element, and attaches the value of the next odd element to this key. The array will look like this:

Array(
    '2012-11-03' => "1",
    '2012-11-04' => "3",
    '2012-11-05' => "2",
    ...
)
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • Out of curiosity, which of our answers would be faster, and why? I realise there's probably very little in it ;) – Jim O'Brien Nov 13 '12 at 17:03
  • Yeah, funny that it is almost exactly the same code ;-) I'm curious too! – MC Emperor Nov 13 '12 at 17:07
  • Thank you, this seems like what I was looking for, I just wasn't sure whether I could go with the even/odd approach or whether there was another way. I will try this now, thanks a lot!! – d12n Nov 13 '12 at 17:10
  • 1
    To answer the speed question: Using a dummy array with 200,000 items (100k pairs of similar data to the example), this answer performed consistently better than Jim's.... by about `0.005` seconds. So effectively, I think they are pretty much the same. PHP5 (Zend) on Windows 7. – joequincy Nov 13 '12 at 18:24
4
<?php
$output = array();
for($i = 0; $i < count($data_array); $i++) {
    $output[$data_array[$i]] = $data_array[++$i];
}
?>

I'm afraid I haven't tested this, but it should work.

Jim O'Brien
  • 2,512
  • 18
  • 29
  • Not sure if you'll be notified on it, but I responded above to your curiosity about the performance of your answer vs. MC Emperor's answer. TL;DR they're effectively the same. – joequincy Nov 13 '12 at 18:34
  • 1
    Effective-wise, they don't differ, they say at http://stackoverflow.com/questions/13365446/which-of-the-following-two-code-snippets-is-faster-and-why/13365751#13365751. – MC Emperor Nov 14 '12 at 00:55