0

次のコードがあります。

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

このようなネストされたオブジェクトの値をチェックする最良の方法は何ですか?

4

1 に答える 1