I query db and I have an array. How can I access stdClass object ? And I want also serializedName property like in java serializedName. So, The fields which query from db must be assign with my custom object class fields.How can I use this?
Array
(
[0] => stdClass Object
(
[receipt_id] => 1
[defect] => asd
[tec_report] => sad
[doc_number] => asd
[warrant] => 12
[warrant_start] => 2016-01-05
[warrant_end] => 2016-01-29
[repair_time] => 3
[receipt_date] => 2016-01-05
[delivery_date] =>
[service_type_id] => 0
[service_price] => 0
[part_price] => 0
[pay_type] =>
[labour] => 0
[summary] => 0
[technician_id] =>
[customer_id] => 0
[device_id] => 0
[seri_number] =>
)
[1] => stdClass Object
(
[receipt_id] => 2
[defect] => asdsad
[tec_report] => asds
[doc_number] =>
[warrant] =>
[warrant_start] =>
[warrant_end] =>
[repair_time] =>
[receipt_date] => 2016-01-05
[delivery_date] =>
[service_type_id] => 1
[service_price] => 12
[part_price] => 12
[pay_type] =>
[labour] => 12
[summary] => 12
[technician_id] =>
[customer_id] => 21
[device_id] => 12
[seri_number] =>
)
)
try this for one by one
echo $array[0]->receipt_id;
try this :
foreach($yourArray as $val){
echo $val->receipt_id;
echo "<br/>".$val->defect;
}
Related
I am working on something in Wordpress and need to parse an array of objects and get some specific property information values.
I believe I am having issues be cause the property name has colons in it:
EX: id:DLM_Download_Version:private
The arrays name is $versions. This is the value of the array when i use print_r:
(
[0] => DLM_Download_Version Object
(
[id:DLM_Download_Version:private] => 208
[author:DLM_Download_Version:private] => 1
[download_id:DLM_Download_Version:private] => 196
[menu_order:DLM_Download_Version:private] => 0
[latest:DLM_Download_Version:private] =>
[date:DLM_Download_Version:private] => DateTime Object
(
[date] => 2022-03-22 12:48:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[version:DLM_Download_Version:private] => 3
[download_count:DLM_Download_Version:private] => 0
[filesize:DLM_Download_Version:private] => 2966739
[md5:DLM_Download_Version:private] =>
[sha1:DLM_Download_Version:private] =>
[sha256:DLM_Download_Version:private] =>
[crc32b:DLM_Download_Version:private] =>
[mirrors:DLM_Download_Version:private] => Array
(
[0] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-SENTIMENT-REPORT-3-21-22.pdf
)
[url:DLM_Download_Version:private] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-SENTIMENT-REPORT-3-21-22.pdf
[filename:DLM_Download_Version:private] => STS-SENTIMENT-REPORT-3-21-22.pdf
[filetype:DLM_Download_Version:private] => pdf
)
[1] => DLM_Download_Version Object
(
[id:DLM_Download_Version:private] => 200
[author:DLM_Download_Version:private] => 1
[download_id:DLM_Download_Version:private] => 196
[menu_order:DLM_Download_Version:private] => 1
[latest:DLM_Download_Version:private] =>
[date:DLM_Download_Version:private] => DateTime Object
(
[date] => 2022-03-22 12:37:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[version:DLM_Download_Version:private] => 2
[download_count:DLM_Download_Version:private] => 2
[filesize:DLM_Download_Version:private] => 3059630
[md5:DLM_Download_Version:private] =>
[sha1:DLM_Download_Version:private] =>
[sha256:DLM_Download_Version:private] =>
[crc32b:DLM_Download_Version:private] =>
[mirrors:DLM_Download_Version:private] => Array
(
[0] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/sts-weekly-sentiment-report-3-14-22.pdf
)
[url:DLM_Download_Version:private] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/sts-weekly-sentiment-report-3-14-22.pdf
[filename:DLM_Download_Version:private] => sts-weekly-sentiment-report-3-14-22.pdf
[filetype:DLM_Download_Version:private] => pdf
)
[2] => DLM_Download_Version Object
(
[id:DLM_Download_Version:private] => 197
[author:DLM_Download_Version:private] => 1
[download_id:DLM_Download_Version:private] => 196
[menu_order:DLM_Download_Version:private] => 2
[latest:DLM_Download_Version:private] =>
[date:DLM_Download_Version:private] => DateTime Object
(
[date] => 2022-03-22 12:35:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[version:DLM_Download_Version:private] => 1
[download_count:DLM_Download_Version:private] => 0
[filesize:DLM_Download_Version:private] => 2648776
[md5:DLM_Download_Version:private] =>
[sha1:DLM_Download_Version:private] =>
[sha256:DLM_Download_Version:private] =>
[crc32b:DLM_Download_Version:private] =>
[mirrors:DLM_Download_Version:private] => Array
(
[0] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-WEEKLY-SENTIMENT-REPORT-3-8-22.pdf
)
[url:DLM_Download_Version:private] => https://wordpress-360386-2306631.cloudwaysapps.com/wp-content/uploads/dlm_uploads/2022/03/STS-WEEKLY-SENTIMENT-REPORT-3-8-22.pdf
[filename:DLM_Download_Version:private] => STS-WEEKLY-SENTIMENT-REPORT-3-8-22.pdf
[filetype:DLM_Download_Version:private] => pdf
)
)
How can I get just the values of id:DLM_Download_Version:private?
I have tried a foreach loop:
foreach ($versions as $version){
echo $version->{'id:DLM_Download_Version:private'}
}
which just outputs nothing.
I have an object $myObject; I'm trying to return php object as json, but it loses some data. This is how $myObject looks:
CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList Object
(
[priceListId:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 32
[amounts:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[0] => 1000
[1] => 2000
[2] => 3000
[3] => 4000
[4] => 5000
[5] => 6000
[6] => 7000
)
[amountsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[1000] => 0
[2000] => 1
[3000] => 2
[4000] => 3
[5000] => 4
[6000] => 5
[7000] => 6
)
[periods:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
[4] => 30
)
[periodsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[10] => 0
[15] => 1
[20] => 2
[25] => 3
[30] => 4
)
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 7000
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 30
[prices:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
(
[30] => Array
(
[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
(
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
[charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
[interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
[annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
[schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] =>
)
)
)
[settings:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings Object
(
[mode:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => credits
[preset:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] =>
[implementation:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => matrix
[defaults:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => Array
(
[period] =>
[amount] =>
)
)
)
After json_encode($myObject); new data looks like this (screenshot for better json view):
screenshot of returned json
Why information I wrote below is missing and how to access it?
Missing stuff:
[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
(
[period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
[amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
[charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
[interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
[annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
[annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
[schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] =>
)
The problem is json_encode can not access private properties.
Here are some workarounds you could do:
Convert the properties to be public
You could use a reflection class to convert the properties accessibility before you convert it
Example:
$refObject = new ReflectionObject( $obj );
$refProperty = $refObject->getProperty( 'property' );
$refProperty->setAccessible( true );
You could loop through the properties of the object using a Reflection object and call all the getters converting the object into an array structure that could be easily translated
Refelection Class Documentation
I got the following response output from curl request.
Array
(
[code] => 200
[message] => Message history obtained successfully.
[data] => Array
(
[0] => Array
(
[id] => 3390257
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784285
[ext_message_id] => 152127f68b2000031bf6584fa8494a7b
[send_date] => 2016-01-05 09:54:17
[dest_address] => 8476099160
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[1] => Array
(
[id] => 3390261
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784289
[ext_message_id] => 152127f6c36000171df5874efba398b4
[send_date] => 2016-01-05 09:54:17
[dest_address] => 6188892860
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[2] => Array
(
[id] => 3390265
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784293
[ext_message_id] => 152127f65dd000031bf6584fa84949af
[send_date] => 2016-01-05 09:54:17
[dest_address] => 2032415751
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[3] => Array
(
[id] => 3390270
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784298
[ext_message_id] => 152127f6a2a0001a8d8e4ef0eebbbe5d
[send_date] => 2016-01-05 09:54:17
[dest_address] => 3106193605
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
)
)
Now I want to get the array value like $keyword_id = 1459 But I can't access the array.
Assuming this response is stored in a variable called $response, here are some examples of accessing array elements...
$response['data'][0]['id'] // 3390257 (id of 1st data element)
$response['data'][1]['dest_address'] // 6188892860 (dest_address of 2nd data element)
$response['data'][3]['keyword_id'] // 1459 (keyword_id of 4th data element)
You could also iterate over the data elements and print the ids as follows...
foreach ($response['data'] as $i => $element) {
echo "The id of element " . $i . " is " . $element['id'] . "\n";
}
you can get keyword_id through foreach loop. run the foreach loop on your array and get data through keyword_id index.
foreach($array as $value){
$keyword_id = $value['keyword_id'];
}
this will give you keyword_id.
I am fairly new to PHP and I am writing a PHP function that grabs an object from SOAP.
I found a code to convert it to an array but I can't manage to echo any data.
The array from print_r
Array
(
[Status] => Array
(
[Code] => 0
[Message] => OK
)
[Order] => Array
(
[OrderNumber] => 9334543
[ExternalOrderNumber] =>
[OrderTime] => 2014-07-15T15:20:31+02:00
[PaymentMethod] => invoice
[PaymentStatus] => Paid
[ShipmentMethod] => Mypack
[DeliveryStatus] => Delivered
[Language] => sv
[Customer] => Array
(
[CustomerId] => 13556
[CustomerNumber] =>
[Username] => admin
[Approved] => 1
[OrgNumber] => 9309138445
[Company] =>
[VatNumber] =>
[FirstName] => Jane
[LastName] => Doe
[Address] => Gatan
[Address2] =>
[Zip] => 1230
[City] => Staden
[Country] => Sweden
[CountryCode] => SE
[PhoneDay] => 84848474
[PhoneNight] =>
[PhoneMobile] =>
[Email] => mail#msn.com
[NewsLetter] =>
[OrgType] => person
[OtherDelivAddress] =>
[DelivName] =>
[DelivAddress] =>
[DelivAddress2] =>
[DelivZip] =>
[DelivCity] =>
[DelivCountry] =>
[DelivCountryCode] =>
)
[Comment] =>
[Notes] => 9063025471 UK/MA
[CurrencyCode] => SEK
[ExchangeRate] => 1
[LanguagePath] => se
[FreightWithoutVat] => 0
[FreightWithVat] => 0
[FreightVatPercentage] => 25
[PayoptionFeeWithoutVat] => 0
[PayoptionFeeWithVat] => 0
[PayoptionFeeVatPercentage] => 25
[CodWithoutVat] => 0
[CodWithVat] => 0
[CodVatPercentage] => 0
[DiscountWithoutVat] => 0
[DiscountWithVat] => 0
[DiscountVat] => 0
[TotalWithoutVat] => 4388
[TotalWithVat] => 5485
[TotalVat] => 1097
[PayWithoutVat] =>
[AffiliateCode] =>
[AffiliateName] =>
[OrderField] => Array
(
[0] => Array
(
[Name] => external_ref
[Value] => 43445
)
[1] => Array
(
[Name] => webshopid
[Value] => 423
)
[2] => Array
(
[Name] => webshopname
[Value] => Manuell
)
)
)
)
Non working code
echo $array[1][0]
I have tried different combos of indexes. I know how to return the values from the soap object but if I could do it this way it would be easier. It should work shouldn't it?
$array[1] is the second index of the array. the key of this array us "Status", this array contains a code and message
i assume you want to echo the message, you can do that with the following
echo $array[1]["Status"]["Message"];
You should use $array['Status']['Code'] , $array['Status']['Message'], $array['Order']['OrderNumber'], $array['Order']['Customer']['CustomerId'] and so on to display your data. It's an associative array so you need to use string keys and not numbers
try
$array['Order']['Customer']['LastName']
is my best guess without losing my sanity in that one line.
But for us to be sure please post the print_r($array) output
There are some way I always do this:
print_r($array);
And the other way is
$array[0]['Order']['LastName']
Try to access the arrays elements with the string keys, not the integer ones you are using:
echo $array['Order']['Customer']['Address'];
Another way you could see what is going on is by iterating through the array, and print out the keys and values:
foreach ($array as $key => $value)
echo "Key=$key value=$value<br>";
I'm trying to use the stackoverflow API and I want to get answers of a question in a php array. So far here is my php code:
<?php
//KEY
$string = "key=my_key";
//Call stack API .$string
$stack_url = "compress.zlib://http://api.stackoverflow.com/1.1/questions";
//Get and Store API results into a variable
$result = file_get_contents($stack_url);
$jsonArray = json_decode($result);
print_r($jsonArray);
//echo($jsonArray->questions[0]->question_answers_url);
//var_dump($jsonArray);
?>
I want to store the answers of a question in an array called answers so that I can access them with a for loop.
The answer i get is :
stdClass Object
(
[total] => 2618591
[page] => 1
[pagesize] => 30
[questions] => Array
(
[0] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => ssh
[2] => openssh
[3] => rsacryptoserviceprovider
)
[answer_count] => 1
[favorite_count] => 0
[question_timeline_url] => /questions/9164203/timeline
[question_comments_url] => /questions/9164203/comments
[question_answers_url] => /questions/9164203/answers
[question_id] => 9164203
[owner] => stdClass Object
(
[user_id] => 311966
[user_type] => registered
[display_name] => simonc
[reputation] => 301
[email_hash] => 021f3344004f0c886d715314fa02037d
)
[creation_date] => 1328548627
[last_edit_date] => 1328611688
[last_activity_date] => 1328611688
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 25
[score] => 0
[community_owned] =>
[title] => Format of PKCS private keys
)
[1] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => .net
[2] => combobox
)
[answer_count] => 3
[favorite_count] => 0
[question_timeline_url] => /questions/9174765/timeline
[question_comments_url] => /questions/9174765/comments
[question_answers_url] => /questions/9174765/answers
[question_id] => 9174765
[owner] => stdClass Object
(
[user_id] => 1194399
[user_type] => registered
[display_name] => Goxy
[reputation] => 1
[email_hash] => 5fc8c96b6b85c6339cb9ac4ab60cb247
)
[creation_date] => 1328611202
[last_activity_date] => 1328611686
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 15
[score] => 0
[community_owned] =>
[title] => WPF: Bind simple List<myClass> to Combobox
)
....
Not sure exactly which property you want to extract, but I assume it's the 'question_answers_url'.
$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
//assuming it is the 'question_answers_url' property that you want
array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}
Ought to do it.