Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Boris Kamp
Boris Kamp
16,660 Points

Echo variables from an array

Hi guys, Im trying to display content from a MySQL db on the frontend of my wordpress site. I created this to fetch all the values:

$users_info = $results->fetchAll(PDO::FETCH_ASSOC);

It gives me a huge array, one for each row. here's a bit of what's inside:

array(49) {
  [0]=>
  array(15) {
    ["id"]=>
    string(1) "1"
    ["bedrijf"]=>
    string(0) ""
    ["titel"]=>
    string(0) ""
    ["voorletters"]=>
    string(4) "B.K."
    ["tussenvoegsel"]=>
    string(0) ""
    ["achtervoegsel"]=>
    string(0) ""
    ["meisjesnaam"]=>
    string(0) ""
    ["thuis_adres"]=>
    string(22) "example"
    ["thuis_postcode"]=>
    string(7) "example"
    ["thuis_plaats"]=>
    string(9) "example"
    ["thuis_land"]=>
    string(0) ""
    ["mobiele_telefoon"]=>
    string(11) "example"
    ["email_thuis"]=>
    string(20) "example"
    ["geboortedatum"]=>
    string(0) ""
    ["bsn_nummer"]=>
    string(0) ""
  }
  [1]=>
  array(15) {
    ["id"]=>
    string(1) "2"
    ["bedrijf"]=>
    string(0) ""
    ["titel"]=>
    string(0) ""
    ["voorletters"]=>
    string(4) "example"
    ["tussenvoegsel"]=>
    string(0) ""
    ["achtervoegsel"]=>
    string(0) ""
    ["meisjesnaam"]=>
    string(0) ""
    ["thuis_adres"]=>
    string(15) "example"
    ["thuis_postcode"]=>
    string(7) "example"
    ["thuis_plaats"]=>
    string(5) "example"
    ["thuis_land"]=>
    string(0) ""
    ["mobiele_telefoon"]=>
    string(0) ""
    ["email_thuis"]=>
    string(12) "example"
    ["geboortedatum"]=>
    string(0) ""
    ["bsn_nummer"]=>
    string(0) ""
  }

so there are 49 arrays inside the main array. How can I, for example display the value id of the first ([0]) array in the group? or the

Thanks!

1 Answer

If you wanted to display it this way, it would be ultra hard because there's no way for you to select the number of each array dynamically. ultimately it would have to be this:

//where ever you wanted this value to show up in your code.
$user_info[number][key];

So for example if you wanted to index into the id of the first array and display it somewhere to the user, in the location of your php file, you would have to type in,

$user_info[0]["id"]

Again, it would be impossible to make it dynamic unless you were looping through each array or had a way of grabbing a specific array index. I would suggest changing your query string so it grabs smaller chucks from the database and working with those chucks individually. For instance, all information within the same row on your table.

Hope that helps.

Boris Kamp
Boris Kamp
16,660 Points

Thank you for sharing your thoughts on this topic.

I saw you gave me some tips in my other topic as well stating that I should only fetch one row of data instead of all of them. Let's get that fixed first, displaying data will be way easier that way.

Thanks!