how to display individual values? - php

I get this from print_r($data):
Array (
[0] => Array (
[0] => stdClass Object (
[cto_office_id] => 1
[office_name] => Airtel India
[address_line1] => Bendoor well
[phone_office1] => 12345678912
[phone_office2] => 789451234512
[address_line2] => Kankanady Circle
[city] => Mangluru
[state] => Haryana
[pin_code] => 002
[country] => India
[web_url] => airte.co.in
[office_quote] => Office
[date_registered] => 2011-11-24 05:59:51
[bill_mail_id] => 15612561
[bill_mail_id_type] =>
[acc_status] => enabled
[comments] =>
[is_account_deleted] => 0
[account_deleted_date] => 0000-00-00 00:00:00
)
)
)
I tried this $data['phone_office1'], but that does not work.

You have an object that is nested inside two arrays. You can get it out like this:
$data[0][0]->phone_office1

Related

How to display data in this format from multiple associative array php

I'm trying to find out in a simple way to display a clean like this format
Having a hard time to spread the data in this associative array got this from an API
Array (
[wind] => Array (
[speed] => 5
[direction] => North West
[directionDegrees] => 310
[unit] => KT
)
[visibility] => Array (
[mainVisibility] => 10SM
)
[clouds] => Array (
[0] => Array (
[height] => 25000
[quantity] => few
)
)
[cavok] =>
[remark] => automated station with a precipitation discriminator sea level pressure of 1021.7 HPa hourly temperature of 17.2°C and dew point of -1.1°C
[day] => 6
[time] => 22:52:00
[airport] => Array (
[name] => Hartsfield Jackson Atlanta International Airport
[city] => Atlanta
[country] => Array (
[name] => United States
)
[iata] => ATL
[icao] => KATL
[latitude] => 33.6367
[longitude] => -84.428101
[altitude] => 1026
[timezone] => -5
[dst] => A
)
[message] => KATL 062252Z 31005KT 10SM FEW250 17/M01 A3017 RMK AO2 SLP217 T01721011
[station] => KATL
[temperature] => 17
[dewPoint] => -1
[altimeter] => 1021
[nosig] =>
[auto] =>
[amendment] =>
[nil] =>
[corrected] =>
[cancelled] =>
)
I'm not good at programming as this is our assignment from our school I'm looking at to loop the array but I can't seem to find a way how to do it.
You can use this parser source code and make your own solution on the base
https://github.com/SafranCassiopee/php-metar-decoder
or simple way to get field
$arr = '$yourDataArrayHere$';
$metarRw = $arr['message']
echo 'Metar message '.$metarRw;
$airportName = $arr['airport']['name']
echo 'Airport name '.$airportName;
etc.

Blocktrail payments API, How to get the values that adress receive

I'am tryng to get the value that an adress sent me on my adress, I have already setted up the webhook, but i can't understand a thing...
I have this array->
Array
(
[network] => tBTC
[event_type] => address-transactions
[addresses] => Array
(
[2MyXzRYSzrExLitY6FumGC3QgzCeqVoeQC1] => -49320
)
[data] => Array
(
[raw] =>
[hash] =>
[first_seen_at] => 2017-10-13T17:19:35+0000
[last_seen_at] => 2017-10-13T17:19:35+0000
[block_height] => 1210150
[block_time] => 2017-10-13T17:20:18+0000
[block_hash] =>
[confirmations] => 2
[is_coinbase] =>
[estimated_value] => 30000
[total_input_value] => 49320
[total_output_value] => 45510
[total_fee] => 3810
[estimated_change] => 15510
[estimated_change_address] => 2N7iSxafSoio4RE8wrd6outJNmbMkyP9QuL
[high_priority] =>
[enough_fee] =>
[contains_dust] =>
[inputs] => Array
(
[0] => Array
(
[index] => 0
[output_hash] =>
[output_index] => 0
[output_confirmed] => 1
[value] => 49320
[sequence] => 4294967295
[address] => 2MyXzRYSzrExLitY6FumGC3QgzCeqVoeQC1
[type] => scripthash
[multisig] =>
[multisig_addresses] =>
[script_signature] =>
)
)
[outputs] => Array
(
[0] => Array
(
[index] => 0
[value] => 30000
[address] => 2NBkQ8aTq8yeXdMWtxXNuxNgEHYnrzvcpV2
[type] => scripthash
[multisig] =>
[multisig_addresses] =>
[script] => OP_HASH160 OP_EQUAL
[script_hex] =>
[spent_hash] =>
[spent_index] => 0
)
[1] => Array
(
[index] => 1
[value] => 15510
[address] => 2N7iSxafSoio4RE8wrd6outJNmbMkyP9QuL
[type] => scripthash
[multisig] =>
[multisig_addresses] =>
[script] => OP_HASH160 OP_EQUAL
[script_hex] =>
[spent_hash] =>
[spent_index] => 0
)
)
[opt_in_rbf] =>
[unconfirmed_inputs] =>
[lock_time_timestamp] =>
[lock_time_block_height] =>
[size] => 367
)
[retry_count] => 0
And I'am tryng to get ONLY THE VALUE OF BTC (SATOSHI) THAT COME ON MY ADRESS, SO WITH THE SUBSTRACTION OF THE FEES, i thought that for get this values I had to get the
$Data=BlocktrailSDK::getWebhookPayload();
$Data['data']['estimated_value'];
Is this correct way to get at the 1000000% THE VALUE OF BTC (SATOSHI) THAT COME ON MY ADRESS, SO WITH THE SUBSTRACTION OF THE FEES, if is not what is the correct way??Anyway thanks for ready, hope that you all had a good day, and thanks for your time :)
P.S Note that on this transaction i sent 30000 satoshi+fees!

How to split array data returned from json string in php

I have json return string like given below. I want to extract cancellation Policy list of objects like cutoff Time and refund In Percentage. I tried using for-loop but it didn't help me. Can you please help me on extracting this.
Array (
[apiStatus] => Array ( [success] => 1 [message] => SUCCESS ) <br>
[apiAvailableBuses] => Array ( <br>
[0] => Array ( [droppingPoints] => [availableSeats] => 41 <br>[partialCancellationAllowed] => [arrivalTime] => 08:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:00PM [location] => Ameerpet,|Jeans Corner 9687452130 [id] => 6 ) [1] => Array ( [time] => 09:15PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 09:10PM [location] => Kondapur,Toyota Show room [id] => 2244 ) ) [operatorName] => Deepak Travels [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6622 [fare] => 800 [busType] => 2+1 Hi-Tech Non A/c [routeScheduleId] => 6622 [commPCT] => 0 [operatorId] => 213 [inventoryType] => 0 ) <br>
[1] => Array ( [droppingPoints] => [availableSeats] => 41 [partialCancellationAllowed] => [arrivalTime] => 07:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:10PM [location] => Ameerpet,|Jeans Corner [id] => 6 ) [1] => Array ( [time] => 09:00PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 08:30PM [location] => KUKATPALLY,JNTU [id] => 2230 ) ) [operatorName] => Dhanunjayabus [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6743 [fare] => 900 [busType] => VOLVO [routeScheduleId] => 6743 [commPCT] => 0 [operatorId] => 233 [inventoryType] => 0 )
)
)
Use a foreach() for it like so:
foreach ($your_response['apiAvailableBuses'] as $el) {
$cancellationPolicy[] = $el['cancellationPolicy'];
}
Try this:
foreach($data['apiStatus']['apiAvailableBuses'] as $item) {
foreach($item['cancellationPolicy'] as $key => $json) {
$jsonDecoded = json_decode($json, true);
// And you will have access to the data like this
// $jsonDecoded['cutoffTime'];
// $jsonDecoded['refundInPercentage'];
}
}
$response = json_decode($apiResponse);
$cancellationPolicies = [];
foreach ($response->apiAvailableBuses as $availableBus) {
$cancellationPolicies[] = $availableBus['cancellationPolicy'];
// if you want to display something you can simply do it like this;
echo $availableBus['cancellationPolicy']->cutoffTime;
}

Extract data from Array/Object in PHP?

First off, I'm new to PHP and coding in general, so this might be quite an obvious answer.
I'm currently working with the Strava API, and I'm trying to extract data from an Array/Object which is the result of the following API call:
$recentactivities = $api->get('athlete/activities', array('per_page' => 100));
which returns:
Array (
[1] => stdClass Object (
[id] => XXXX
[resource_state] => 2
[external_id] => XXXX
[upload_id] => XXXX
[athlete] => stdClass Object (
[id] => XXXX
[resource_state] => 1
)
[name] => Let\'s see if I can remember how to do this cycling malarkey...
[distance] => 11858.3
[moving_time] => 1812
[elapsed_time] => 2220
[total_elevation_gain] => 44
[type] => Ride
[start_date] => 2014-07-12T13:48:17Z
[start_date_local] => 2014-07-12T14:48:17Z
[timezone] => (
GMT+00:00
) Europe/London
[start_latlng] => Array (
[0] => XXXX
[1] => XXXX
)
[end_latlng] => Array (
[0] => XXXX
[1] => -XXXX
)
[location_city] => XXXX
[location_state] => England
[location_country] => United Kingdom
[start_latitude] => XXXX
[start_longitude] => XXXXX
[achievement_count] => 4
[kudos_count] => 1
[comment_count] => 0
[athlete_count] => 1
[photo_count] => 0
[map] => stdClass Object (
[id] => a164894160
[summary_polyline] => XXXX
[resource_state] => 2
)
[trainer] =>
[commute] =>
[manual] =>
[private] =>
[flagged] =>
[gear_id] => b739244
[average_speed] => 6.544
[max_speed] => 10.8
[average_cadence] => 55.2
[average_temp] => 29
[average_watts] => 99.3
[kilojoules] => 179.9
[device_watts] =>
[average_heartrate] => 191.2
[max_heartrate] => 200
[truncated] =>
[has_kudoed] =>
)
This repeats for the most recent activities.
I'm attempting to extract average_heartrate, which I can do for a single object using the following:
$recentactivities[1]->average_heartrate;
but I'd like to extract all instances of average_heartrate from the Array. I've tried to use a foreach statement, but to be honest, I have no idea where to start.
Any help would be much appreciated.
It's actually pretty simple you can indeed use a foreach loop:
foreach($myArray as $obj){
//$obj is an object so:
if(isset($obj->average_heartrate)){
echo $obj->average_heartrate;
}
}
With this code you iterate through your array with objects and save the wanted array within an array so you can work further with it.
$heartrateArray = array(); // create a new array
//iterate through it with an foreach
foreach($recentactivities as $activity){
// save the average_heartrate as a value under a new key in the array
$heartrateArray[] = $activity->average_heartrate;
}

How to echo an array value

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>";

Categories