SecureLive
SecureLive

Reputation: 80

Multidimensional, get info from parent searching the child array

I have a multidimensional array that I am struggling with. The array is this:

$array => Array (1)
(
|    ['WHMCSAPI'] => Array (6)
|    (
|    |    ['ACTION'] = String(9) "getorders"
|    |    ['RESULT'] = String(7) "success"
|    |    ['TOTALRESULTS'] = String(1) "4"
|    |    ['STARTNUMBER'] = String(1) "0"
|    |    ['NUMRETURNED'] = String(1) "4"
|    |    ['ORDERS'] => Array (4)
|    |    (
|    |    |    ['ORDER'] => Array (27)
|    |    |    (
|    |    |    |    ['ID'] = String(3) "190"
|    |    |    |    ['ORDERNUM'] = String(10) "8782265746"
|    |    |    |    ['USERID'] = String(2) "66"
|    |    |    |    ['CONTACTID'] = String(1) "0"
|    |    |    |    ['DATE'] = String(19) "2013-12-31 16:00:42"
|    |    |    |    ['NAMESERVERS'] = NULL(0) NULL
|    |    |    |    ['TRANSFERSECRET'] = NULL(0) NULL
|    |    |    |    ['RENEWALS'] = NULL(0) NULL
|    |    |    |    ['PROMOCODE'] = NULL(0) NULL
|    |    |    |    ['PROMOTYPE'] = NULL(0) NULL
|    |    |    |    ['PROMOVALUE'] = NULL(0) NULL
|    |    |    |    ['ORDERDATA'] = String(6) "a:0:{}"
|    |    |    |    ['AMOUNT'] = String(4) "4.00"
|    |    |    |    ['PAYMENTMETHOD'] = String(9) "authorize"
|    |    |    |    ['INVOICEID'] = String(1) "0"
|    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    ['IPADDRESS'] = String(12) "64.50.180.53"
|    |    |    |    ['FRAUDMODULE'] = NULL(0) NULL
|    |    |    |    ['FRAUDOUTPUT'] = NULL(0) NULL
|    |    |    |    ['NOTES'] = NULL(0) NULL
|    |    |    |    ['PAYMENTMETHODNAME'] = String(11) "Credit Card"
|    |    |    |    ['PAYMENTSTATUS'] = NULL(0) NULL
|    |    |    |    ['NAME'] = String(11) "Chad Riddle"
|    |    |    |    ['CURRENCYPREFIX'] = String(1) "$"
|    |    |    |    ['CURRENCYSUFFIX'] = String(4) " USD"
|    |    |    |    ['FRAUDDATA'] = NULL(0) NULL
|    |    |    |    ['LINEITEMS'] => Array (1)
|    |    |    |    (
|    |    |    |    |    ['LINEITEM'] => Array (8)
|    |    |    |    |    (
|    |    |    |    |    |    ['TYPE'] = String(7) "product"
|    |    |    |    |    |    ['RELID'] = String(3) "488"
|    |    |    |    |    |    ['PRODUCTTYPE'] = String(21) "Other Product/Service"
|    |    |    |    |    |    ['PRODUCT'] = String(46) "SecureLive Addons - SecureLive Addon Domain 10"
|    |    |    |    |    |    ['DOMAIN'] = String(17) "2kplusgateway.org"
|    |    |    |    |    |    ['BILLINGCYCLE'] = String(7) "Monthly"
|    |    |    |    |    |    ['AMOUNT'] = String(9) "$4.00 USD"
|    |    |    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    |    ['ORDER1'] => Array (27)
|    |    |    (
|    |    |    |    ['ID'] = String(3) "189"
|    |    |    |    ['ORDERNUM'] = String(10) "6062113947"
|    |    |    |    ['USERID'] = String(2) "66"
|    |    |    |    ['CONTACTID'] = String(1) "0"
|    |    |    |    ['DATE'] = String(19) "2013-12-31 16:00:38"
|    |    |    |    ['NAMESERVERS'] = NULL(0) NULL
|    |    |    |    ['TRANSFERSECRET'] = NULL(0) NULL
|    |    |    |    ['RENEWALS'] = NULL(0) NULL
|    |    |    |    ['PROMOCODE'] = NULL(0) NULL
|    |    |    |    ['PROMOTYPE'] = NULL(0) NULL
|    |    |    |    ['PROMOVALUE'] = NULL(0) NULL
|    |    |    |    ['ORDERDATA'] = String(6) "a:0:{}"
|    |    |    |    ['AMOUNT'] = String(4) "4.00"
|    |    |    |    ['PAYMENTMETHOD'] = String(9) "authorize"
|    |    |    |    ['INVOICEID'] = String(1) "0"
|    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    ['IPADDRESS'] = String(12) "64.50.180.53"
|    |    |    |    ['FRAUDMODULE'] = NULL(0) NULL
|    |    |    |    ['FRAUDOUTPUT'] = NULL(0) NULL
|    |    |    |    ['NOTES'] = NULL(0) NULL
|    |    |    |    ['PAYMENTMETHODNAME'] = String(11) "Credit Card"
|    |    |    |    ['PAYMENTSTATUS'] = NULL(0) NULL
|    |    |    |    ['NAME'] = String(11) "Chad Riddle"
|    |    |    |    ['CURRENCYPREFIX'] = String(1) "$"
|    |    |    |    ['CURRENCYSUFFIX'] = String(4) " USD"
|    |    |    |    ['FRAUDDATA'] = NULL(0) NULL
|    |    |    |    ['LINEITEMS2'] => Array (1)
|    |    |    |    (
|    |    |    |    |    ['LINEITEM3'] => Array (8)
|    |    |    |    |    (
|    |    |    |    |    |    ['TYPE'] = String(7) "product"
|    |    |    |    |    |    ['RELID'] = String(3) "487"
|    |    |    |    |    |    ['PRODUCTTYPE'] = String(21) "Other Product/Service"
|    |    |    |    |    |    ['PRODUCT'] = String(46) "SecureLive Addons - SecureLive Addon Domain 10"
|    |    |    |    |    |    ['DOMAIN'] = String(11) "jbrowns.com"
|    |    |    |    |    |    ['BILLINGCYCLE'] = String(7) "Monthly"
|    |    |    |    |    |    ['AMOUNT'] = String(9) "$4.00 USD"
|    |    |    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    |    ['ORDER4'] => Array (27)
|    |    |    (
|    |    |    |    ['ID'] = String(3) "188"
|    |    |    |    ['ORDERNUM'] = String(10) "2178188116"
|    |    |    |    ['USERID'] = String(2) "66"
|    |    |    |    ['CONTACTID'] = String(1) "0"
|    |    |    |    ['DATE'] = String(19) "2013-12-31 16:00:34"
|    |    |    |    ['NAMESERVERS'] = NULL(0) NULL
|    |    |    |    ['TRANSFERSECRET'] = NULL(0) NULL
|    |    |    |    ['RENEWALS'] = NULL(0) NULL
|    |    |    |    ['PROMOCODE'] = NULL(0) NULL
|    |    |    |    ['PROMOTYPE'] = NULL(0) NULL
|    |    |    |    ['PROMOVALUE'] = NULL(0) NULL
|    |    |    |    ['ORDERDATA'] = String(6) "a:0:{}"
|    |    |    |    ['AMOUNT'] = String(4) "4.00"
|    |    |    |    ['PAYMENTMETHOD'] = String(9) "authorize"
|    |    |    |    ['INVOICEID'] = String(1) "0"
|    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    ['IPADDRESS'] = String(12) "64.50.180.53"
|    |    |    |    ['FRAUDMODULE'] = NULL(0) NULL
|    |    |    |    ['FRAUDOUTPUT'] = NULL(0) NULL
|    |    |    |    ['NOTES'] = NULL(0) NULL
|    |    |    |    ['PAYMENTMETHODNAME'] = String(11) "Credit Card"
|    |    |    |    ['PAYMENTSTATUS'] = NULL(0) NULL
|    |    |    |    ['NAME'] = String(11) "Chad Riddle"
|    |    |    |    ['CURRENCYPREFIX'] = String(1) "$"
|    |    |    |    ['CURRENCYSUFFIX'] = String(4) " USD"
|    |    |    |    ['FRAUDDATA'] = NULL(0) NULL
|    |    |    |    ['LINEITEMS5'] => Array (1)
|    |    |    |    (
|    |    |    |    |    ['LINEITEM6'] => Array (8)
|    |    |    |    |    (
|    |    |    |    |    |    ['TYPE'] = String(7) "product"
|    |    |    |    |    |    ['RELID'] = String(3) "486"
|    |    |    |    |    |    ['PRODUCTTYPE'] = String(21) "Other Product/Service"
|    |    |    |    |    |    ['PRODUCT'] = String(46) "SecureLive Addons - SecureLive Addon Domain 10"
|    |    |    |    |    |    ['DOMAIN'] = String(14) "blestatest.com"
|    |    |    |    |    |    ['BILLINGCYCLE'] = String(7) "Monthly"
|    |    |    |    |    |    ['AMOUNT'] = String(9) "$4.00 USD"
|    |    |    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    |    )
|    |    |    |    )
|    |    |    )
|    |    |    ['ORDER7'] => Array (26)
|    |    |    (
|    |    |    |    ['ID'] = String(3) "187"
|    |    |    |    ['ORDERNUM'] = String(10) "9493314051"
|    |    |    |    ['USERID'] = String(2) "66"
|    |    |    |    ['CONTACTID'] = String(1) "0"
|    |    |    |    ['DATE'] = String(19) "2013-12-31 15:58:48"
|    |    |    |    ['NAMESERVERS'] = NULL(0) NULL
|    |    |    |    ['TRANSFERSECRET'] = NULL(0) NULL
|    |    |    |    ['RENEWALS'] = NULL(0) NULL
|    |    |    |    ['PROMOCODE'] = NULL(0) NULL
|    |    |    |    ['PROMOTYPE'] = NULL(0) NULL
|    |    |    |    ['PROMOVALUE'] = NULL(0) NULL
|    |    |    |    ['ORDERDATA'] = String(6) "a:0:{}"
|    |    |    |    ['AMOUNT'] = String(4) "4.00"
|    |    |    |    ['PAYMENTMETHOD'] = String(9) "authorize"
|    |    |    |    ['INVOICEID'] = String(1) "0"
|    |    |    |    ['STATUS'] = String(6) "Active"
|    |    |    |    ['IPADDRESS'] = String(12) "64.50.180.53"
|    |    |    |    ['FRAUDMODULE'] = NULL(0) NULL
|    |    |    |    ['FRAUDOUTPUT'] = NULL(0) NULL
|    |    |    |    ['NOTES'] = NULL(0) NULL
|    |    |    |    ['PAYMENTMETHODNAME'] = String(11) "Credit Card"
|    |    |    |    ['PAYMENTSTATUS'] = NULL(0) NULL
|    |    |    |    ['NAME'] = String(11) "Chad Riddle"
|    |    |    |    ['CURRENCYPREFIX'] = String(1) "$"
|    |    |    |    ['CURRENCYSUFFIX'] = String(4) " USD"
|    |    |    |    ['FRAUDDATA'] = NULL(0) NULL
|    |    |    )
|    |    )
|    )
)

Basically what I am trying to get is the order id, which is basically $array['WHMCSAPI']['ORDERS']['ORDER']['ID'] by searching for the domain name, which is $array['WHMCSAPI']['ORDERS']['ORDER']['LINEITEMS']['LINEITEM']['DOMAIN']

Notice that the 'ORDER' KEY changes, so does the 'LINEITEMS' and 'LINEITEM'.

So if I was to search for jbrowns.com, it would pull back order number 189, or if I was searching for blestatest it would pull back id 188.

I have tried array_walk, array_search and many pre-built functions that did not give me the information I needed. Has anyone come across this situation.

This is basically for WHMCS, this is the getorders API function. If one of my customers needs to cancel their service from the website, the API function 'deleteorder' requires the orderid, and the only way I can get the order id is by searching for the domain they would like to cancel.

Any assistance would be very helpful.

Thanks, Jeff

Upvotes: 0

Views: 121

Answers (2)

Adlin Ling
Adlin Ling

Reputation: 382

This should work for you. Just plug in your array and the domain you are looking for and assign to a variable.

<?php

function getOrderID($searchfor, $input_array){
    foreach($input_array['WHMCSAPI']['ORDERS'] as $orderskey => $ordersvalue){

        foreach($ordersvalue as $orderkey => $ordervalue){

            if(preg_match("/LINEITEMS[1-9]*/i", $orderkey, $match)){

                foreach($ordervalue as $key => $value){        

                   if($value['DOMAIN'] == $searchfor){
                       $orderid = $input_array['WHMCSAPI']['ORDERS'][$orderskey]['ID']."<br/>";
                   }

                }

             }
         }
    }

    if(isset($orderid)){
         return $orderid;
    }else{
         return "Domain not found";
    }

}


$id = getOrderID("blestatest.com", $array);

echo $id;

?>

Upvotes: 1

Ali
Ali

Reputation: 5121

Try this..

<?php
$domain = "www.your-domain.com";
$order_id = 0;
$found = FALSE;

if (!$found) {
    foreach ($array as $arr) {
        foreach ($arr['ORDERS'] as $ords) {
            foreach($ords['ORDER'] as $ord) {
                $order_id = $ord['ID'];
                foreach($ord['LINEITEMS'] as $litems) {
                    foreach($litems['LINEITEM'] as $litem) {
                        if ($litem['DOMAIN'] == $domain) {
                            $found = TRUE;
                            break 5;
                        }
                    }
                }
            }
        }
    }
}
?>

Upvotes: 2

Related Questions