Posted By

damarev on 11/22/06


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


output all properties of an object/array

 / Published in: PHP

  1. foreach ($object as $key => $value) {
  2. echo "Key: $key; Value: $value<br>\n";
  3. }
  7. Ej. (prevent sql injection):
  9. foreach ($_POST as $key => $value) {
  10. $_POST[$key] = mysql_escape_string($value);
  11. }

Report this snippet  


RSS Icon Subscribe to comments
Posted By: tylerhall on November 22, 2006

You could also do print_r($object);

Posted By: damarev on December 5, 2006

Thanks !


Posted By: Jesdisciple on November 4, 2007

Tyler, that would print them to the page, but you'd have to somehow retrieve the output (probably by AJAX) and use a monster regex to manipulate them.

If you want the keys as an array, you can use getobjectvars($object) and array_keys($array). (And then you can get the values using $object->$key and $array[$key].)

Posted By: Jesdisciple on November 4, 2007

Sorry, getobjectvars is supposed to have underscores.

Posted By: Jesdisciple on November 5, 2007

Self-correction: print_r($var, true) would return the output to the program, rather than outputting it, but the monster regex would still be required to manipulate the data.

And it's get_object_vars. (The underscores should show now.)

You need to login to post a comment.