I have stdClass data after submitting the form:
"Array
(
[0] => stdClass Object
(
[username] => stdClass Object
(
[0] => 1001037
[1] => AARONJAMES
[2] => 1
[3] => ULTIMA (Red)
[4] => 1001037|1
[staff_id] => 1001037
[username] => AARONJAMES
[fkhouseid] => 1
[hname] => ULTIMA (Red)
[id] => 1001037|1
[$$hashKey] => object:13
)
[$$hashKey] => object:6
[points] => 50
)
[1] => stdClass Object
(
[username] => stdClass Object
(
[0] => 1001075
[1] => AARONKOH
[2] => 2
[3] => NACHOZZ (Purple)
[4] => 1001075|2
[staff_id] => 1001075
[username] => AARONKOH
[fkhouseid] => 2
[hname] => NACHOZZ (Purple)
[id] => 1001075|2
[$$hashKey] => object:14
)
[house] =>
[points] => 20
[$$hashKey] => object:306
)
)"
I want to access id variable inside username array to explode it later and insert data into database.
I tried to use to access it but it didnt work:
foreach ($persons as $key => $object) {
var_dump($persons[$key]->id);
}
How can I access id variable inside username?
Assuming $persons have array. You can try like below.
foreach ($persons as $key => $object) {
var_dump($object->username->id);
}
As per my comments you should try this
foreach ($persons as $key => $object) {
echo $object[$key]->username->id;
}
some details about array
$key is the index number of person array.
username is the stdClass Object so you can get like this ->username.
Related
I am getting below json data thru Shiprocket API. Now I want to extract value of below variables in PHP code from this json.
I have tried to use json_decode but it did not work and show null value:
$data = json_decode($json);
$sr_status = $data['shipment_status'];
Please suggest the code to retrieve below variables value.
shipment_status , awb_code , courier_company_id
Array
(
[0] => stdClass Object
(
[tracking_data] => stdClass Object
(
[track_status] => 1
[shipment_status] => 7
[shipment_track] => Array
(
[0] => stdClass Object
(
[id] => 180339484
[awb_code] => 11150911492
[courier_company_id] => 55
[shipment_id] => 1711169662
[order_id] => 233223781187
[pickup_date] => 2023-01-11 03:02:00
[delivered_date] => 2023-01-16 12:22:00
[weight] => 0.25
[packages] => 1
[current_status] => Delivered
[delivered_to] => Solapur
[destination] => Solapur
[consignee_name] => ABC
[origin] => Ludhiana
[courier_agent_details] =>
[edd] =>
)
)
[shipment_track_activities] => Array
(
[0] => stdClass Object
(
[date] => 2023-01-16 12:22:00
[status] => 000-T-DL
[activity] => SHIPMENT DELIVERED
[location] => SOLAPUR
[sr-status] => 7
[sr-status-label] => DELIVERED
)
[1] => stdClass Object
(
[date] => 2023-01-16 11:34:00
[status] => 002-S-UD
[activity] => SHIPMENT OUTSCAN
[location] => SOLAPUR
[sr-status] => 17
[sr-status-label] => OUT FOR DELIVERY
)
)
[track_url] => https://shiprocket.co//tracking/11150911492
[etd] => 2023-01-14 17:02:00
[qc_response] => stdClass Object
(
[qc_image] =>
[qc_failed_reason] =>
)
)
)
)
you can try this:
$array = ...; // Your array here
$data= json_decode($array);
$shipment_status = $data[0]->tracking_data->shipment_status;
$awb_code = $data[0]->tracking_data->shipment_track[0]->awb_code;
$courier_company_id = $data[0]->tracking_data->shipment_track[0]->courier_company_id;
Or use $data = json_decode($json,true); which return an array where you can use
foreach($data as $val) {
$shipment_status = $val['tracking_data']['shipment_status'];
foreach ($val['shipment_track'] as $value) {
$awb_code = $value['awb_code'];
$courier_company_id = $value['courier_company_id'];
}
}
I want to add elements to an array with a condition, but with this code the $ready array only work with the first value of array. what did i miss ?
$data['garduinduk'] :
Array ( [0] => stdClass Object (
[id] => 1
[id_app] => 2
[id_basecamp] => 1
[nama] => GI BOGOR BARU
[room] =>
)
[1] => stdClass Object (
[id] => 2
[id_app] => 2
[id_basecamp] => 1
[nama] => GI CIAWI BARU
[room] =>
)
[2] => stdClass Object (
[id] => 3
[id_app] => 2
[id_basecamp] => 1
[nama] => GI CIAWI
[room] =>
)
$ready :
Array ( [0] => stdClass Object (
[lokasi] => 1
[berlaku_dari] =>
[berlaku_sampai] => 0000-00-00
[status] => On_progress
[file] =>
)
[1] => stdClass Object (
[lokasi] => 16
[berlaku_dari] =>
[berlaku_sampai] => 0000-00-00
[status] => On_progress
[file] =>
)
[2] => stdClass Object (
[lokasi] => 2
[berlaku_dari] =>
[berlaku_sampai] => 0000-00-00
[status] => On_progress
[file] => /assets/files/155bd035e88358b.PNG
)
[3] => stdClass Object (
[lokasi] => 2
[berlaku_dari] => 2019-02-27
[berlaku_sampai] => 2019-02-28
[status] => Selesai
[file] => /assets/files/215c76691f12ae9.pdf
)
)
Code :
foreach ($data['garduinduk'] as $key => $gi){
foreach ($ready as $rdy){
if ($rdy->lokasi == $gi->id){
if ($rdy->status == 'On_progress'){
$gi->berlaku_dari = 'ON PROGRESS';
$gi->berlaku_sampai = 'ON PROGRESS';
$gi->file = 'ON PROGRESS';
}else{
$gi->berlaku_dari = $rdy->berlaku_dari;
$gi->berlaku_sampai = $rdy->berlaku_sampai;
$gi->file = $rdy->file;
}
}else{
$gi->berlaku_dari = '';
$gi->berlaku_sampai = '';
$gi->file = '';
}
}
}
Use reference on $gi
foreach ($data['garduinduk'] as $key => &$gi){
or use
$data['garduinduk'][$key]->
instead of
$gi->
I've that xml structure retrieving from device
<packet>
<info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" />
<ptu name="A" bres="Nobi">123.23</ptu>
<ptu name="B">123.23</ptu>
<ptu name="D">8</ptu>
<sale>999.23</sale>
</packet>
simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml);
print_r($array);
It prints
SimpleXMLElement Object
(
[info] => SimpleXMLElement Object
(
[#attributes] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[ptu] => Array
(
[0] => 123.23
[1] => 123.23
[2] => 8
)
[sale] => 999.23
)
As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){
$x = simplexml_load_string($xml);
$result = [];
function parse($xml, &$res){
$res['name'] = $xml->getName();
$res['value'] = $xml->__toString();
foreach ($xml->attributes() as $k => $v){
$res['attr'][$k] = $v->__toString();
}
foreach($xml->children() as $child){
parse($child, $res['children'][]);
}
}
parse($x, $result);
return $result;
}
$resArray = parseXMLtoArray($rawXml);
print_r($resArray);
this returns such array
Array
(
[name] => packet
[value] =>
[attr] => Array
(
[crc] => BKJFKHKD54
)
[children] => Array
(
[0] => Array
(
[name] => info
[value] =>
[attr] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[1] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => B
)
)
[3] => Array
(
[name] => ptu
[value] => 8
[attr] => Array
(
[name] => D
)
)
[4] => Array
(
[name] => sale
[value] => 999.23
)
)
)
Is this correct?
Thanks again Nigel
When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML()...
$array = simplexml_load_string($xml);
echo $array->asXML();
To check the attributes of <ptu> element, (this just gives the first one)...
echo $array->ptu[0]['name'];
This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'].
I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object
My object just like this
I want to add the the Object2 into Object1
Objet1
stdClass Object
(
[id_laporan_pemeriksa] => 5
[no_pkpt] => SNE
[tgl_pkpt] => 2010
[no_penugasan] => ST-4000/PW25/2/2017
[tgl_penugasan] => 2017-08-09
[judul_laporan] => Masukkan Kode disini
[no_laporan] => LBINA-9000/PW25/2/2017
[tgl_laporan] => 2017-08-01
[tahun_anggaran_penugasan] => 2009
[nilai_anggaran_penugasan] => 10000000
[realisasi_anggaran_penugasan] => 100000000
[jenis_anggaran_penugasan] => Utang
[sumber_laporan] => Inspektorat Maluku
[nama_sumber_penugasan] => PKPT
[nama_ketua_tim] => Abdul Rofiek, Ak.
[nama_pengendali_teknis] => Alfian Massagony, S.E.
[nama_unit_penugasan] => Irban Wil. I
[nama_penugasan] => Penjaminan
[nama_sub_penugasan] => Audit
[id_s_sub_penugasan] => 010105
[nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian.
)
Object2
stdClass Object
(
[id] => 3
[data_sebab] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_rekomendasi] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 9
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
[1] => stdClass Object
(
[id] => 5
[id_rekomendasi] =>
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 10
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 11
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
)
)
)
)
I have tried
$Object1['data']->$Object2;
But i got an error
Cannot use object of type stdClass as array
The syntax of adding $Object2 as a property of $Object1 is:
$Object1->Object2 = $Object2;
Or:
$Object1->{'Object2'} = $Object2;
It should be:
$Object1->data = $Object2; // it will create data element with obj2 as value
As the objects are objects and not arrays, using:
$Object1['data']->$Object2;
wont work. However doing the following will work:
$Object1->data = $Object2;
I got this result when I printed the array print_r($post);
Array ( [0] => Array ( [0] => stdClass Object ( [subscriber_id] => 80010055 [cto_id] => [name] => n [mobile] => 1234564444 [state] => [city] => fsd [cto_subscriber_id] => 0 [password] => e10adc3949ba59abbe56e057f20f883e [email] => n#n.com [email_verified] => 1 [ip_address] => ::1 [last_login_time] => 2012-10-24 11:37:19 ) ) [1] => Array ( [0] => stdClass Object ( [subscriber_id] => 80010055 [ip_address] => [landline_number] => [sex] => 0 [dob] => 0000-00-00 00:00:00 [marital_status] => hfg [state] => [addres] => fgh [city] => fghfg [pincode] => fghfgh ) ) )
How can I get single values
Typecast it to array?
$arr = (array)$post[0][0];
And then you'll be able to loop through it as through normal array
foreach ($arr as $key => $value){....}
foreach $post as $sub {
foreach $sub as $single {
echo $single;
}
}
Think you should learn the basics of arrays and objects first.
Anyway in order to get the value of lets say subscriber_id you should use the following:
$post[0][0]->subscriber_id
First you get into the array and then get the value out of the Object