How to loop collection object in laravel? - php

I am trying to foreach loop in laravel.Here is my print_r result of jobseekers.
Illuminate\Support\Collection Object (
[items:protected] => Array (
[0] => stdClass Object ( [myid] => 37 [max(call_back_date)] => 2018-12-12 )
[1] => stdClass Object ( [myid] => 60 [max(call_back_date)] => 2017-12-12 )
[2] => stdClass Object ( [myid] => 61 [max(call_back_date)] => 2017-11-30 )
[3] => stdClass Object ( [myid] => 73 [max(call_back_date)] => 2017-11-29 )
[4] => stdClass Object ( [myid] => 62 [max(call_back_date)] => 2017-11-28 )
[5] => stdClass Object ( [myid] => 63 [max(call_back_date)] => 2017-11-22 )
[6] => stdClass Object ( [myid] => 64 [max(call_back_date)] => 2017-11-15 )
[7] => stdClass Object ( [myid] => 66 [max(call_back_date)] => 2017-11-15 )
[8] => stdClass Object ( [myid] => 65 [max(call_back_date)] => 2017-11-10 )
[9] => stdClass Object ( [myid] => 54 [max(call_back_date)] => 2017-10-24 )
[10] => stdClass Object ( [myid] => 53 [max(call_back_date)] => 2017-09-30 )
[11] => stdClass Object ( [myid] => 48 [max(call_back_date)] => 2017-09-20 )
[12] => stdClass Object ( [myid] => 46 [max(call_back_date)] => 2017-08-26 )
[13] => stdClass Object ( [myid] => 58 [max(call_back_date)] => 2017-12-12 )
[14] => stdClass Object ( [myid] => 70 [max(call_back_date)] => 2017-11-29 )
[15] => stdClass Object ( [myid] => 47 [max(call_back_date)] => 2017-08-20 )
[16] => stdClass Object ( [myid] => 44 [max(call_back_date)] => 2017-08-18 )
[17] => stdClass Object ( [myid] => 72 [max(call_back_date)] => 2018-02-17 )
[18] => stdClass Object ( [myid] => 55 [max(call_back_date)] => 2000-12-12 )
[19] => stdClass Object ( [myid] => 49 [max(call_back_date)] => )
[20] => stdClass Object ( [myid] => 50 [max(call_back_date)] => )
[21] => stdClass Object ( [myid] => 74 [max(call_back_date)] => )
[22] => stdClass Object ( [myid] => 32 [max(call_back_date)] => )
)
)
and i tried like this
foreach ($jobseekers as $value) {
echo $value->myid;
}
it shows me only the first item. like "37", i expect many items. like "37,60,61 etc...".

i think you need to do more one loop to iterate the data.
update
try one of this
foreach($jobseekers ->items as value){}
or this
foreach ($jobseekers as $value) {
foreach ($value as $item) {}
}

Related

Replace the key in the array in php codeigniter

I have a string which i am spitting using explode functionality. Code Snippet is shown below
$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
$tags = (explode("/",$string));
print_r($tags);
foreach ($tags as $key)
$invoicedata[] = (object)$key;
echo '<pre>'; print_r($invoicedata); exit;
The Output what i am getting right now is
Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
Array
(
[0] => stdClass Object
(
[scalar] => 2017167637
)
[1] => stdClass Object
(
[scalar] => 43
)
[2] => stdClass Object
(
[scalar] => 42
)
[3] => stdClass Object
(
[scalar] => 38
)
[4] => stdClass Object
(
[scalar] => 46
)
[5] => stdClass Object
(
[scalar] => 41
)
[6] => stdClass Object
(
[scalar] => 40
)
[7] => stdClass Object
(
[scalar] => 39
)
But the actual output what i need is in place of [scalar] i need [invoice]. How to do it? Any help appreciated.
Actual output what i need is shown below
Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
Array
(
[0] => stdClass Object
(
[invoice] => 2017167637
)
[1] => stdClass Object
(
[invoice] => 43
)
[2] => stdClass Object
(
[invoice] => 42
)
[3] => stdClass Object
(
[invoice] => 38
)
[4] => stdClass Object
(
[invoice] => 46
)
[5] => stdClass Object
(
[invoice] => 41
)
[6] => stdClass Object
(
[invoice] => 40
)
[7] => stdClass Object
(
[invoice] => 39
)
)
I tried with adding $invoice['invoice'] but it did not worked so how to get the desired output any help appreciated.
Here is working code. Try this.
$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
$tags = (explode("/",$string));
print_r($tags);
foreach ($tags as $key=>$value)
{
$invoicedata[$key] = new stdClass();
$invoicedata[$key]->invoice = $value;
}
echo '<pre>'; print_r($invoicedata); exit;

How to echo string value from an multi dimmensional array

How can i echo each hotelCode, totalPrice, boardType and roomCategory from the below multidimmensional array
<?php
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
if (!isset($hotelCodes[$hotel->hotelCode])) {
$hotelCodes[$hotel->hotelCode] = array();
}
// you could ofcourse just take specific info from $hotel and put it in here.
$hotelCodes[$hotel->hotelCode][] = $hotel;
}
?>
and
print_r of $hotelCodes is
Array ( [HOTELCODE1] => Array ( [0] => stdClass Object ( [processId] => HV-82768336 [hotelCode] => ITULJP [availabilityStatus] => InstantConfirmation [totalPrice] => 279 [totalTax] => 0 [totalSalePrice] => 298.86 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Twin Room - Non-Refundable-1 double bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 279 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 34.84 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 34.88 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 34.88 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 34.88 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 34.88 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 34.88 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 34.88 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 34.88 ) ) ) ) ) [1] => stdClass Object ( [processId] => HN-10616086 [hotelCode] => ITULJP [availabilityStatus] => InstantConfirmation [totalPrice] => 294 [totalTax] => 0 [totalSalePrice] => 314.59 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Double or Twin Room-1 double bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 294 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 36.75 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 36.75 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 36.75 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 36.75 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 36.75 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 36.75 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 36.75 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 36.75 ) ) ) ) ) ) [HOTELCODE2] => Array ( [0] => stdClass Object ( [processId] => HZ-21733446 [hotelCode] => ITOI6P [availabilityStatus] => InstantConfirmation [totalPrice] => 295 [totalTax] => 0 [totalSalePrice] => 0 [currency] => EUR [boardType] => Room Only [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Double Or Twin Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 295 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 37 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 37 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 37 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 37 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 37 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 37 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 37 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 36 ) ) ) ) ) ) )
I need to echo the values
HOTELCODE1 and HOTELCODE1 as $hotelcode[1] and $hotelcode[2]
and totalRoomRate, roomCategory and boardType for each hotelcode
Thanks
You're already there, you just need to make sure only one hotel is stored for any hotel-code.
foreach($availHotels as $hotel){
// if we do not have a hotel with this code yet
if (!isset($hotelCodes[$hotel->hotelCode])) {
// store this one there
$hotelCodes[$hotel->hotelCode] = $hotel;
}
}
// run over the available hotels
foreach( $hotelCodes as $hotel ) {
echo $hotel['hotelCode'] . ' -> ' . $hotel['totalPrice' ] . ' -> etc ';
}

Split multidimensional array from SOAP response

I hae vthe below code
<?php
if (is_object($checkAvailability->availableHotels))
$hotelResponse[] = $checkAvailability->availableHotels;
$hotelResponse = $checkAvailability->availableHotels;
foreach ((array)$hotelResponse as $hnum => $hotel)
?>
$hotelResponse is a Multi-dimensional array
print_r($checkAvailability->availableHotels); is generating :
Array ( [0] => stdClass Object ( [processId] => H0-41925041 [hotelCode] => ITJHRV [availabilityStatus] => InstantConfirmation [totalPrice] => 1421 [totalTax] => 0 [totalSalePrice] => 1509.38 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Classic Double or Twin Room-1 queen bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 1421 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-04-20 [amount] => 203 ) [1] => stdClass Object ( [date] => 2015-04-21 [amount] => 203 ) [2] => stdClass Object ( [date] => 2015-04-22 [amount] => 203 ) [3] => stdClass Object ( [date] => 2015-04-23 [amount] => 203 ) [4] => stdClass Object ( [date] => 2015-04-24 [amount] => 203 ) [5] => stdClass Object ( [date] => 2015-04-25 [amount] => 203 ) [6] => stdClass Object ( [date] => 2015-04-26 [amount] => 203 ) ) ) ) ) [1] => stdClass Object ( [processId] => HA-51032431 [hotelCode] => ITRR5G [availabilityStatus] => InstantConfirmation [totalPrice] => 1590 [totalTax] => 0 [totalSalePrice] => 0 [currency] => EUR [boardType] => Bed & Breakfast [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Twin Room (Including Breakfast and Wi-Fi) [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 1590 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-04-20 [amount] => 197 ) [1] => stdClass Object ( [date] => 2015-04-21 [amount] => 197 ) [2] => stdClass Object ( [date] => 2015-04-22 [amount] => 239 ) [3] => stdClass Object ( [date] => 2015-04-23 [amount] => 239 ) [4] => stdClass Object ( [date] => 2015-04-24 [amount] => 239 ) [5] => stdClass Object ( [date] => 2015-04-25 [amount] => 239 ) [6] => stdClass Object ( [date] => 2015-04-26 [amount] => 240 ) ) ) ) ) )
How can i do that get every response by echoing them as below
<?php echo $hotel->hotelCode?>
<?php echo $hotel->totalPrice?>
As i understand i need to convert the 2d array into a object but from there i have no clue. Please help.
Try this:
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
$hotelCodes[] = $hotel->hotelCode ;
//echo $hotel->hotelCode ;
//echo $hotel->totalPrice ;
}
Now you have $hotelCodes array and access it like $hotelCodes[0], $hotelCode[1] and so more.

Issue in looping stdClass Object in Php

I am facing strange issue in looping json object using php. Here is the format of my data
stdClass Object
(
[userform] => Array
(
[0] => stdClass Object
(
[id] => 69
[product] => testuser
)
[1] => stdClass Object
(
[id] => 70
[product] => testuser
)
[2] => stdClass Object
(
[id] => 71
[product] => testuser
)
[3] => stdClass Object
(
[id] => 72
[product] => testuser
)
[4] => stdClass Object
(
[id] => 73
[product] => testuser
)
[5] => stdClass Object
(
[id] => 74
[product] => testuser
)
[6] => stdClass Object
(
[id] => 75
[product] => testuser
)
[7] => stdClass Object
(
[id] => 76
[product] => testuser
)
[8] => stdClass Object
(
[id] => 77
[product] => testuser
)
[9] => stdClass Object
(
[id] => 78
[product] => testuser
)
[10] => stdClass Object
(
[id] => 79
[product] => testuser
)
[11] => stdClass Object
(
[id] => 80
[product] => testuser
)
[12] => stdClass Object
(
[id] => 81
[product] => testuser
)
[13] => stdClass Object
(
[id] => 82
[product] => testuser
)
[14] => stdClass Object
(
[id] => 83
[product] => testuser
)
[15] => stdClass Object
(
[id] => 84
[product] => testuser
)
[16] => stdClass Object
(
[id] => 85
[product] => testuser
)
[17] => stdClass Object
(
[id] => 86
[product] => testuser
)
[18] => stdClass Object
(
[id] => 87
[product] => testuser
)
[19] => stdClass Object
(
[id] => 88
[product] => testuser
)
[20] => stdClass Object
(
[id] => 89
[product] => testuser
)
)
)
But this loop throws following error.
Trying to get property of non-object in
Please let me know how to read the product property value from the above format
Thanks all
That foreach should be on $data->userform , not just $data
foreach($data->userform as $key=>$row){
if(isset($row->product)){
print_r($key);
print_r($row->product);
}else{
//product property is not set
}
}

stdClass Object foreach PHP

Been searching for a solution for my problem. Seams meny has the same q as me, but still haven't got a solution for my problem.
I have a stdClass Object that needs to be printed out in a foreach or somlike like that.
Here is a cut of the result i get with a "print_r($result)".
stdClass Object
(
[ServiceGroup] => Array
(
[0] => stdClass Object
(
[Service] => Array
(
[0] => stdClass Object
(
[_] => 3D-modulering
[count] => 71
)
[1] => stdClass Object
(
[_] => CAD
[count] => 81
)
[2] => stdClass Object
(
[_] => Databasutveckling
[count] => 118
)
[3] => stdClass Object
(
[_] => Datainmatare
[count] => 6
)
[4] => stdClass Object
(
[_] => Driftsteknik
[count] => 87
)
[5] => stdClass Object
(
[_] => IT-konsult
[count] => 39
)
[6] => stdClass Object
(
[_] => IT-tekniker
[count] => 223
)
[7] => stdClass Object
(
[_] => Mjuk- och hårdvarutestning
[count] => 150
)
[8] => stdClass Object
(
[_] => Nätverksteknik
[count] => 142
)
[9] => stdClass Object
(
[_] => Produkttestare
[count] => 171
)
[10] => stdClass Object
(
[_] => Programmerare
[count] => 146
)
[11] => stdClass Object
(
[_] => Projektledning
[count] => 156
)
[12] => stdClass Object
(
[_] => Serviceteknik
[count] => 157
)
[13] => stdClass Object
(
[_] => Support
[count] => 360
)
[14] => stdClass Object
(
[_] => Systemadministration
[count] => 145
)
[15] => stdClass Object
(
[_] => Systemutveckling
[count] => 110
)
[16] => stdClass Object
(
[_] => Webbdesign
[count] => 246
)
[17] => stdClass Object
(
[_] => Webbutveckling
[count] => 135
)
[18] => stdClass Object
(
[_] => webmaster
[count] => 103
)
)
[name] => Data/IT
)
[1] => stdClass Object
(
[Service] => Array
(
[0] => stdClass Object
(
[_] => Affärsanalys
[count] => 192
)
[1] => stdClass Object
(
[_] => Aktuarie
[count] => 20
)
I don't know exactly what you want, but can easily foreach through objects just like arrays. This example shows that.
foreach($result->ServiceGroup as $value){
foreach($value->Service as $obj){
echo $obj->_;
echo $obj->count;
}
echo $value->name;
}

Categories