3

I need to iterate over objects in PHP and to apply a certain function on each and every single value in this object. The objects are absolutely arbitrary. They can include vars, another objects, arrays, arrays of objects and so on...

Is there a generic method to do so? If yes, how?

Usage example: RESTful API which receives requests in JSON format. json_decode() is executed on request body and creates an arbitrary object. Now, it is good, for example, to execute mysqli_real_escape_string() on every value in this object before further validations.

OBJECT EXAMPLE:

{
  "_id": "551a78c500eed4fa853870fc",
  "index": 0,
  "guid": "f35a0b22-05b3-4f07-a3b5-1a319a663200",
  "isActive": false,
  "balance": "$3,312.76",
  "age": 33,
  "name": "Wolf Oconnor",
  "gender": "male",
  "company": "CHORIZON",
  "email": "[email protected]",
  "phone": "+1 (958) 479-2837",
  "address": "696 Moore Street, Coaldale, Kansas, 9597",
  "registered": "2015-01-20T03:39:28 -02:00",
  "latitude": 15.764928,
  "longitude": -125.084813,
  "tags": [
    "id",
    "nulla",
    "tempor",
    "do",
    "nulla",
    "laboris",
    "consequat"
  ],
  "friends": [
    {
      "id": 0,
      "name": "Casey Dominguez"
    },
    {
      "id": 1,
      "name": "Morton Rich"
    },
    {
      "id": 2,
      "name": "Marla Parsons"
    }
  ],
  "greeting": "Hello, Wolf Oconnor! You have 3 unread messages."
}
3
  • 1
    Can you give us an example object? That sure would make it easier. Commented Mar 31, 2015 at 10:35
  • The objects are too long to fill in the comment box. :( Basically, they are very big and I need to apply some generic validations. Commented Mar 31, 2015 at 10:38
  • Use of mysqli_real_escape_string() would be unnecessary if you were using bind variables in your SQL code Commented Mar 31, 2015 at 10:51

2 Answers 2

3

If you just need to walk over the data and won't need to re-encode it, json_decode()'s second parameter, $assoc will cause it to return an associative array. From there, array_walk_recursive() should work well for what you're after.

$data = json_decode($source_object);
$success = array_walk_recursive($data, "my_validate");

function my_validate($value, $key){
    //Do validation.
}
Sign up to request clarification or add additional context in comments.

Comments

0
function RecursiveStuff($value, $callable) 
{
   if (is_array($value) || is_object($value)) 
   {
       foreach (&$prop in $value) {
          $prop = RecursiveStuff($prop);
       }
   } 
   else {
       $value = call_user_func($callable, $value);
   }
   return $value;
}

And use it like:

$decodedObject = RecursiveStuff($decodedObject, function($value) 
{
   return escapesomething($value); // do something with value here
});

You can just pass function name like:

$decodedObject = RecursiveStuff($decodedObject, 'mysqli_real_escape_string');

1 Comment

You got errors it should be: ...foreach ($value as &$prop) { $prop = RecursiveStuff($prop, $callable);...

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.