how to Array inside Array Data in laravel - php

I am trying to get array value in BLADE file. My array is shown below but I am not able to get value inside array.
I have get [student_levels]=> [level] Data
laravel Code
foreach($items as $item)
{
echo $item['student_levels']['level'];
}
$items is Array
Array
(
[0] => Array
(
[_id] => 5c1e4295b0dace71ec62e325
[email] => dhavalpansuriya1195#gmail.com
[franchisecode] => MELHH
[title] => Mr
[firstname] => dhaval s
[lastname] => patel
[gender] => Male
[age] => 16-18
[phone] => 9601354880
[address] => rajkot
[city] => Ahmedabad
[country] => India
[tempcode] => hUmoEF
[studentid] => HTNKLF
[updated_at] => 2019-11-21 13:32:31
[created_at] => 2018-12-22 19:26:37
[student_status] => eda
[student_levels] => Array
(
[_id] => 5c8b11d2b0dace2f9a044d86
[student_id] => HTNKLF
[teacher_id] => 5c1e1ab5b0dace716e429654
[level_name] => recomended_level
[level] => Level 1
[comments] => hhbhhhh
[exam_type] => eda
[student_course] =>
[updated_at] => 2019-03-15 08:15:38
[created_at] => 2019-03-15 08:15:38
)
)

You have an array inside an array..so you can use foreach loop as
#foreach($items as $item)
{{$item['student_levels']['level']}}
#endforeach
Or directly
{{$items[0]['student_levels']['level']}}

It seems your array is returning an array of students, you have an extra level in your array. So to fetch the first student level, you need to use the following code:
echo $item[0]['student_levels']['level'];
Maybe the code that fetches this data could be optimized to only fetch a single student, could you post this code?

Related

Loop multidimensional array from mysql database using php

I can't for the life me return single values of my multidimensional array from database. For example, I want to return the value of each key to return in table columns.
This is the array coming from the database:
Array
(
[0] => stdClass Object
(
[student_id] => lvzr0001
[fname] => Hamza
[lname] => ibrahimi
[student_email] => Hamza_ib2#hotmail.com
[course] => CPAN 220 D30
[full_name] => asdfddd
[institution] => asdf
[position] => sadf
[proc_email] => Hamza_ib2#hotmail.com
[phone] => 14168334599
[address] => 20 edgecliffe golfway
[city] => Toronto
[prov_state] => Ont
[postal_code] => M3C 3A4
[country] => Canada
[comments] => adfadsfasdfasdfas
)
[1] => stdClass Object
(
[student_id] => 56gfdsgdfs
[fname] => zxcv
[lname] => cvcvz
[student_email] => dsfb2#hotmail.com
[course] => ELIC 101 90
[full_name] => dfa
[institution] => asdf
[position] => zxcvzxcv
[proc_email] => zxcvzxvc#hotmail.com
[phone] => 4002452345
[address] => 102 yorkland st
[city] => Richmond Hill
[prov_state] => Ont
[postal_code] => l4s1a1
[country] => Canada
[comments] => xzcvzxcv
)
)
I want to get a specific column result like this:
<?php
global $wpdb;
$displayQuery = $wpdb->get_results("SELECT * FROM viewoffcampusproctorrequests");
echo '<pre>';
print_r ($displayQuery);
echo '</pre>';
foreach($displayQuery as $key => $value){
foreach($value as $title => $description){
echo $description['fname'];
}
}
But I only get the entire key values when I echo $description;. How would I access each key item in my array?
You're close: $value is an object, so you can retrieve a particular key/value pair. If you want the value for fname for each object:
foreach($displayQuery as $key => $value){
echo $value->fname;
}

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

how to get individual values from this array

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

how to display individual values?

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

Categories