在数组对象中查找值

时间:2013-09-03 20:18:34

标签: php arrays object

我有以下代码:

function searchObject($obj, $field, $value) {
    foreach ($obj as $item){ # gets to products
        foreach ($item as $child) { #gets to products, products is an array of products
            foreach ($child as $grandchild){ #gets to products array
                if (isset($grandchild->$field) && $grandchild->$field == $value) {
                    return $grandchild;
                }
            }
        }
    }
    return "Not Found";
}

这就是它的名称:

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

但这不起作用,报告“无效论证”。这是数组对象的print_r:

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

检查像这样的嵌套对象的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

可能有一个php函数可以执行此操作,但是,正如您的函数尝试一样,而不是强制执行它,让我们应用一些递归思维:

function searchObject($obj, $field, $value) {
  if (is_array($obj)) { //Is this object even array?
    if( array_key_exists($field, $obj) ) { //Check to see if the object exists
      return $obj->$field;  //If so, return it
    } else {
      foreach ($obj as $key => $val) {//Search all sub-objects
        $result = searchObject($val, $field, $value);//Make a recursive call
        if ($result !== false) {//If not false, we found it!
          return $result; //return the result
        }
      }
      return false;//Otherwise we didn't find it, return false
    }
  } else {
    return false;//This isn't an array, so it can't possibly have the field.
  }
}

这应该能够(深度优先)搜索您的字段的任何深度的对象,并返回它或false如果它不在那里。请注意,它将返回它找到的第一个这样的字段;如果有多个匹配字段,您将错过第一个以外的任何内容。