2

I have this code right now, it fetches the date and time. I wanted to create kind of a numbered index because I get back about 10 date and times in that one foreach loop. It's in a function and I need to return this array to do something with it elsewhere.

$i = 0;
    foreach ($data as $key =>$value) {
        $new_data = array (
            $i => array (
                "date"=> $value->DATE,
                "time"=> $value->TIME,
            )
        );
        $i++;
    }

I wanted it to look more like this

Array (
    [0] => Array (
        [date] => whatever date
        [time] => whatever time
    )
    [1] => Array (
        [date] => whatever date
        [time] => whatever time
    )
    [2] .. etc
)

Turns out that's not what I'm getting. I get this.

Array (
        [10] => Array (
            [date] => whatever date
            [time] => whatever time
        )

It only prints out in one / just gives me the tenth. How would I get it to keep ++ing the $i variable to allow me to get it in the numbered indices? Why is it only showing me the tenth?

3
  • 1
    it could be a typo but you are missing a double quote after time "time =>
    – Fabio
    Jun 6, 2013 at 17:04
  • If you're not using $key, then omit it in the foreach statement foreach($data as $value)
    – reformed
    Jun 6, 2013 at 17:15
  • Probably just a typo, as code clearly IS working (just not as expected); semicolons should've broke that, too. Edited both out.
    – raina77ow
    Jun 6, 2013 at 17:15

2 Answers 2

6

You probably wanted just this:

$new_data = array();
foreach ($data as $value) {
    $new_data[] = array (
        "date"=> $value->DATE;
        "time" => $value->TIME;
    );
}

First, without [], you just assign a new value to $new_data variable at each iteration. With it, you append a new element to an array at each step; that's obviously what should be done in this case.

Second, you don't have to use another variable in this case: indexing starts from 0, and with each new [] increases as it should be.

Finally (kudos to @reformed for mentioning that in comments), you don't have to write long form of foreach if you actually don't use array's keys. foreach $arr as $val is sufficient.


In my opinion, that may be rewritten more concise with array_map:

$new_data = array_map(function($val) { return array(
  'date' => $val->DATE,
  'time' => $val->TIME
); }, $data);

This function usually is quite a fit when one wants to create a normal array based on another array, and each element of this new array is somehow based on the corresponding element of the original collection.

I've used an anonymous function here as an argument of array_map, that's available since PHP 5.3.

1
  • Ah, yes. Arrays get the best of me sometimes. Jun 6, 2013 at 17:08
1
$i = 0;
foreach ($data as $key =>$value) {
    $new_data[$i++] = array (
            "date"=> $value->DATE;
            "time" => $value->TIME;
        )
    );
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.