stdClass Object output to a list – PHP

  php

Q(Question):

Hello,
i would like to know how i can get better output from a foreach.

this code:

$x = $wpdb->get_results("SELECT * FROM " . WP_PLAATJES);
foreach ($x as $y) {
print_r($y);
var_dump( $y );

outputs:

stdClass Object ( [plaatjes_ID] => 1 [plaatjes_name] => Screenie [plaatjes_images] => \screenshot.png [plaatjes_status] => 1 )
object(stdClass)[3]
public ‘plaatjes_ID’ => string ‘1’ (length=1)
public ‘plaatjes_name’ => string ‘Screenie’ (length=8)
public ‘plaatjes_images’ => string ‘\screenshot.png’ (length=15)
public ‘plaatjes_status’ => string ‘1’ (length=1)

what i would like to see is a normal list of ‘plaatjes_name’ and it’s image, like:
name: screenie
image: \screenshot.png

Thanks in advance.
~muckymuck

A(Answer):

nevermind, found it.

echo $y->{‘plaatjes_name’};

A(Answer):

echo $y->plaatjes_name works just as well, and likely faster.

A(Answer):

curly braces are useful, if you don’t know the name before runtime.

public function func($name)
{
// some code
return $this->{'do' . $name}();
}

A(Answer):

But if you don’t know that name before run time, how will you know it’s validity?

public function func($name) {
// some code
if (method_exists($this, "do{$name}")) {
return $this->{"do{$name}"}();
}
return "FAIL!";
}

I see your code and raise you one control statement. :3

A(Answer):

I see your code and raise you one control statement. :3

that’s what the

// some code

was for

A(Answer):

Got me there.

LEAVE A COMMENT