I have the following code:
$res = $db->getArticles($db);
$hey = print_r($res, TRUE);
echo $hey['0'];
Here is the code for $db->getArticles:
public function getArticles() {
$array = array();
try {
$sth = $this->db->prepare("SELECT * FROM posts ORDER BY id");
$sth->execute();
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {
$array[] = $row;
}
return $array;
} catch (Exception $e) {
}
}
The logic is that the function queries the database and puts everything into an array. The number of rows in the table will always be pretty low, so performance isn't an issue.
The following is the output of the first code snippet above:
echo $hey['0']; // prints "A" (without the quotes).
echo $hey['1']; // prints "r"
echo $hey['2']; // prints "r"
echo $hey['3']; // prints "a"
echo $hey['4']; // prints "y"
As you can see, it spells out the word "Array."
echo $hey prints the following:
Array ( [0] => Array ( [id] => 1 [author] => 0 [content] => This Is a Test [publication_date] => 1380380992 ) )
My end goal is to store each individual value into a variable and do something with it. This would all happen inside a for loop which runs on the array so I can get information from each row.
What am I doing wrong?
print_rjust prints a string detailing the array, thetrueoption just returns the string instead of printing it out directly. It doesn't put the actual data array into the variable