php loop through array of objects and assign variable - php

I'm trying to print out the value of sessionkey and assign to variable, but I'm confused on how I can go about doing that in php.
stdClass Object
(
[timeout] => 1800
[lastname] => cloud
[registered] => false
[username] => admin
[firstname] => admin
[domainid] => d323388a-ab41-11e4-91c4-464e571bcea9
[userid] => ff5705c6-ab41-11e4-91c4-464e571bcea9
[type] => 1
[sessionkey] => DPguBrSBFB7hcdNZAgAyMJIPm4c=
[account] => admin
)
output for var_dump
object(stdClass)#5 (10) { ["timeout"]=> string(4) "1800" ["lastname"]=> string(5) "cloud" ["registered"]=> string(5) "false" ["username"]=> string(5) "admin" ["firstname"]=> string(5) "admin" ["domainid"]=> string(36) "d323388a-ab41-11e4-91c4-464e571bcea9" ["userid"]=> string(36) "ff5705c6-ab41-11e4-91c4-464e571bcea9" ["type"]=> string(1) "1" ["sessionkey"]=> string(28) "vzaQz+uVdL/NQQE7eRSimP6FIf4=" ["account"]=> string(5) "admin" }
This is what I have tried, but its not working.
foreach ($vms as $key => $obj) {
foreach (get_object_vars($obj) as $name => $value) {
echo "Object mapped to $key has property $name => $value";
}
}
but I keep seeing this in the logs.
PHP Notice: Trying to get property of non-object in
your help is highly appreciated.

Related

Get Unique from Multi-dimentional array based on one key - PHP [duplicate]

This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
Referring to this question & this, i have a little different problem here.
I have an array like this:
Array
(
[0] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1201"
)
[1] => Array
(
[name] => "Size"
[value] => "Small"
[id] => "1203"
)
[2] => Array
(
[name] => "Size"
[value] => "Medium"
[id] => "1204"
)
[3] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1205"
)
[4] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1206"
)
[5] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1207"
)
)
Above array have repetition of Large Three times, i want to identify unique on key based value. and remove that index (0,1,2,3,4,5) from that array.
Mentioned questions contains problems like this, but not the exact problem i am facing.
I am trying like this:
array_map("unserialize", array_unique(array_map("serialize", $input)));
but not working.
Since you have not answered my question yet I assume "id" is irrelevant.
By using array_column to make the array associative on "value" and it will delete any duplicates, then array_values will reset the keys to indexed.
This way you don't have to loop at all.
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1207"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
}
https://3v4l.org/aOhVS
If you want to keep the lowest "id", and the "id" is higher the further down in the array you go (as in your example), then you can use rsort($arr); before the code.
rsort($arr);
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1201"
}
}
https://3v4l.org/VtgAM
You could try make a foreach creating another array like you need
$arrayOrdenado = array();
foreach($array as $a){
$arrayOrdenado[$a["value"]][] = $a;
}

Accessing object inside array that is inside another object using PHP

I am new to CI. Using codeigniter, I am trying to print categories and their items as below:
Category 1 Name
item 1
item 2
Category 2 Name
item 3
item 4
item 5
and so on.
I have build an array and subarray inside controller as below:
$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
$data['categories'][$category->cID]= $this->publicpages_model->getitems($category->cID);
}
In the view, I am trying to use the following code but I am not able to complete it to get the desired output as discussed above.
foreach($categories AS $category)
{
echo '<h1>' . $category->name . '</h1>';
//Missing code to print the items
echo "<br>";
}
Although the name of category is printed but I am also getting the following error:
Severity: Notice
Message: Trying to get property of non-object
print_r($categories) gives following result:
Array ( [0] => stdClass Object ( [cID] => 1 [name] => Breakfast ) [1] => Array ( [0] => stdClass Object ( [itemID] => 1 [name] => Aaloo Paratha [description] => descriptio 1 [menu] => 1 [price] => 22 [tax] => 20 [sStatus] => ) ) [2] => stdClass Object ( [cID] => 7 [name] => Fast Food ) [5] => Array ( [0] => stdClass Object ( [itemID] => 5 [name] => Missi Roti [description] => Hot and Crunchy [menu] => 5 [price] => 5 [tax] => 1 [sStatus] => 1 ) ) [7] => )
vardump gives following output:
array(5) { [0]=> object(stdClass)#22 (2) { ["cID"]=> string(1) "1" ["name"]=> string(9) "Breakfast" } [1]=> array(1) { [0]=> object(stdClass)#25 (7) { ["itemID"]=> string(1) "1" ["name"]=> string(13) "Aaloo Paratha" ["description"]=> string(12) "descriptio 1" ["menu"]=> string(1) "1" ["price"]=> string(2) "22" ["tax"]=> string(2) "20" ["sStatus"]=> string(0) "" } } [2]=> object(stdClass)#24 (2) { ["cID"]=> string(1) "7" ["name"]=> string(9) "Fast Food" } [5]=> array(1) { [0]=> object(stdClass)#26 (7) { ["itemID"]=> string(1) "5" ["name"]=> string(10) "Missi Roti" ["description"]=> string(15) "Hot and Crunchy" ["menu"]=> string(1) "5" ["price"]=> string(1) "5" ["tax"]=> string(1) "1" ["sStatus"]=> string(1) "1" } } [7]=> bool(false) }
Please help with the solution.
You have a little problem there in your code. What you are trying to do is fine, but how you do it is the problem.
In this foreach loop, you are modifying the same array which you loop; hence it breaks the structure of the $data['categories'] array completely. As a result of that, your second element of the array is not having a name key, so it throws that warning.
foreach($data['categories'] as $category)
{
$data['categories'][$category->cID] = ...;
}
If you were trying to get the subitems of each category, and add them as a new key, you need to modify the $category array. Not the outer array :) So change it like this.
foreach($data['categories'] as $category)
{
$category->subItems = $this->publicpages_model->getitems($category->cID);
}
Or if you wanted the $category->cID instead of the key subItems, you can change the above to this:
foreach($data['categories'] as $category)
{
$category->{$category->cID} = $this->publicpages_model->getitems($category->cID);
}
I hope it helps :) feel free to ask anything if it's not clear.
imho what you are doing makes no sense - you should add the items to the category
try the following instead
your controller
$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
$category->arrItems = $this->publicpages_model->getitems($category->cID);
}
your view
foreach($categories AS $category)
{
echo '<h1>' . $category->name . '</h1>';
if (isset($category->arrItems) && is_array($category->arrItems))
{
foreach($category->arrItems AS $objItem)
{
print_r($objItem);
}
}
echo "<br>";
}

Trying to remove session array from array PHP

Im trying to remove a session['cart_items] array from a multi array and I can't seem to get the delete part to work.
I'm getting the id through: $_POST['product] however I've tried the following and getting a bit stuck:
$temp = array_flip($_SESSION['cart_items']);
unset($_SESSION['cart_items'][$temp[$_POST['product']]]);
I've also tried:
unset($_SESSION['cart_items'][$key]);
My output is:
Array
(
[0] => Array
(
[0] => Array
(
[item_id] => 407
[item_name] => Am I Bothered? About Homophobia
[item_qty] => 22
)
)
)
An help would be great
Multiple array result:
array(1) {
["cart_items"]=>
array(2) {
[1]=>
array(1) {
[0]=>
array(3) {
["item_id"]=>
string(3) "407"
["item_name"]=>
string(31) "Am I Bothered? About Homophobia"
["item_qty"]=>
string(2) "50"
}
}
[2]=>
array(1) {
[0]=>
array(3) {
["item_id"]=>
string(4) "1131"
["item_name"]=>
string(50) "10 Ways A Condom Can’t Protect You – Postcards"
["item_qty"]=>
string(2) "14"
}
}
}
}
for(i=0;i<sizeof($_SESSION['cart_items'][0]);i++){
if($_SESSION['cart_items'][0][i]['item_id'] == $key);
unset($_SESSION['cart_items'][0][i]['item_id']);
}

Access to PHP array elements

I have a PHP array when I used var_dump() this is the result I get:
array(1) { ["GetVehicleConfigurationByVehicleIdResult"]=> array(9) { ["Id"]=> string(1) "2" ["VIN"]=> NULL ["Year"]=> array(2) { ["Id"]=> string(4) "2006" ["Value"]=> string(4) "2006" } ["Make"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(5) "Acura" } ["Model"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(2) "TL" } ["Trim"]=> array(2) { ["Id"]=> string(6) "268650" ["Value"]=> string(12) "3.2 Sedan 4D" } ["Mileage"]=> string(6) "100000" ["OptionalEquipment"]=> array(1) { ["EquipmentOption"]=> array(35) { [0]=> array(13) { ["DisplayName"]=> string(19) "V6, VTEC, 3.2 Liter" ["VehicleOptionId"]=> string(3) "204" ["IsSelected"]=> string(4) "true" ["OptionTypeDisplayName"]=> string(6) "Engine" ["OptionGroupName"]=> string(3) "N/A" ["DisplayNameAdditionalData"]=> string(3) "N/A" ["ManufactureCode"]=> string(0) "" ["OptionAvailabilityDisplayName"]=> string(3) "N/A" ["IsDefaultConfiguration"]=> string(4) "true" ["DetailName"]=> string(3) "N/A" ["NonBoldName"]=> string(3) "N/A" ["Footer"]=> string(3) "N/A" ["SortOrder"]=> string(4) "1000" }
How I can get the elements from this array?
Some of the elements are complex, like they are array inside array.
That is the formatted print out of the array to understand better:
Array
(
[GetVehicleConfigurationByVehicleIdResult] => Array
(
[Id] => 2
[VIN] =>
[Year] => Array
(
[Id] => 2006
[Value] => 2006
)
[Make] => Array
(
[Id] => 2
[Value] => Acura
)
[Model] => Array
(
[Id] => 2
[Value] => TL
)
[Trim] => Array
(
[Id] => 268650
[Value] => 3.2 Sedan 4D
)
[Mileage] => 100000
[OptionalEquipment] => Array
(
[EquipmentOption] => Array
(
[0] => Array
(
[DisplayName] => V6, VTEC, 3.2 Liter
[VehicleOptionId] => 204
[IsSelected] => true
[OptionTypeDisplayName] => Engine
[OptionGroupName] => N/A
[DisplayNameAdditionalData] => N/A
[ManufactureCode] =>
[OptionAvailabilityDisplayName] => N/A
[IsDefaultConfiguration] => true
[DetailName] => N/A
[NonBoldName] => N/A
[Footer] => N/A
[SortOrder] => 1000
)
I want to get: Id, VIN, Year, Make, Model, Trim,Mileage and OptionalEquipment and pass them as 1 single parameter to another method.
It solved:
$Id = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Id'];
$Year = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Year']['Value'];
You are correct. There are arrays inside of arrays here. And var_dump shows it very nicely so you can perfectly know how to navigate the levels of this multi-dimensional array.
If you want VIN just get $array['GetVehicleConfigurationByVehicleIdResult']['VIN']
For Year you need to get $array['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']
I think you can guess the others now.
They are just array elements no matter how deep you go so you can just reference them by name like below:
$id = $that_array['GetVehicleConfigurationByVehicleIdResult']['Id'];
$id = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Id'];;
$vin = $array_name["GetVehicleConfigurationByVehicleIdResult"]['VIN'];
$year = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Year']
$make = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Make'];
$model = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Model'];
$trim = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Trim']['Value'];
$mileage = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Mileage'];
$optional_equipment = $array_name["GetVehicleConfigurationByVehicleIdResult"]['OptionalEquipment']['EquipmentOption'][0]['DisplayName'];
For simplicity's sake I'm assuming this array is saved as $array.
You can access the data from the array like this:
$vin = $array['GetVehicleConfigurationByVehicleIdResult']['VIN'];
But you stated that you want to pass them all as a single parameter, so to do that you would probably want to just pass an array.
someFuntion($array['GetVehicleConfigurationByVehicleIdResult']);

Displaying Array Values from DB

I can`t find a way to loop this structure of array . Anyone does encounter it.?
Print_r output
Array (
[0] => Array ( [0] => [LABOR_NO] => [1] => 3 [WORK_CODE] => 3 [2] => [MHR] => [3] => [PHR] => [4] => [PESO_VALUE] => )
[1] => Array ( [0] => [LABOR_NO] => [1] => 3 [WORK_CODE] => 3 [2] => [MHR] => [3] => [PHR] => [4] => [PESO_VALUE] => )
[2] => Array ( [0] => 1 [LABOR_NO] => 1 [1] => 3 [WORK_CODE] => 3 [2] => 2.50 [MHR] => 2.50 [3] => 0.00 [PHR] => 0.00 [4] => 3000.00 [PESO_VALUE] => 3000.00 )
)
Vardump Output
array(3) {
[0]=> array(10) {
[0]=> NULL ["LABOR_NO"]=> NULL [1]=> string(1) "3" ["WORK_CODE"]=> string(1) "3" [2]=> NULL ["MHR"]=> NULL [3]=> NULL ["PHR"]=> NULL [4]=> NULL ["PESO_VALUE"]=> NULL }
[1]=> array(10) { [0]=> NULL ["LABOR_NO"]=> NULL [1]=> string(1) "3" ["WORK_CODE"]=> string(1) "3" [2]=> NULL ["MHR"]=> NULL [3]=> NULL ["PHR"]=> NULL [4]=> NULL ["PESO_VALUE"]=> NULL }
[2]=> array(10) { [0]=> string(1) "1" ["LABOR_NO"]=> string(1) "1" [1]=> string(1) "3" ["WORK_CODE"]=> string(1) "3" [2]=> string(4) "2.50" ["MHR"]=> string(4) "2.50" [3]=> string(4) "0.00" ["PHR"]=> string(4) "0.00" [4]=> string(7) "3000.00" ["PESO_VALUE"]=> string(7) "3000.00" }
}
Heres what i have done but not working i think. i dont know => has been continues to each that turned them to $keys..
include('MySQLHandler.php');
class Displayer extends MySQLHandler {
public function getTitle($batchNo = NULL,$workCode = NULL){
$this->init();
$STMT="SELECT REC_NO,PARTS_WORKSCOPE FROM RAPID_TEMP_ESTIMATE_V3 WHERE BATCH_NO={$batchNo} AND WORK_CODE={$workCode} AND PARTS_WORKSCOPE_TYPE='Workscope:'";
$DATA=$this->Select($STMT);
// create array of data subjects..
$result = array();
$result['REC_NO'] = $DATA[0]['REC_NO'];
$result['PARTS_WORKSCOPE'] = $DATA[0]['PARTS_WORKSCOPE'];
return $result;
}
public function getTotal($batchNo = NULL,$workCode = NULL){
$this->init();
// par selection
$STMT="SELECT PAR_VALUE FROM RAPID_PAR WHERE PAR_NAME='LABOR_RATE_PER_HOUR'";
$DATA = $this->Select($STMT);
$PAR_VALUE = $DATA[0]['PAR_VALUE'];
// rollup
$STMT="SELECT LABOR_NO,WORK_CODE,MHR,PHR,PESO_VALUE FROM RAPID_TEMP_ESTIMATE_V3 WHERE BATCH_NO={$batchNo} AND WORK_CODE={$workCode} ORDER BY MINOR_WORK_CODE, WORK_CODE_ORDER";
$DATA=$this->Select($STMT);
foreach($DATA as $key=>$value){
foreach($value as $fieldset=>$values){
echo $fieldset.'<br/>';
}
}
}
}
$displayer = new Displayer;
$displayer->getTotal(18,3);

Categories