#php #arrays #json #count
#php #массивы #json #подсчитывать
Вопрос:
Я просто хочу подсчитать значения данных внутри foreach. Если значения одинаковы, это будет считаться, как если «Shared Hosting — Starter» имеет значение 6, затем посчитайте и выведите «Shared Hosting — Starter (6)»
Это мой код
$emailtemplates = '{"result":"success","totalresults":13,"startnumber":0,"numreturned":13,"orders":{"order":[{"id":121,"ordernum":1991897965,"userid":1,"contactid":0,"date":"2020-10-09 09:38:14","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"52500.00","paymentmethod":"banktransfer","invoiceid":1000000017,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"ovh test lagi","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":114,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}},{"id":120,"ordernum":6100408413,"userid":1,"contactid":0,"date":"2020-10-07 14:48:55","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000016,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test hosting","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":113,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"ewfwfhl.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":119,"ordernum":5975662952,"userid":1,"contactid":0,"date":"2020-10-06 07:48:48","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000014,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test email not to spam","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":112,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby.xyz","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":116,"ordernum":8975405194,"userid":1,"contactid":0,"date":"2020-10-05 13:46:37","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"52500.00","paymentmethod":"banktransfer","invoiceid":1000000010,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test ovh","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":[]},{"id":114,"ordernum":9059646332,"userid":1,"contactid":0,"date":"2020-10-03 08:08:31","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000008,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":107,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby.xyz","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Cancelled"}]}},{"id":113,"ordernum":5332646679,"userid":1,"contactid":0,"date":"2020-10-02 16:53:17","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000007,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test cpanel","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":106,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby1234.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":112,"ordernum":4793578376,"userid":1,"contactid":0,"date":"2020-10-02 09:50:31","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"ipay88","invoiceid":1000000006,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"iPay88 Payment Completed.","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":105,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasurabayanow.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":70,"ordernum":6736003294,"userid":3,"contactid":0,"date":"2020-09-29 16:26:53","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":0,"status":"Active","ipaddress":"91.192.166.30","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Pembayaran Online","paymentstatus":null,"name":"Modulesgarden Support","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":63,"producttype":"Other Product/Service","product":"VPS Linux - VPS Value","domain":"mgtest2.com","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}},{"id":56,"ordernum":1999753213,"userid":1,"contactid":0,"date":"2020-09-24 11:45:46","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"30200.00","paymentmethod":"ipay88","invoiceid":72,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"test","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":49,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"iwansakti.xyz","billingcycle":"Monthly","amount":"Rp.20,000 IDR","status":"Active"},{"type":"domain","relid":20,"producttype":"Domain","product":"Register","domain":"iwansakti.xyz","billingcycle":1,"amount":"Rp.10,200 IDR","status":"Active","dnsmanagement":1,"emailforwarding":0,"idprotection":0}]}},{"id":31,"ordernum":3357231065,"userid":3,"contactid":0,"date":"2020-09-18 13:57:53","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":34,"status":"Active","ipaddress":"91.192.166.30","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Pembayaran Online","paymentstatus":"Unpaid","name":"Modulesgarden Support","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":31,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"vps-d1dcb601.vps.ovh.ca","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Suspended"}]}},{"id":30,"ordernum":2871924540,"userid":1,"contactid":0,"date":"2020-09-17 14:25:54","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":33,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"test ovh 3","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":30,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Terminated"}]}},{"id":29,"ordernum":6918336188,"userid":1,"contactid":0,"date":"2020-09-17 04:22:36","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":32,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"Test beli iwan 2","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":29,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Cancelled"}]}},{"id":28,"ordernum":1295130072,"userid":1,"contactid":0,"date":"2020-09-17 03:24:46","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":31,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"order pertama","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":28,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}}]}}';
//kita decode dulu
$decodenya = json_decode($emailtemplates, true);
echo "<pre>";
//print_r($decodenya);
echo "</pre><br/>";
$count=1;
foreach ($decodenya["orders"]["order"] as $key=>$oder){
foreach($oder["lineitems"]["lineitem"] as $key1=>$oder1){
$hitung = $oder1["product"];
if ($hitung == $hitung){
$count=1;
$values[] = $oder1;
$count ;
echo $count;
//$count = 0;
} else if ($hitung == $hitung){
$count = 1;
$values[] = $oder1;
$count ;
echo $count;
} else if ($hitung == $hitung) {
$count = 1;
$values[] = $oder1;
$count ;
echo $count;
}
}
}
?>```
The output is 2222222222222
any help will be greatly appreciated
Комментарии:
1. Вы объявляете вас
$count
2 раза, поэтому объявляйте его только вне цикла и попробуйте.2. Я только что добавил
echo $count;
вне цикла, и вывод23456789101112131414
Ответ №1:
Я думаю, что это решение, которое вы ищете. Просто запустите код.
$emailtemplates = '{"result":"success","totalresults":13,"startnumber":0,"numreturned":13,"orders":{"order":[{"id":121,"ordernum":1991897965,"userid":1,"contactid":0,"date":"2020-10-09 09:38:14","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"52500.00","paymentmethod":"banktransfer","invoiceid":1000000017,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"ovh test lagi","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":114,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}},{"id":120,"ordernum":6100408413,"userid":1,"contactid":0,"date":"2020-10-07 14:48:55","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000016,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test hosting","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":113,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"ewfwfhl.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":119,"ordernum":5975662952,"userid":1,"contactid":0,"date":"2020-10-06 07:48:48","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000014,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test email not to spam","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":112,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby.xyz","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":116,"ordernum":8975405194,"userid":1,"contactid":0,"date":"2020-10-05 13:46:37","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"52500.00","paymentmethod":"banktransfer","invoiceid":1000000010,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test ovh","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":[]},{"id":114,"ordernum":9059646332,"userid":1,"contactid":0,"date":"2020-10-03 08:08:31","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000008,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":107,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby.xyz","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Cancelled"}]}},{"id":113,"ordernum":5332646679,"userid":1,"contactid":0,"date":"2020-10-02 16:53:17","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000007,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test cpanel","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":106,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby1234.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":112,"ordernum":4793578376,"userid":1,"contactid":0,"date":"2020-10-02 09:50:31","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"ipay88","invoiceid":1000000006,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"iPay88 Payment Completed.","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":105,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasurabayanow.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":70,"ordernum":6736003294,"userid":3,"contactid":0,"date":"2020-09-29 16:26:53","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":0,"status":"Active","ipaddress":"91.192.166.30","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Pembayaran Online","paymentstatus":null,"name":"Modulesgarden Support","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":63,"producttype":"Other Product/Service","product":"VPS Linux - VPS Value","domain":"mgtest2.com","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}},{"id":56,"ordernum":1999753213,"userid":1,"contactid":0,"date":"2020-09-24 11:45:46","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"30200.00","paymentmethod":"ipay88","invoiceid":72,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"test","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":49,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"iwansakti.xyz","billingcycle":"Monthly","amount":"Rp.20,000 IDR","status":"Active"},{"type":"domain","relid":20,"producttype":"Domain","product":"Register","domain":"iwansakti.xyz","billingcycle":1,"amount":"Rp.10,200 IDR","status":"Active","dnsmanagement":1,"emailforwarding":0,"idprotection":0}]}},{"id":31,"ordernum":3357231065,"userid":3,"contactid":0,"date":"2020-09-18 13:57:53","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":34,"status":"Active","ipaddress":"91.192.166.30","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Pembayaran Online","paymentstatus":"Unpaid","name":"Modulesgarden Support","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":31,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"vps-d1dcb601.vps.ovh.ca","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Suspended"}]}},{"id":30,"ordernum":2871924540,"userid":1,"contactid":0,"date":"2020-09-17 14:25:54","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":33,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"test ovh 3","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":30,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Terminated"}]}},{"id":29,"ordernum":6918336188,"userid":1,"contactid":0,"date":"2020-09-17 04:22:36","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":32,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"Test beli iwan 2","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":29,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Cancelled"}]}},{"id":28,"ordernum":1295130072,"userid":1,"contactid":0,"date":"2020-09-17 03:24:46","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":31,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"order pertama","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":28,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}}]}}';
//kita decode dulu
$decodenya = json_decode($emailtemplates, true);
$arrayAfterCount=array();//It will store product as a key and count as a value
foreach ($decodenya["orders"]["order"] as $key=>$oder){
if(isset($oder["lineitems"]["lineitem"])){
foreach($oder["lineitems"]["lineitem"] as $key1=>$oder1){
if(isset($arrayAfterCount[$oder1['product']])){
//If the product found again then the value will increase by one
$arrayAfterCount[$oder1['product']] ;
}else{
//The product is found for the first time
$arrayAfterCount[$oder1['product']]=1;
}
}
}
}
//Print the output
foreach ($arrayAfterCount as $key => $value) {
echo "{$key} ({$value}) <br/>";
}
Вывод:
VPS Linux - VPS Starter (5)
Shared Hosting - Starter (6)
VPS Linux - VPS Value (1)
Register (1)
Ответ №2:
Используя стандартные функции, вы можете легко достичь результата.
$emailtemplates = '{"result":"success","totalresults":13,"startnumber":0,"numreturned":13,"orders":{"order":[{"id":121,"ordernum":1991897965,"userid":1,"contactid":0,"date":"2020-10-09 09:38:14","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"52500.00","paymentmethod":"banktransfer","invoiceid":1000000017,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"ovh test lagi","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":114,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}},{"id":120,"ordernum":6100408413,"userid":1,"contactid":0,"date":"2020-10-07 14:48:55","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000016,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test hosting","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":113,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"ewfwfhl.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":119,"ordernum":5975662952,"userid":1,"contactid":0,"date":"2020-10-06 07:48:48","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000014,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test email not to spam","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":112,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby.xyz","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":116,"ordernum":8975405194,"userid":1,"contactid":0,"date":"2020-10-05 13:46:37","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"52500.00","paymentmethod":"banktransfer","invoiceid":1000000010,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test ovh","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":[]},{"id":114,"ordernum":9059646332,"userid":1,"contactid":0,"date":"2020-10-03 08:08:31","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000008,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":107,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby.xyz","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Cancelled"}]}},{"id":113,"ordernum":5332646679,"userid":1,"contactid":0,"date":"2020-10-02 16:53:17","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"banktransfer","invoiceid":1000000007,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"test cpanel","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":106,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasby1234.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":112,"ordernum":4793578376,"userid":1,"contactid":0,"date":"2020-10-02 09:50:31","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"10000.00","paymentmethod":"ipay88","invoiceid":1000000006,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"maxmind","fraudoutput":"{u0022codeu0022:u0022ACCOUNT_ID_REQUIREDu0022,u0022erroru0022:u0022An account ID and license key are required to use this service.u0022,u0022http_response_codeu0022:401}","notes":"iPay88 Payment Completed.","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":105,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"beritasurabayanow.my.id","billingcycle":"Monthly","amount":"Rp.10,000 IDR","status":"Active"}]}},{"id":70,"ordernum":6736003294,"userid":3,"contactid":0,"date":"2020-09-29 16:26:53","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":0,"status":"Active","ipaddress":"91.192.166.30","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Pembayaran Online","paymentstatus":null,"name":"Modulesgarden Support","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":63,"producttype":"Other Product/Service","product":"VPS Linux - VPS Value","domain":"mgtest2.com","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}},{"id":56,"ordernum":1999753213,"userid":1,"contactid":0,"date":"2020-09-24 11:45:46","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"30200.00","paymentmethod":"ipay88","invoiceid":72,"status":"Active","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"test","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":49,"producttype":"Hosting Account","product":"Shared Hosting - Starter","domain":"iwansakti.xyz","billingcycle":"Monthly","amount":"Rp.20,000 IDR","status":"Active"},{"type":"domain","relid":20,"producttype":"Domain","product":"Register","domain":"iwansakti.xyz","billingcycle":1,"amount":"Rp.10,200 IDR","status":"Active","dnsmanagement":1,"emailforwarding":0,"idprotection":0}]}},{"id":31,"ordernum":3357231065,"userid":3,"contactid":0,"date":"2020-09-18 13:57:53","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":34,"status":"Active","ipaddress":"91.192.166.30","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Pembayaran Online","paymentstatus":"Unpaid","name":"Modulesgarden Support","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":31,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"vps-d1dcb601.vps.ovh.ca","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Suspended"}]}},{"id":30,"ordernum":2871924540,"userid":1,"contactid":0,"date":"2020-09-17 14:25:54","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":33,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"test ovh 3","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":30,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Terminated"}]}},{"id":29,"ordernum":6918336188,"userid":1,"contactid":0,"date":"2020-09-17 04:22:36","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":32,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"Test beli iwan 2","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":29,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Cancelled"}]}},{"id":28,"ordernum":1295130072,"userid":1,"contactid":0,"date":"2020-09-17 03:24:46","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"a:0:{}","amount":"52500.00","paymentmethod":"ipay88","invoiceid":31,"status":"Cancelled","ipaddress":"0.0.0.0","fraudmodule":"","fraudoutput":"","notes":"order pertama","paymentmethodname":"Pembayaran Online","paymentstatus":"Paid","name":"iwan nasrulloh","currencyprefix":"Rp.","currencysuffix":" IDR","frauddata":"","lineitems":{"lineitem":[{"type":"product","relid":28,"producttype":"Other Product/Service","product":"VPS Linux - VPS Starter","domain":"","billingcycle":"Monthly","amount":"Rp.52,500 IDR","status":"Active"}]}}]}}';
//kita decode dulu
$decodenya = json_decode($emailtemplates, true);
$orders = array_column( (array_column( $decodenya['orders']['order'], 'lineitems')), 'lineitem' );
$product_counter = [];
foreach ($orders as $order_key => $each_order ) {
foreach ($each_order as $product) {
if ( isset ($product_counter[ $product['product'] ] ) ){
$product_counter[ $product['product'] ];
}else{
$product_counter[ $product['product'] ] = 1;
}
}
}
var_dump( $product_counter );