0

I var_dumped an array that shows these elements (there are 266, but I am showing you two).

array(266) {
  [1]=>
  array(5) {
    ["date_created"]=>
    string(10) "1381816800"
    ["project_number"]=>
    string(5) "02783"
    ["name"]=>
    string(9) "sdfsdfdfd"
    ["description"]=>
    string(13) "dsfsfdsfdssdf"
    ["manager"]=>
    string(11) "Kevin Allen"
  }
  [21]=>
  array(5) {
    ["date_created"]=>
    string(10) "1381816800"
    ["project_number"]=>
    string(5) "02783"
    ["name"]=>
    string(9) "sdfsdfdfd"
    ["description"]=>
    string(13) "dsfsfdsfdssdf"
    ["manager"]=>
    string(16) "Carter Hilkewich"
  }
}

I needed to convert the date in this array too: m-d-Y so I wrote:

private function dateConverter($array){
    foreach($array as $key=>$value){
        if(isset($value['date_created'])){
            $value['date_created'] = date("m-d-Y", strtotime($value['date_created']));
        }
    }

    return $array;
}

Which you pass the array into, walks through, converts the date and returns the array. simple. 'Cept what it's returning is the exact same array. So I am wondering do I need to save the "new" array into a separate array? I have a similar function that does this with objects and I never had to save a"new" object.

thoughts?

1
  • The strtotime() is unnecessary since the time is already a timestamp. Commented Oct 16, 2013 at 17:51

1 Answer 1

1

Two things:

  • The foreach does not pass the value by reference, so modifying it directly will not store it back to the original array
  • The date_created field is already a timestamp, no need for strtotime

Try this:

private function dateConverter($array){
    foreach($array as $key => &$value){
        if(isset($value['date_created'])){
            $value['date_created'] = date("m-d-Y", $value['date_created']);
        }
    }

    return $array;
}
Sign up to request clarification or add additional context in comments.

4 Comments

The value in date_created appears to be a unix timestamp, the same kind that is returned by strtotime.
The biggest problem with your code is the fact that you're modifying a copy of the array value and not the value in the original array. Notice the ampersand before $value in the foreach of the function I posted; that causes it to be passed into the array by reference and stored back to the original array when you change the value.
I see that and I am still lost on "pass by reference" I have read the manual a couple of times. So this will pass the value I intend instead of just, as you say, a copy? so If I passed apples, I pass the original apples and NOT the copy of apples I would pass with out &?
Passing by reference is simply telling where to find the value rather than passing a copy of the value. So, if you needed to pass an apple to your friend and passed it as $apple, you would copy your apple and give it to him. He could eat it, and you'd still have your apple. If you passed it by reference as &$apple you would tell him, "There's an apple on the counter in the kitchen next to the sink." If he takes a bite of the apple, your apple has a bite taken out of it because you just told him where the apple was, rather than giving him his own copy. Hard to explain pointers in 500 chars.

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.