1

Must be something simple, but I have been struggling with this for a while. I have PHP array

//Have array $arr which contains values from database
$createdAtArray=array();
foreach($arr as  $value){
    $idToMatch = $value['id'];
    $dateToCompare = $value['createdAt']->format('F j Y, g:i');

    array_push($createdAtArray,$dateToCompare,$idToMatch);
}

Now to check how my array looks i use

    echo '<pre>'; print_r($createdAtArray); echo '</pre>';
    var_dump($createdAtArray);

which returns : enter image description here

So I try to access my arrays value like this : $createdAtArray[0] and except to get January 1 2019, 12:00 but instead I get 'J' (First Letter in string)

After trying multiple options I found out that by accessing just $createdAtArray I get January 1, 2019, 12:00.

However I cant get values that I want. What am I doing wrong?

2
  • 2
    array_push($createdAtArray,array($dateToCompare,$idToMatch)); Commented Sep 6, 2016 at 10:41
  • array_push($createdAtArray,array($dateToCompare,$idToMatch))‌​; . check and then print your array and iterate accordingly. Commented Sep 6, 2016 at 10:43

1 Answer 1

3

array push

array_push($createdAtArray,array('date'=>$dateToCompare,'id'=>$idToMatch));

 foreach($createdATArray as $key=>$row)
  {

     echo $row['date'];
     echo $row['id'];
  }
Sign up to request clarification or add additional context in comments.

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.