How to access ' Json data in Laravel' [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I am getting the server response in json format to my laravel controller. In my controller decoding the json data and passing to my view. In laravel blade i am unable to access data.
When i am printing total decoded result it is some thing like below.
I tried to access this by $result->shipping_address , $result['shipping_address']. But none of them worked.
JSON RESULT is like below
[
{"data":
{ "entity_id":"77",
"state":"new",
"status":"pending",
"coupon_code":null,
"protect_code":"ab0fc2890c4c9cc93d7d4586e5dcd8ff",
"shipping_description":"Flat Rate - Fixed",
"is_virtual":"0",
"store_id":"1",
"customer_id":"2",
"base_discount_amount":"0.0000",
"base_discount_canceled":null,
"base_discount_invoiced":null,
"base_discount_refunded":null,
"base_grand_total":"2146.2300",
"base_shipping_amount":"5.0000",
"base_shipping_canceled":null,
"base_shipping_invoiced":null,
"base_shipping_refunded":null,
"base_shipping_tax_amount":"0.0000",
"base_shipping_tax_refunded":null,
"base_subtotal":"1846.6800",
"base_subtotal_canceled":null,
"base_subtotal_invoiced":null,
"base_subtotal_refunded":null,
"base_tax_amount":"294.5500",
"base_tax_canceled":null,
"base_tax_invoiced":null,
"base_tax_refunded":null,
"base_to_global_rate":"1.0000",
"base_to_order_rate":"1.0000",
"base_total_canceled":null,
"base_total_invoiced":null,
"base_total_invoiced_cost":null,
"base_total_offline_refunded":null,
"base_total_online_refunded":null,
"base_total_paid":null,
"base_total_qty_ordered":null,
"base_total_refunded":null,
"discount_amount":"0.0000",
"discount_canceled":null,
"discount_invoiced":null,
"discount_refunded":null,
"grand_total":"2146.2300",
"shipping_amount":"5.0000",
"shipping_canceled":null,
"shipping_invoiced":null,
"shipping_refunded":null,
"shipping_tax_amount":"0.0000",
"shipping_tax_refunded":null,
"store_to_base_rate":"0.0000",
"store_to_order_rate":"0.0000",
"subtotal":"1846.6800",
"subtotal_canceled":null,
"subtotal_invoiced":null,
"subtotal_refunded":null,
"tax_amount":"294.5500",
"tax_canceled":null,
"tax_invoiced":null,
"tax_refunded":null,
"total_canceled":null,
"total_invoiced":null,
"total_offline_refunded":null,
"total_online_refunded":null,
"total_paid":null,
"total_qty_ordered":"1.0000",
"total_refunded":null,
"can_ship_partially":null,
"can_ship_partially_item":null,
"customer_is_guest":"0",
"customer_note_notify":"1",
"billing_address_id":"76",
"customer_group_id":"1",
"edit_increment":null,
"email_sent":null,
"send_email":"1",
"forced_shipment_with_invoice":null,
"payment_auth_expiration":null,
"quote_address_id":null,
"quote_id":"72",
"shipping_address_id":"75",
"adjustment_negative":null,
"adjustment_positive":null,
"base_adjustment_negative":null,
"base_adjustment_positive":null,
"base_shipping_discount_amount":"0.0000",
"base_subtotal_incl_tax":"2141.2300",
"base_total_due":"2146.2300",
"payment_authorization_amount":null,
"shipping_discount_amount":"0.0000",
"subtotal_incl_tax":"2141.2300",
"total_due":"2146.2300",
"weight":"0.0000",
"customer_dob":null,
"increment_id":"000000157",
"applied_rule_ids":null,
"base_currency_code":"INR",
"customer_email":"test#gmail.com",
"customer_firstname":"ridha",
"customer_lastname":"Gatto",
"customer_middlename":null,
"customer_prefix":null,
"customer_suffix":null,
"customer_taxvat":null,
"discount_description":null,
"ext_customer_id":null,
"ext_order_id":null,
"global_currency_code":"INR",
"hold_before_state":null,
"hold_before_status":null,
"order_currency_code":"INR",
"original_increment_id":null,
"relation_child_id":null,
"relation_child_real_id":null,
"relation_parent_id":null,
"relation_parent_real_id":null,
"remote_ip":"193.33.131.226",
"shipping_method":"flatrate_flatrate",
"store_currency_code":"INR",
"store_name":"Main Website\nMain Website Store\n",
"x_forwarded_for":null,
"customer_note":null,
"created_at":"2018-11-28 06:01:59",
"updated_at":"2018-11-28 06:02:09",
"total_item_count":"1",
"customer_gender":"0",
"discount_tax_compensation_amount":"0.0000",
"base_discount_tax_compensation_amount":"0.0000",
"shipping_discount_tax_compensation_amount":"0.0000",
"base_shipping_discount_tax_compensation_amnt":"0.0000",
"discount_tax_compensation_invoiced":null,
"base_discount_tax_compensation_invoiced":null,
"discount_tax_compensation_refunded":null,
"base_discount_tax_compensation_refunded":null,
"shipping_incl_tax":"5.0000",
"base_shipping_incl_tax":"5.0000",
"coupon_rule_name":null,
"gift_message_id":null,
"paypal_ipn_customer_notified":"0",
"vendor_order_id":"VN100028",
"extension_attributes":{
},
"items":{"50":
{
}
}
}
},
{ "shipping_address":
{
"entity_id":"75",
"parent_id":"77",
"customer_address_id":null,
"quote_address_id":"250",
"region_id":"564",
"customer_id":null,
"fax":null,
"region":"chennai",
"postcode":"700089",
"lastname":"Gatto",
"street":"srinaarat",
"city":"chennai",
"email":"test#gmail.com",
"telephone":"9876543210",
"country_id":"IN",
"firstname":"hgd",
"address_type":"shipping",
"prefix":"address_","middlename":null,
"suffix":null,
"company":"sd",
"vat_id":null,
"vat_is_valid":null,
"vat_request_id":null,
"vat_request_date":null,
"vat_request_success":null
}
},
{ "billing_address":
{
"entity_id":"76",
"parent_id":"77",
"customer_address_id":"1",
"quote_address_id":"251",
"region_id":"564",
"customer_id":null,
"fax":null,
"region":"chennai",
"postcode":"700034",
"lastname":"Gatt0",
"street":"srinagar",
"city":"chennai",
"email":"text#gmail.com",
"telephone":"9848352205",
"country_id":"IN",
"firstname":"ridha",
"address_type":"billing",
"prefix":null,
"middlename":null,
"suffix":null,
"company":"ridha",
"vat_id":null,
"vat_is_valid":null,
"vat_request_id":null,
"vat_request_date":null,
"vat_request_success":null
}
},
{ "vendor":
{
"entity_id":"1",
"entity_type_id":"9",
"attribute_set_id":"0",
"increment_id":"000000009",
"parent_id":"0",
"created_at":"2018-10-01 11:09:32",
"updated_at":"2018-11-27 12:55:57",
"is_active":"1",
"website_id":"1",
"address":"chennai",
"city":"chennai",
"zip_code":"700087",
"customer_id":"1",
"shop_url":"cf97wksk",
"status":"approved",
"group":"general",
"public_name":"cf97wksk",
"name":"test",
"email":"test#gmail.com",
"contact_number":"9876543210",
"country_id":"IN",
"vendor_gstin":"11PGTAU12346Q",
"gender":"1",
"region_id":"564"
}
},
{ "items":
{"item_id":"50",
"order_id":"77",
"parent_item_id":null,
"quote_item_id":"208",
"store_id":"1",
"created_at":"2018-11-28 06:02:09",
"updated_at":"2018-11-28 06:02:09",
"product_id":"14",
"product_type":"simple",
"product_options":
{
"info_buyRequest":
{"uenc":"aHR0cDovLzE1OS44OS4xNjQuMTM5L2IyYi9idXkxLWtsbS1mYXNoaW9uLW1hbGwtd29tZW4tcy1mYW5jeS1jb3R0b24tc2lsay1zYXJlZS1yZWQteWVsbG93LXNrdS1zcmlwbC0yODU0Lmh0bWw,","product":"14","selected_configurable_option":"","related_product":"","qty":"1"},
"additional_options":[{"code":"vendor_name","label":"Vendor","value":"cf97wksk"}]},
"weight":null,
"is_virtual":"0",
"sku":"DKSR0000001-1",
"name":"KLM Fashion Mall Women's Fancy Cotton Silk Saree ( RED & YELLOW )",
"description":null,
"applied_rule_ids":null,
"additional_data":null,
"is_qty_decimal":"0",
"no_discount":"0",
"qty_backordered":null,
"qty_canceled":"0.0000",
"qty_invoiced":"0.0000",
"qty_ordered":"1.0000",
"qty_refunded":"0.0000",
"qty_shipped":"0.0000",
"base_cost":null,
"price":"1846.6800",
"base_price":"1846.6800",
"original_price":"1846.6800",
"base_original_price":"1846.6800",
"tax_percent":"5.0000",
"tax_amount":"294.5500",
"base_tax_amount":"294.5500",
"tax_invoiced":"0.0000",
"base_tax_invoiced":"0.0000",
"discount_percent":"0.0000",
"discount_amount":"0.0000",
"base_discount_amount":"0.0000",
"discount_invoiced":"0.0000",
"base_discount_invoiced":"0.0000",
"amount_refunded":"0.0000",
"base_amount_refunded":"0.0000",
"row_total":"1846.6800",
"base_row_total":"1846.6800",
"row_invoiced":"0.0000",
"base_row_invoiced":"0.0000",
"row_weight":"0.0000",
"base_tax_before_discount":null,
"tax_before_discount":null,
"ext_order_item_id":null,
"locked_do_invoice":null,
"locked_do_ship":null,
"price_incl_tax":"2141.2300",
"base_price_incl_tax":"2141.2300",
"row_total_incl_tax":"2141.2300",
"base_row_total_incl_tax":"2141.2300",
"discount_tax_compensation_amount":"0.0000",
"base_discount_tax_compensation_amount":"0.0000",
"discount_tax_compensation_invoiced":null,
"base_discount_tax_compensation_invoiced":null,
"discount_tax_compensation_refunded":null,
"base_discount_tax_compensation_refunded":null,
"tax_canceled":null,
"discount_tax_compensation_canceled":null,
"tax_refunded":null,
"base_tax_refunded":null,
"discount_refunded":null,
"base_discount_refunded":null,
"free_shipping":"0",
"gift_message_id":null,
"gift_message_available":"0",
"weee_tax_applied":null,
"weee_tax_applied_amount":null,
"weee_tax_applied_row_amount":null,
"weee_tax_disposition":null,
"weee_tax_row_disposition":null,
"base_weee_tax_applied_amount":null,
"base_weee_tax_applied_row_amnt":null,
"base_weee_tax_disposition":null,
"base_weee_tax_row_disposition":null,
"vendor_id":"1",
"seller_price":"1679",
"hsn":"5007",
"seller_item_total":"1678.80",
"vendor_tax":"83.94",
"vendor_row_total":"1762.74",
"cess_rate":"0.00",
"product":{},
"thumbnail":"\/1\/5\/1538546849_sku-sripl-2854_1.png",
}
}
]
Array
(
[shipping_address] => Array
(
[entity_id] => 1
[parent_id] => 4
[customer_address_id] => 1
[quote_address_id] => 4
[region_id] => 564
[customer_id] =>
[fax] =>
[region] => Telangana
[postcode] => 500049
[lastname] => Latname
[street] => Road No 23
Deepthisree Nagar Madinaguda
[city] => India
[email] => asarush#xmial.com
[telephone] => 9999999999
[country_id] => IN
[firstname] => Aakshay
[address_type] => shipping
[prefix] =>
[middlename] =>
[suffix] =>
[company] => Aarush
[vat_id] =>
[vat_is_valid] =>
[vat_request_id] =>
[vat_request_date] =>
[vat_request_success] =>
)
)
Array
(
[billing_address] => Array
(
[entity_id] => 2
[parent_id] => 4
[customer_address_id] => 1
[quote_address_id] => 10
[region_id] => 564
[customer_id] =>
[fax] =>
[region] => Telangana
[postcode] => 500076
[lastname] => Gupta
[street] => Road No 23
Golkonda
[city] => Hyderabad
[email] => fradnsss#gmail.com
[telephone] => 9999999999
[country_id] => IN
[firstname] => Firsthan
[address_type] => billing
[prefix] =>
[middlename] =>
[suffix] =>
[company] => Firstname
[vat_id] =>
[vat_is_valid] =>
[vat_request_id] =>
[vat_request_date] =>
[vat_request_success] =>
)
)
Array
(
[vendor] => Array
(
[entity_id] => 1
[entity_type_id] => 9
[attribute_set_id] => 0
[increment_id] => 000000009
[parent_id] => 0
[created_at] => 2018-10-01 11:09:32
[updated_at] => 2018-11-27 12:55:57
[is_active] => 1
[website_id] => 1
[address] => hyyderabad
[city] => hyderabad
[zip_code] => 500055
[customer_id] => 1
[shop_url] => cf97wksk
[status] => approved
[group] => general
[public_name] => cf97wksk
[name] => Firstname Lastname
[email] => testemail#gmail.com
[contact_number] => 9999999999
[country_id] => IN
[vendor_gstin] => 11PGTAU12346Q
[gender] => 1
[region_id] => 564
)
)
In my controller sending the data to view as follows:
$result = json_decode($result_json,true);
return view('orders.view',compact('result'));
I need to display shipping address and billing address in my view blade. How can i do that.
I expect the out put like below
Shipping Address
Firstname Lastname
Hyderabad
Pin Code : 500072
Contact Number : 999888777

In your controller
return view('view-name', [
'shipping_address' => $shipping_address
// ...
]);
In your view-name.blade.php file:
<input id="shipping_address" type="hidden" value="#json($shipping_address)">
In a Javascript file:
let shipping_address= $('#shipping_address').val();
console.log(shipping_address)
See console log in browser debug and you can do anything.

you cannot use $result->shipping_address because as per your output that is array.
Try below one.
echo $result[1]['shipping_address']['firstname'] . ' ' . $result[1]['shipping_address']['lastname'] . ' ' . $result[1]['shipping_address']['city'] . 'Pin code: ' . $result[1]['shipping_address']['postcode'] . ' ' . 'Contact Number : ' . $result[1]['shipping_address']['telephone'];
or
{{$result[1]['shipping_address']['firstname']}} {{$result[1]['shipping_address']['lastname']}} {{$result[1]['shipping_address']['city']}} Pin code: {{$result[1]['shipping_address']['postcode']}} Contact Number : {{$result[1]['shipping_address']['telephone']}}
I have just provided option to print value, you may required some validation for variables.
Thank you.

Related

PHP exif_read_data returning weird character [duplicate]

My application allows users to upload an image with "embedded" captions. These captions are taken from the exif data:
foreach (exif_read_data('image.jpg', 0, true) as $key => $section) {
foreach ($section as $name => $val) {
if ($key == 'IFD0' && $name == 'Title') {
$caption = $val;
break;
}
}
if ($caption !== '') { break; }
}
//save $caption to database
The images are displayed along with their captions. Each image's caption is rendered 2 times:
title attribute of an <a> that is wrapped around the image
in a div that contains the caption
This is the result:
<a title="T?h?i?s? ?i?s? ?a? ?c?a?p?t?i?o?n?" href="#"><img src="image.jpg"></a>
<div class="caption">This is a caption.</div>
As you can see, when the caption is inside the title attribute, question marks appear. Why is this so?
I would note that I have made the whole application work with utf8 (database, html pages). I have also tried to utf8_encode() the extracted caption before saving it into the database, to no avail.
Any ideas?
EDIT:
By the way, the reason for using an <a> is so that the image appears in a jQuery lightbox when clicked.
EDIT:
#tpaksu, here is an actual dump of the result of exif_read_data('image.jpg', 0, true)
Array
(
[FILE] => Array
(
[FileName] => 174.jpg
[FileDateTime] => 1332196128
[FileSize] => 236179
[FileType] => 2
[MimeType] => image/jpeg
[SectionsFound] => ANY_TAG, IFD0, EXIF, GPS, WINXP
)
[COMPUTED] => Array
(
[html] => width="640" height="427"
[Height] => 427
[Width] => 640
[IsColor] => 1
[ByteOrderMotorola] => 1
[CCDWidth] => 9mm
[ApertureFNumber] => f/11.0
[UserComment] =>
[UserCommentEncoding] => UNDEFINED
)
[IFD0] => Array
(
[ImageDescription] => STATE OF CALAMITY/FEB. 2, 2012: Firemen and residents of sitio San Roque in Talisay City help each other in putting off the fire. The Talisay City council wants to declare sitio Laray in a state of calamity.(CDN PHOTO/JUNJIE MENDOZA)
[Make] => Canon
[Model] => Canon EOS 50D
[Orientation] => 1
[Software] => Adobe Photoshop CS2 Windows
[DateTime] => 2012:02:04 18:54:15
[YCbCrPositioning] => 1
[UndefinedTag:0x1001] => 4752
[UndefinedTag:0x1002] => 3168
[Exif_IFD_Pointer] => 2544
[GPS_IFD_Pointer] => 5304
[Title] => S�T�A�T�E� �O�F� �C�A�L�A�M�I�T�Y�/�F�E�B�.� �2�,� �2�0�1�2�:� �F�i�r�e�m�e�n� �a�n�d� �r�e�s�i�d�e�n�t�s� �o�f� �s�i�t�i�o� �S�a�n� �R�o�q�u�e� �i�n� �T�a�l�i�s�a�y� �C�i�t�y� �h�e�l�p� �e�a�c�h� �o�t�h�e�r� �i�n� �p�u�t�t�i�n�g� �o�f�f� �t�h�e� �f�i�r�e�.� �T�h�e� �T�a�l�i�s�a�y� �C�i�t�y� �c�o�u�n�c�i�l� �w�a�n�t�s� �t�o� �d�e�c�l�a�r�e� �s�i�t�i�o� �L�a�r�a�y� �i�n� �a� �s�t�a�t�e� �o�f� �c�a�l�a�m�i�t�y�.�(�C�D�N� �P�H�O�T�O�/�J�U�N�J�I�E� �M�E�N�D�O�Z�A�)���
[Subject] => S�T�A�T�E� �O�F� �C�A�L�A�M�I�T�Y�/�F�E�B�.� �2�,� �2�0�1�2�:� �F�i�r�e�m�e�n� �a�n�d� �r�e�s�i�d�e�n�t�s� �o�f� �s�i�t�i�o� �S�a�n� �R�o�q�u�e� �i�n� �T�a�l�i�s�a�y� �C�i�t�y� �h�e�l�p� �e�a�c�h� �o�t�h�e�r� �i�n� �p�u�t�t�i�n�g� �o�f�f� �t�h�e� �f�i�r�e�.� �T�h�e� �T�a�l�i�s�a�y� �C�i�t�y� �c�o�u�n�c�i�l� �w�a�n�t�s� �t�o� �d�e�c�l�a�r�e� �s�i�t�i�o� �L�a�r�a�y� �i�n� �a� �s�t�a�t�e� �o�f� �c�a�l�a�m�i�t�y�.�(�C�D�N� �P�H�O�T�O�/�J�U�N�J�I�E� �M�E�N�D�O�Z�A�)���
[UndefinedTag:0xEA1C] => �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
)
[EXIF] => Array
(
[ExposureTime] => 1/125
[FNumber] => 11/1
[ISOSpeedRatings] => 400
[ExifVersion] => 0221
[DateTimeOriginal] => 2012:02:02 12:40:00
[DateTimeDigitized] => 2012:02:02 12:40:00
[ComponentsConfiguration] => �
[ShutterSpeedValue] => 458752/65536
[ApertureValue] => 458752/65536
[ExposureBiasValue] => 0/1
[Flash] => 16
[FocalLength] => 16/1
[UserComment] => ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
[SubSecTimeOriginal] => 00
[SubSecTimeDigitized] => 00
[FlashPixVersion] => 0100
[ColorSpace] => 1
[ExifImageWidth] => 2000
[ExifImageLength] => 1333
[FocalPlaneXResolution] => 4752000/894
[FocalPlaneYResolution] => 3168000/597
[FocalPlaneResolutionUnit] => 2
[CustomRendered] => 0
[ExposureMode] => 1
[WhiteBalance] => 0
[SceneCaptureType] => 0
[UndefinedTag:0xEA1C] => �������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
)
[GPS] => Array
(
[GPSVersion] => ��
)
[WINXP] => Array
(
[Title] => STATE OF CALAMITY/FEB. 2, 2012: Firemen and residents of sitio San Roque in Talisay City help each other in putting off the fire. The Talisay City council wants to declare sitio Laray in a state of calamity.(CDN PHOTO/JUNJIE MENDOZA)
[Subject] => STATE OF CALAMITY/FEB. 2, 2012: Firemen and residents of sitio San Roque in Talisay City help each other in putting off the fire. The Talisay City council wants to declare sitio Laray in a state of calamity.(CDN PHOTO/JUNJIE MENDOZA)
)
)
Windows 7 seems to encode IFD0 tags using UTF-16 Little Endian, so you can also try parsing those values with iconv() and trim() the resulting string just in case:
$exif_data = exif_read_data( $file, NULL, true, false );
if ( isset( $exif_data['IFD0'] ) ) {
$title = trim(iconv('UTF-16LE','UTF-8',$ifd0['Title']));
}
Of course it would be wise to make sure your system provides UTF-16LE and UTF-8
Ok try this inside your code.
if ($key == 'IFD0' && $name == 'Title') {
if(strpos($val,"�")>0){
for($i=0;$i<len($val);$i++){
$newval.=$val[i];
$i++;
}
}
$caption = $val;
break;
}
or
if ($key == 'IFD0' && $name == 'Title') {
$caption = preg_replace('/[^(\x20-\x7F)]*/','', $val);
break;
}

PHP - select value by key (date), then return values sum

I may have stupid question but I do not know how to solve my problem.
What I want to achieve.
I need to divide the field by date (key) (specifically, I'm interested in the year) and figure out the total amount (value) for each year.
my array:
Array
(
[2017-04-20 15:26:08] => 0.00
[2016-10-11 13:52:05] => 0.00
[2016-05-26 13:04:14] => 200000.00
[2016-05-24 14:32:34] => 0.00
[2016-05-24 11:32:17] => 0.00
[2016-03-29 19:02:24] => 79800.00
[2016-02-16 07:54:38] => 0.00
[2015-12-16 19:01:55] => 149900.00
[2015-01-30 15:07:38] => 402103.00
[2014-11-03 15:13:29] => 0.00
[2014-10-15 15:58:44] => 129000.00
[2014-10-06 12:44:52] => 0.00
[2014-07-28 10:47:45] => 0.00
[2014-07-23 15:50:24] => 133333.33
[2014-05-16 12:39:25] => 0.00
[2014-04-29 13:21:01] => 5045524.00
[2014-03-04 15:28:03] => 0.00
[2014-01-13 09:41:47] => 0.00
[2013-11-26 08:40:20] => 70000.00
[2013-07-22 12:17:24] => 0.00
[2012-10-05 13:51:06] => 0.00
[2012-03-08 15:45:44] => 149880.00
[2012-03-02 13:11:19] => 0.00
[2012-02-14 09:26:43] => 0.00
[2011-12-05 10:44:23] => 0.00
[2011-12-05 10:33:53] => 1500000.00
[2011-07-01 14:40:22] => 0.00
[2011-05-30 09:54:22] => 1680000.00
[2011-01-13 08:59:14] => 72000.00
)
my code:
$min_year = date('Y',strtotime(min(array_column($MY_ARRAY,'published_at'))));
$max_year = date('Y',strtotime(max(array_column($MY_ARRAY,'published_at'))));
for($year = $max_year; $year >= $min_year; $year--){
$amount_array = array_column($MY_ARRAY,'contract_amount','published_at');
echo 'Year: ' .$year . ', Total amount for this year: ' . ???? . ' €';
}
how should I do it?
edit// part of original array
Array
(
[0] => stdClass Object
(
[id] => 2898917
[contract_identifier] => 155/AD00/2017
[contracting_authority_name] => some company
[contracting_authority_formatted_address] => address
[contracting_authority_cin] => 123456789
[supplier_name] => another company
[supplier_formatted_address] =>
[supplier_cin] => n123456789mber
[subject] => contract title
[subject_description] =>
[signed_on] => 2017-04-19
[effective_note] =>
[contract_price_amount] => 0.00
[contract_price_total_amount] => 0.00
[note] =>
[published_at] => 2017-04-20 15:26:08
[changed_at] => 2017-04-20 15:10:09
[change_note] =>
[internal_id] => 155
[internal_note] =>
[confirmation_file_name] =>
[confirmed_on] =>
[source_id] => 2
[description] =>
[reference] =>
[effective_from] => 2017-04-21
[effective_to] =>
[crz_department_name] => department
[crz_status_name] => 1
[crz_type_name] => 1
[crz_kind_name] => 2
[crz_confirmation_status_name] => 5
[crz_attachments_title] => 11111
[crz_attachments_file_name] =>
[crz_attachments_file_size] =>
[crz_attachments_scan_file_name] => 2898918_dokument.pdf
[crz_attachments_scan_file_size] => 373463
)
[1] => stdClass Object
(
[id] => 2635819
[contract_identifier] => 241
[contracting_authority_name] => ...
...
...
I would do something like this:
Loop through the whole stuff.
Create another array.
Get the date part and parse the year.
Cumulatively add the years as the key and value as the sum.
Display.
Code:
<?php
$arr = array(
"2017-04-20 15:26:08" => 0.00,
"2016-10-11 13:52:05" => 0.00,
"2016-05-26 13:04:14" => 200000.00,
"2016-05-24 14:32:34" => 0.00,
"2016-05-24 11:32:17" => 0.00,
"2016-03-29 19:02:24" => 79800.00,
"2016-02-16 07:54:38" => 0.00,
"2015-12-16 19:01:55" => 149900.00,
"2015-01-30 15:07:38" => 402103.00,
"2014-11-03 15:13:29" => 0.00,
"2014-10-15 15:58:44" => 129000.00,
"2014-10-06 12:44:52" => 0.00,
"2014-07-28 10:47:45" => 0.00,
"2014-07-23 15:50:24" => 133333.33,
"2014-05-16 12:39:25" => 0.00,
"2014-04-29 13:21:01" => 5045524.00,
"2014-03-04 15:28:03" => 0.00,
"2014-01-13 09:41:47" => 0.00,
"2013-11-26 08:40:20" => 70000.00,
"2013-07-22 12:17:24" => 0.00,
"2012-10-05 13:51:06" => 0.00,
"2012-03-08 15:45:44" => 149880.00,
"2012-03-02 13:11:19" => 0.00,
"2012-02-14 09:26:43" => 0.00,
"2011-12-05 10:44:23" => 0.00,
"2011-12-05 10:33:53" => 1500000.00,
"2011-07-01 14:40:22" => 0.00,
"2011-05-30 09:54:22" => 1680000.00,
"2011-01-13 08:59:14" => 72000.00
);
$sum = array();
foreach ($arr as $year => $value) {
$year = date('Y', strtotime($year));
if (!isset($sum[$year]))
$sum[$year] = 0.0;
$sum[$year] += $value;
}
?>
Output
array(7) {
[2017]=>
float(0)
[2016]=>
float(279800)
[2015]=>
float(552003)
[2014]=>
float(5307857.33)
[2013]=>
float(70000)
[2012]=>
float(149880)
[2011]=>
float(3252000)
}
Would be way more easier to do something like:
$sum = array();
foreach ($MY_ARRAY as $date => $value) {
$year = substr($date, 0, 4);
$sum[$year] = (isset($sum[$year]) ? $sum[$year] : 0) + $value;
}
print_r($sum);

php WhatsApp API: Identity error while trying to log in

I am using PHP WhatsApp Library. As per instructions I got my login and password. Upon using I am getting error:
] Logging in as 'SmartConnect Agent' (12334) tx  <stream:features></stream:features>
tx  <auth user="12334" mechanism="WAUTH-2"></auth>
rx  <from s.whatsapp.net=""></from>
rx  <stream:features></stream:features>
rx  <challenge>fafa1216bdf3c4c4528e10d80b29c6026493682d</challenge>
tx  <response>19d8120136e5897f38fe0904e54611cd8510f029bdd97f077f460faba15bc68cd0644e87ebf4b8288a143dc1914cb61bf4bfa2f17e0d7187afa8fcd18e69bb2b75d4f2da549c3d3c3facedd808bc</response>
rx  <failure>
rx    <not-authorized></not-authorized>
rx  </failure>
Upon running blockchecker.php I get. Even same thing when I am retying to register by using registerTool.php
Wrong identity.
Array
(
[cc] => 92
[in] => 1234566
[lg] => en
[lc] => PK
[id] => >zv?+?????w!l?v?5b?
[mistyped] => 6
[network_radio_type] => 1
[simnum] => 1
[s] =>
[copiedrc] => 1
[hasinrc] => 1
[rcmatch] => 1
[pid] => 579
[extexist] => 1
[extstate] => 1
)
stdClass Object
(
[status] => fail
[reason] => incorrect
[sms_length] => 6
[voice_length] => 6
[sms_wait] => 0
[voice_wait] => 0
)
It generated multiple IDentity file of a same number. I removed all and it generated new password.

Extract item from an array

I want to be able to extract the output of [HostName] from this array in PHP. Thank you!
Array
(
[Protocol] => 17
[HostName] => [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
[Map] => ba_jail_hellsgamers_fx6
[ModDir] => cstrike
[ModDesc] => Counter-Strike: Source
[AppID] => 240
[Players] => 12
[MaxPlayers] => 64
[Bots] => 0
[Dedicated] => d
[Os] => l
[Password] =>
[Secure] => 1
[Version] => 1.0.0.75
[GamePort] => 27015
[ServerID] => -682722125
[GameTags] => HLstatsX:CE,alltalk,bunnyhopping,cool,drugs,free,gameme,gang,hellsgamers,hg,increased_maxplayers,jail,jailbr,nostats,startmoney
)
Like this:
echo $Array[HostName]; // output: [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
where $Array is the array you printed and copied here.
I recommend you start reading stuff from php.net - as this is the most basic thing you can do.
Thats the most simplest question .........
Just use this
echo $Your_array[HostName];

PHP OOP: Weird Array Return

I have the following 'Course' class:
class Course {
// The constructor just sets the database object
public function __construct($mysqli) {
$this->mysqli = $mysqli;
}
public function getCourseInfoByID($id) {
$result = $this->mysqli->query("SELECT * FROM courses WHERE id='$id'");
$course_info = $result->fetch_array();
// If found, return the student object
if($course_info) {
return $course_info;
}
return FALSE;
}
}
When I declare the class and try to run the function "getCourseInfoByID", I get weird results (see below)
$cid = process_get_request('cid');
$course = new Course($mysqli);
if(! $course_info = $course->getCourseInfoByID($cid)) {
$error[] = "Invalid Course ID";
setError();
redirectTo("instructors.php");
}
print_r($course_info);
I get this:
Array ( [0] => 2 [id] => 2 [1] => 1 [course_type_id] => 1 [2] => 1 [instructor_id] => 1 [3] => Tooele [dz_name] => Tooele [4] => 4 Airport Road [dz_address] => 4 Airport Road [5] => Tooele [dz_city] => Tooele [6] => Utah [dz_state] => Utah [7] => 84020 [dz_zip] => 84020 [8] => [dz_email] => [9] => 2011-12-30 17:25:12 [created] => 2011-12-30 17:25:12 [10] => 2012-01-02 16:24:08 [start_date] => 2012-01-02 16:24:08 [11] => 2012-01-08 16:24:17 [end_date] => 2012-01-08 16:24:17 [12] => 10 [student_slots] => 10 [13] => Brett will also be assisting in teaching this course as Nathan's assistant. Brett paid Nathan quite well to be his assistant. [notes] => Brett will also be assisting in teaching this course as Nathan's assistant. Brett paid Nathan quite well to be his assistant. [14] => 0 [approved_by] => 0 [15] => 0000-00-00 00:00:00 [approved_on] => 0000-00-00 00:00:00 [16] => 0 [completed] => 0 )
Why is each record duplicated?
This is happening because you are returning both numeric and associative indexes. You should use fetch_assoc() or pass the appropriate constant MYSQLI_ASSOC or MYSQLI_NUM to return just those keys.
See documentation on mysqli_result::fetch_array().
As an aside I would type hint your constructor to force the passing of a mysqli class so you can't accidentally pass an invalid argument.
Read the docs for the fetch_array() method. The second parameter defaults to MYSQLI_BOTH.

Categories