0

How can I get(echo/print) all of the username values from a variable which is a stdClass object? below is the structure of this variable I got from var_dump. thanks!

object(stdClass)[4]
public 'rows' => 
array (size=7)
      0 => 
    array (size=3)
      'id' => string '1' (length=1)
      'username' => string 'Justin' (length=6)
      'password' => string '123' (length=3)
  1 => 
    array (size=3)
      'id' => string '2' (length=1)
      'username' => string 'Papino' (length=6)
      'password' => string '456' (length=3)
  2 => 
    array (size=3)
      'id' => string '3' (length=1)
      'username' => string 'Sophia' (length=6)
      'password' => string '789' (length=3)
  3 => 
    array (size=3)
      'id' => string '4' (length=1)
      'username' => string 'something' (length=3)
      'password' => string '1234' (length=4)

2 Answers 2

1

Use array_map in order to get an array of usernames like so:

array_map(function($row) {
    echo $row['username'];
}, $object->rows);

// $usernames = ['Papino', 'Sophia', ..]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but this doesn't work, PHP reports Parse error for unexpected ')', however this is not I am cared about. I think there must a way to echo the values that held in my $result variable which is a stdClass object as I mentioned above, by using the foreach or while loop, I just don't know how to do it.
I put an extra ) my answer is updated, if you only want to print the usernames change the return for echo
1

Try the following snippet change $object with your StdClass Object Name.

$userNames = [];
foreach($object->rows as $item){
  $userNames []  = $item['username'];
}

var_dump($userNames);

update after comments There is no way to do it with var_dump solely, you should some jobs to get it.

in my projects I always use Krumo instead of print_r , var_dump

$userNames = [];
foreach($object->rows as $item){
  echo  $item['username'].PHP_EOL;
}

6 Comments

Thanks for your quick answer, but I don't want to var_dump the $userNames, I already have those values saved in my variable like $result for example. If I var_dump my $result I got above data structure which is a stdClass includes a 2 dimensional array, all I want is echo/print out all of the inner-most values like Justin,Papino,Sophia...NOT var_dump again!!!! hope I explained this well.
he answer is missing a ' in $item['username]; but if you just need to echo them out, the replace that line with echo $item['username'].PHP_EOL; or something similar.
@NigelRen thanks for your comment, I updated missing '.
This answer will basically work, just change this line $userNames [] = $item['username']; to echo $item['username']."<br>\n"; and then delete the var_dump line.
Thanks, I simply used foreach($result->rows as $item){ echo $item['username']; } which did the perfect job.
|

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.