How to get data from foreach loop using below Array? - php

How to get the values from array? I am stucked in this. Anybody knows please help me. I am getting only main array's values.
[0] => stdClass Object
(
[id] => 68427249
[active] => 1
[name] => Three Way
[status] => open
[market_type_id] => 3896
[market_type_name] => Three Way
[market_type_order_number] => 1000
[event_id] => 8669447
[event_name] => Nuovo Campobasso Calcio vs Jesina Calcio
[line] =>
[scope] => full_event
[order_number] => 0
[selections] => Array
(
[0] => stdClass Object
(
[id] => 558087597
[name] => Nuovo Campobasso Calcio
[status] => ok
[odd] => 2.32
[lay_odd] => 1.0
[order_number] => 0
[line] =>
[market_subtype_id] => 293985
[market_subtype_code] => 11
[market_subtype_name] => Home
[market_subtype_order_number] => 0
)
[1] => stdClass Object
(
[id] => 558087568
[name] => Draw
[status] => ok
[odd] => 3.13
[lay_odd] => 1.0
[order_number] => 1
[line] =>
[market_subtype_id] => 293986
[market_subtype_code] => 10
[market_subtype_name] => Draw
[market_subtype_order_number] => 1
)
[2] => stdClass Object
(
[id] => 558087589
[name] => Jesina Calcio
[status] => ok
[odd] => 2.57
[lay_odd] => 1.0
[order_number] => 2
[line] =>
[market_subtype_id] => 293987
[market_subtype_code] => 12
[market_subtype_name] => Away
[market_subtype_order_number] => 2
)
)
)
my code
foreach($m as $ms)
{
echo '<tr><td>'.$ps->id.'</td><td>'.$ms->event_name.'</td><td>'.$ms['selections']->odd.'</td></tr>';
}
My code is not working to get values from [selections]. Please help me to get values.

It will be something like this to access the data of selections as its also array. So, you need to run loop for selections to get its value.
foreach ($array as $obj) {
echo $obj->name;
echo $obj->status;
if (is_array($obj->selections)) {
foreach ($obj->selections as $selection) {
echo $selection->name;
echo $selection->odd;
}
}
}
I hope it will help you.

You can access your object array values like this :
foreach ($objArr as $obj) {
echo '<tr><td>'.$obj->id.'</td><td>'.$obj->event_name.'</td>';
foreach ($obj->selections as $objS) {
echo '<td>'.$objS->odd.'</td>';
}
echo '</tr>';
}

<?php
$user = (Object)["name" => "Meraj","email"=> "merajsiddiqui#outlook.com", "education" => (Object)[
"school" => "JMI", "college" => "GGSIPU"]];
function printer($nested_object)
{
foreach ($nested_object as $property => $value) {
if (is_object($value)) {
printer($value);
} else {
echo $property."=".$value."\n";
}
}
}
printer($user);
Instead of echoing you can do as per your requirements;
//output
name=Meraj
email=merajsiddiqui#outlook.com
school=JMI
college=GGSIPU

Related

get std class value from an std classin php

stdClass Object
(
[id_pro_alert] => 155
[User_Id] => 252
[Pro_Id] => 329
[alertmethod] => daily
[Alertname] => xcxv
[name] => cyriac
[email] => cyriac#tewve.com
[date] => 2016-21-10 09:20:26
[mobile] =>
[comments] =>
[Alert_Id] => 329
[Prop_Purpose] => lease
[Min_Area] => 500
[Max_Area] => 1000
[Area_unit] => Sq.ft
[Min_cost] => 1000
[Max_cost] => 10000
[City] => kochi
[Create_Date] => 2016-10-17 12:59:14
[Update_Date] => 2016-10-17 12:59:14
[Status] => Active
[0] => stdClass Object
(
[Prop_type] => Agricultural Land
)
[1] => stdClass Object
(
[Prop_type] => Office in IT park
)
[2] => stdClass Object
(
[Prop_type] => Business center
)
)
How to get Prop_type value from stdclass
Check this, You can access all Prop_type's
foreach($data as $key=>$row){
if (is_int($key)) {
echo $row->Prop_type;
}
}
Create array of Prop_type's
$propData =array();
foreach($data as $key=>$row){
if (is_int($key)) {
$propData[] = $row->Prop_type;
}
}
echo $array->0->Prop_type;

PHP loops and array help, looping through results and targeting only specific records

Scenario:
I downloaded a Joomla extension and am editing the php file to alter the layout of the module. Now in the source code of the file lies this bit of code.
<?php if ( !empty($this->fields) ) { ?>
foreach ($this->fields as $field)
{
echo RSDirectoryFilter::getInstance($field, $options)->generate();
}
} ?>
which cycles through a list of form fields and prints out the fields and their options.
what i want to do is apply some styling to a specific form field only.
that loop printed out 3 fields altogether. My first step was to add a print_r($field) into the loop to see what data is stored within the $field parameter. each field was an array of data
this is what it printed out
stdClass Object ( [id] => 1 [field_type_id] => 1 [name] => title [column_name] => title [form_field_name] => title [required] => 1 [published] => 1 [field_type] => title [core] => 1 [create_column] => 0 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [form_caption] => Title [default_value] => [field_prepend] => [field_append] => [show_help_tip] => 1 [help_tip] => [show_help_text] => 1 [help_text_position] => block [help_text] => [readonly] => 0 [additional_html_attributes] => [id] => 1 [searchable_simple] => 1 [searchable_advanced] => textbox [searchable_advanced_caption] => Keywords [searchable_advanced_items] => [searchable_advanced_condition_type] => containing [default_validation_rule] => none [extra_accepted_chars] => [regex_syntax] => [custom_validation_rule] => [characters_limit] => 0 [validation_message] => There was an error with the title field. ) [separator] => . ) )
stdClass Object ( [id] => 31 [field_type_id] => 11 [name] => status [column_name] => f_31 [form_field_name] => status [required] => 1 [published] => 1 [field_type] => dropdown [core] => 0 [create_column] => 1 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [id] => 31 [help_text] => [additional_html_attributes] => [credits] => 0 [default_values] => Stolen Lost Found [default_value] => [size] => 1 [multiple] => 0 [field_prepend] => [field_append] => [show_help_tip] => 1 [help_tip] => [show_help_text] => 1 [help_text_position] => block [listing_caption] => status [dependency] => 0 [items] => Stolen Lost Found [form_caption] => status [searchable_simple] => 1 [searchable_advanced] => dropdown [searchable_advanced_caption] => Listing Status [use_dependency] => 1 [use_field_items] => 1 [searchable_advanced_items] => [searchable_advanced_condition_type] => strict [validation_message] => Invalid input. ) [separator] => . ) )
stdClass Object ( [id] => 34 [field_type_id] => 17 [name] => date-of-incident [column_name] => f_34 [form_field_name] => date_of_incident [required] => 1 [published] => 1 [field_type] => calendar [core] => 0 [create_column] => 1 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [form_caption] => Date Of Incident [listing_caption] => Date Of Incident [default_date] => [min_date] => [max_date] => [date_mask] => d F Y [time_mask] => g:i a [calendar_layout] => flat [readonly] => 0 [show_help_tip] => 0 [help_tip] => [show_help_text] => 0 [help_text_position] => block [help_text] => [additional_html_attributes] => [credits] => 0 [id] => 0 [searchable_simple] => 1 [searchable_advanced] => date_range [searchable_advanced_caption] => Date Range [searchable_advanced_items] => [searchable_advanced_condition_type] => strict [validation_message] => Invalid input. ) [separator] => . ) )
so the field i want to target is the one which has the [name] date-of-incident. (the 3rd one above)
i tried to modify the loop so target this specific record like so
<?php if ( !empty($this->fields) )
{
foreach ($this->fields as $field)
{
print_r($field);
if($field[name] == "date-of-incident")
{
echo "Date of incident here";
}
else
{
echo "<div class='span3'>";
echo RSDirectoryFilter::getInstance($field, $options)->generate();
echo "</div>";
}
}
} ?>
but this just crashes the site but doesnt give an error message, i assume i am using if($field[name] == "date-of-incident") incorrectly?
Appreciate any help
Thanks
Luke
Note from your print_r($field); that the output says they are Objects i.e. stdClass Object
You therefore address the name property using the object notation -> and not the array notation.
<?php
if ( !empty($this->fields) ) {
foreach ($this->fields as $field) {
//print_r($field);
// here is the change
if($field->name == "date-of-incident") {
echo "Date of incident here";
} else {
echo "<div class='span3'>";
echo RSDirectoryFilter::getInstance($field, $options)->generate();
echo "</div>";
}
}
}
?>

Compare two arrays and remove unwanted items

I'm so close to this I could just scream.
Here's what I'm after. I have two arrays. The first one is a follows:
array("id", "txtLname", "txtFname");
The second is as follows:
Array
(
[0] => Array
(
[id] => 220
[RecordGUID] => 1233C9-1F7A15-E8A447-C56CB2-227C20-2829E0
[txtEmplid] => 5469857
[txtLname] => Jones
[txtFname] => Richard
[txtMname] =>
[txtEmail] => email address
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-15 18:03:18
[Status] => 0
[Approvalcode] => 17dc8e7336f0e9fd3411a4d9617efe865c5744ac
[Approvaldate] => 2013-09-15 18:03:47
[Approved] => 1
)
[1] => Array
(
[id] => 221
[RecordGUID] => DD1E72-368879-68CFE2-E03010-ECE1B1-0974E9
[txtEmplid] => 4454688
[txtLname] => Mathews
[txtFname] => Richard
[txtMname] =>
[txtEmail] => user2#gmail.com
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user3#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:08
[Status] => 0
[Approvalcode] => 7182769e45dc38a3a747c4bdb128e2f0a8c658e4
[Approvaldate] =>
[Approved] => 0
)
[2] => Array
(
[id] => 222
[RecordGUID] => 40D8E7-04C600-30A829-8E26CC-498BBE-9D3DF6
[txtEmplid] =>
[txtLname] =>
[txtFname] =>
[txtMname] =>
[txtEmail] =>
[Reg_Pass] =>
[Reg_User] =>
[txtSecEmail] =>
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:24
[Status] => 0
[Approvalcode] => 8d2c33f7b7d6ef620811dbc4358e8103346bfb59
[Approvaldate] =>
[Approved] => 0
)
)
All I need is to loop through the second array and remove the items that do not match the first. The result would be as follows:
Array
(
[0] => Array
(
[id] => 220
[txtLname] => Method
[txtFname] => Richard
)
[1] => Array
(
[id] => 221
[txtLname] => Method
[txtFname] => Richard
)
[2] => Array
(
[id] => 222
[txtLname] => Method
[txtFname] => Richard
)
)
Here's what I've done so far.
foreach ($r as $reg) {
foreach($reg as $k => $v) {
if ($k == !in_array($k, $f)) {
echo $reg[$k];
}
}
}
The echo response I get is a listing of all of the data from the first array, minus the fields that are in the first array. So, it is removing items that
I want removed, but I can't seem to get it into the proper format.
Thanks in advance for your help.
$arr1 = array("id"=>1, "txtLname"=>1, "txtFname"=>1)
$result = array();
// $arr2 = array 2 (check your code)
foreach ($arr2 as $el) {
$result[] = array_intersect_key($el, $arr1);
}
var_dump($result);
replace echo $reg[$k]; with unset($reg[$k]);

retrieve matching elements in array

I have an array like this
Array
(
[35635d5ebdd938d6360e65a9e2484073] => Array
(
[rowid] => 35635d5ebdd938d6360e65a9e2484073
[id] => MYU_SC3
[qty] => 1
[price] => 4800
[name] => JAMB UTME Scratch Card
[service_image] => assets/img/jamb.jpg
[service_category] => cards
[subtotal] => 4800
)
[d8df18561040f3d9bd9868f5c5aaa7c2] => Array
(
[rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
[id] => MYU_SC1
[qty] => 1
[price] => 1600
[name] => WAEC Scratch Card
[service_image] => assets/img/waec.jpg
[service_category] => cards
[subtotal] => 1600
)
[a4a751dd9a69824eb3abb6f49c7a7f61] => Array
(
[rowid] => a4a751dd9a69824eb3abb6f49c7a7f61
[id] => MYU_SC2
[qty] => 1
[price] => 1600
[name] => NECO Scratch Card
[service_image] => assets/img/neco.jpg
[service_category] => cards
[subtotal] => 1600
)
)
I want to retrieve the sub-arrays that match elements in another array
$card_skus = array("MYU_SC1","MYU_SC2","MYU_SC3");
Am looping through the main array
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus))
{
//Didn't know what to do at this point
}
}
How do i get this done, thanks for the help
I guess #nickb already answered in the comments, didnt see it while writing an answer.
Try this
$subArray = array();
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus))
{
//Didn't know what to do at this point
$subArray[] = $item;
}
}
Use $subArray now as per your requirement

Search array? Then get sub values?

I have an array that looks like
[products] => Array
(
[0] => stdClass Object
(
[order_product_id] => 91385
[order_id] => 5065
[nid] => 2140
[title] => Gi Treasure
[manufacturer] =>
[model] => giftcard
[qty] => 5
[cost] => 0.00000
[price] => 25.00000
[weight] => 0
[data] => Array
(
[gift_description] => HJello!
[gift_email] => dubccom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => Thesure
[gift_card] => 2130
[gift_price] => 25
[gift_qty] => 5
[gift_name] => Steveek
[module] => uc_product
[cert_code] => 8-x8mfqXyUYXze
)
[order_uid] => 1
)
[1] => stdClass Object
(
[order_product_id] => 91386
[order_id] => 5065
[nid] => 2140
[title] => Gift asure 2
[manufacturer] =>
[model] => giftcard
[qty] => 1
[cost] => 0.00000
[price] => 35.00000
[weight] => 0
[data] => Array
(
[gift_description] => Hello There!
[gift_email] => dubcaom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => The Holida
[gift_card] => 2134
[gift_price] => 35
[gift_qty] => 1
[gift_name] => Steven
[module] => uc_product
[cert_code] => 9-8xsxgDW9yrMq
)
[order_uid] => 1
)
)
And I want to get the data array from array of products where the order_product_id (so if it was 91385 I would get
[data] => Array
(
[gift_description] => Hello
[gift_email] => dubccom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => Thesure
[gift_card] => 2130
[gift_price] => 25
[gift_qty] => 5
[gift_name] => Steveek
[module] => uc_product
[cert_code] => 8-x8mfqXyUYXze
)
Any help how I could do so?
function search_products($id,$products)
{
$id = intval($id);
foreach($products as $product)
{
if($product->order_product_id == $id)
{
return($product->data);
}
}
return null;
}
An educated guess at what you're looking for. Call it like search_products(91385, $products). If it returns null, it hasn't found the product ID. I also added a call to intval so if you're relying on user input for this, it'll be an int regardless. If you already sanitize it to an int, this doesn't hurt.
EDIT: Misread original post. Updated from array syntax to object property syntax.
function data_by_order ($arr, $orderId) {
foreach ($arr as $item) { // Loop the array
if ($item->order_product_id == $orderId) { // Test this item for the right order id
return $item->data; // Return the data array
}
}
return FALSE; // Return false if we didn't find it
}
// Example usage:
$data = data_by_order($array,91385);
print_r($data);
$prod_data = false;
foreach ($products as $product)
{
if ($product->order_product_id == 91385)
{
$prod_data = $product->data;
break;
}
}
if ($prod_data) {
// knock yourself out
}
Quite similar to plain English, huh?

Categories