Kristaps Gaidulis Kristaps Gaidulis - 2 months ago 10
PHP Question

PHP array returns displays wrong value

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?

Answer

array push

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

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

     echo $row['date'];
     echo $row['id'];
  }
Comments