Loop multidimensional array from mysql database using php - 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;
}

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.

how to Array inside Array Data in laravel

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?

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