0

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?

1
  • print_r just prints a string detailing the array, the true option just returns the string instead of printing it out directly. It doesn't put the actual data array into the variable Commented Sep 30, 2013 at 14:50

2 Answers 2

1
$hey = print_r($res, TRUE);

This will return a string that gives the info of the array $res. If you echo it, you should expect to see the same as what print_r($res); displays, as you've shown. $res is already an array of your query data, so loop over that.

foreach($res as $row) {  //row loop
  foreach($row as $col) {  //column loop
    //do something
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Tried replacing // do something with echo $res[$row][$col]; and got "Warning: Illegal offset type" on that echo line 4 times.
The variable would be just $col. If you did a nested for loop, the syntax would be like you tried.
0
$array = array( array( ID => 1, 
                  author => 0,
                  content => "This Is a Test",
          publication_date => 1380380992
                )
         );



echo $array['0']['author'];

This works for me...

just echo echo $array['0']['author']; and replace author by the field you need.

5 Comments

Hey, thanks for the quick reply! I tried it but its still the same, except I have a notice too now: Warning: Illegal string offset 'content' in...A. The error is on that line.
Can you maybe try again?
echo $hey['0']['0']; prints just "A" without the warning, but I get "Notice: Uninitialized string offset: 1" if I do echo $hey['0']['1']; or echo $hey['0']['2'];
the problem is print_r does not return array data, using $hey['0'] or any other array syntax with $hey you are just accessing the individual characters that make up the returned string.
You need to use the array, not $hey ;) Look at my answer.

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.