php - Find In Object of Arrays A Value -


i have following code:

function searchobject($obj, $field, $value) {     foreach ($obj $item){ # gets products         foreach ($item $child) { #gets products, products array of products             foreach ($child $grandchild){ #gets products array                 if (isset($grandchild->$field) && $grandchild->$field == $value) {                     return $grandchild;                 }             }         }     }     return "not found"; } 

this how it's called:

$freetrialobj = searchobject($arr, "pid", 15); 

but doesn't work, reporting 'invalid argument'. here print_r of object of arrays:

array: stdclass object ( [result] => success [clientid] => 706 [serviceid] =>  [pid] =>  [domain] =>  [totalresults] => 1 [startnumber] => 0 [numreturned] => 1 [products] => stdclass object     (         [product] => array             (                 [0] => stdclass object                     (                         [id] => 1014                         [clientid] => 706                         [orderid] => 902                         [pid] => 15                         [regdate] => 2013-09-03                         [name] =>                          [groupname] =>                          [domain] =>                          [dedicatedip] =>                          [serverid] => 0                         [servername] =>                          [serverip] =>                          [serverhostname] =>                          [firstpaymentamount] => 0.00                         [recurringamount] => 0.00                         [paymentmethod] => authorize                         [paymentmethodname] => authorize.net                         [billingcycle] => free account                         [nextduedate] => 0000-00-00                         [status] => pending                         [username] =>                          [password] =>                          [subscriptionid] =>                          [promoid] => 0                         [overideautosuspend] =>                          [overidesuspenduntil] => 0000-00-00                         [ns1] =>                          [ns2] =>                          [assignedips] =>                          [notes] =>                          [diskusage] => 0                         [disklimit] => 0                         [bwusage] => 0                         [bwlimit] => 0                         [lastupdate] => 0000-00-00 00:00:00                         [customfields] => stdclass object 

what best way check nested object value?

there php function this, rather brute-forcing it, function attempts, lets apply recursive thinking:

function searchobject($obj, $field, $value) {   if (is_array($obj)) { //is object array?     if( array_key_exists($field, $obj) ) { //check see if object exists       return $obj->$field;  //if so, return     } else {       foreach ($obj $key => $val) {//search sub-objects         $result = searchobject($val, $field, $value);//make recursive call         if ($result !== false) {//if not false, found it!           return $result; //return result         }       }       return false;//otherwise didn't find it, return false     }   } else {     return false;//this isn't array, can't possibly have field.   } } 

this should able (do depth-first) search object of depth field, , return or false if it's not there. note return first such field finds; if there multiple matching fields, miss beyond first.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -