This question already has answers here:
How do you remove an array element in a foreach loop?
(6 answers)
Closed 5 years ago.
This is a sample of my array:
Array
(
[productId] => 7740792
[productCode] => 1019534
[productPrice] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 0
[text] => £0.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
[variants] => Array
(
[0] => Array
(
[variantId] => 7740915
[sku] => 5784194
[isInStock] => 1
[isLowInStock] => 1
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
[1] => Array
(
[variantId] => 7740906
[sku] => 5784195
[isInStock] => 1
[isLowInStock] =>
[price] => Array
(
[current] => Array
(
[value] => 150
[text] => £150.00
)
[previous] => Array
(
[value] => 150
[text] => £150.00
)
[rrp] => Array
(
[value] => 0
[text] => £0.00
)
[xrp] => Array
(
[value] => 150
[text] => £150.00
)
[currency] => GBP
[isMarkedDown] =>
[isOutletPrice] =>
)
)
I want to delete/unset "productPrice", "sku" and "price" from the whole array.
I have used this so far:
unset($alldata[0]['variants'][0]['price']);
unset($alldata[0]['variants'][1]['price']);
But the array changes and has thousands of entries so coding the unset like this would not be easy. I am new to PHP and have searched all I can and looked up the array functions for something suitable with no luck.
unset ($alldata['productPrice']);
foreach ($alldata['variants'] as $key => $value) {
unset (
$alldata['variants'][$key]['price'],
$alldata['variants'][$key]['sku']
);
}
Related
I have a multidimensional array below and I want to loop through it and change the value of [City] shortcode, which is being pulled from a database.
Array
(
[0] => Array
(
[id] => 37482a99
[elType] => section
[settings] => Array
(
[content_width] => Array
(
[unit] => px
[size] => 1290
[sizes] => Array
(
)
)
[background_background] => classic
[padding] => Array
(
[unit] => px
[top] => 120
[right] => 0
[bottom] => 120
[left] => 0
[isLinked] =>
)
[background_image] => Array
(
[id] => 317
[url] => https://test.ca/wp-content/uploads/2021/09/inner_ser_banner.jpg
[alt] =>
[source] => library
)
[background_attachment] => fixed
[background_repeat] => no-repeat
[background_size] => cover
[background_overlay_background] => classic
[background_overlay_color] => #000000
[background_overlay_opacity] => Array
(
[unit] => px
[size] => 0.6
[sizes] => Array
(
)
)
[content_position] => middle
[gap] => no
[padding_tablet] => Array
(
[unit] => px
[top] => 100
[right] => 10
[bottom] => 100
[left] => 10
[isLinked] =>
)
[background_ypos] => Array
(
[unit] => px
[size] => -90
[sizes] => Array
(
)
)
)
[elements] => Array
(
[0] => Array
(
[id] => 2377e846
[elType] => column
[settings] => Array
(
[_column_size] => 100
[_inline_size] =>
)
[elements] => Array
(
[0] => Array
(
[id] => 14536ff4
[elType] => widget
[settings] => Array
(
[title] => Fall Protection Training & Certification in [City], [State]
[align] => left
[title_color] => #FFFFFF
[typography_typography] => custom
[typography_font_family] => Roboto
[typography_font_size] => Array
(
[unit] => px
[size] => 48
[sizes] => Array
(
)
)
[align_mobile] => center
[typography_font_size_tablet] => Array
(
[unit] => px
[size] => 48
[sizes] => Array
(
)
)
[typography_font_size_mobile] => Array
(
[unit] => px
[size] => 32
[sizes] => Array
(
)
)
[header_size] => h1
)
[elements] => Array
(
)
[widgetType] => heading
)
[1] => Array
(
[id] => 25c60fde
[elType] => widget
[settings] => Array
(
[view] => inline
[icon_list] => Array
(
[0] => Array
(
[text] => Home
[selected_icon] => Array
(
[value] =>
[library] =>
)
[_id] => b449156
)
[1] => Array
(
[text] => Fall Protection Training & Certification
[selected_icon] => Array
(
[value] => fas fa-angle-right
[library] => fa-solid
)
[_id] => 9fefeaf
)
)
[icon_color] => #FFFFFF
[text_color] => #FFFFFF
[icon_typography_typography] => custom
[icon_typography_font_family] => Roboto
[icon_typography_font_size] => Array
(
[unit] => px
[size] => 16
[sizes] => Array
(
)
)
[icon_typography_font_weight] => 400
[space_between] => Array
(
[unit] => px
[size] => 15
[sizes] => Array
(
)
)
[icon_align] => left
[icon_size] => Array
(
[unit] => px
[size] => 15
[sizes] => Array
(
)
)
[text_indent] => Array
(
[unit] => px
[size] => 0
[sizes] => Array
(
)
)
[icon_align_mobile] => center
[icon_typography_font_size_tablet] => Array
(
[unit] => px
[size] => 18
[sizes] => Array
(
)
)
[icon_typography_font_size_mobile] => Array
(
[unit] => px
[size] => 16
[sizes] => Array
(
)
)
)
[elements] => Array
(
)
[widgetType] => icon-list
)
)
[isInner] =>
)
)
[isInner] =>
)
[1] => Array
(
[id] => 544def4
[elType] => section
[settings] => Array
(
)
[elements] => Array
(
[0] => Array
(
[id] => 88de60a
[elType] => column
[settings] => Array
(
[_column_size] => 100
[_inline_size] =>
)
[elements] => Array
(
[0] => Array
(
[id] => 0447ddf
[elType] => widget
[settings] => Array
(
[editor] =>
Do I need fall protection training and certification in [City], ON?
According to the Government of Ontario and OHSA Regulation 213/91: Construction Projects, all employees need fall protection training and certification if they work on sites involving the use of:
[_padding] => Array
(
[unit] => px
[top] => 90
[right] => 90
[bottom] => 90
[left] => 90
[isLinked] => 1
)
)
[elements] => Array
(
)
[widgetType] => text-editor
)
)
[isInner] =>
)
)
[isInner] =>
)
)
Actually this is an elemntor array, which has all the page data stored in and I want to replace [City] with a value. Is this Possible?
I just started using moltin and learn to developing it.
In the getting started there is code that to show product
<?php
$product = \Product::Find(['slug' => 'baju']);
?>
documentation link https://moltin.com/getting-started/php
and the result is when I print array
<?php
print_r($product);
?>
It shows like this
Array ( [status] => 1 [result] => Array ( [0] => Array ( [id] => 1207658536885027482 [order] => [created_at] => 2016-03-17 03:08:58 [updated_at] => 2016-03-17 03:08:58 [sku] => baju-1 [title] => baju [slug] => baju [sale_price] => 0 [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [category] => Array ( [value] => Uncategorized [data] => Array ( [1134518259857490806] => Array ( [id] => 1134518259857490806 [order] => [created_at] => 2015-12-07 05:12:15 [updated_at] => 2015-12-07 05:12:15 [parent] => [slug] => uncategorized [status] => Array ( [value] => Live [data] => Array ( [key] => 1 [value] => Live ) ) [title] => Uncategorized [description] => Products that do not fit into another category ) ) ) [stock_level] => 10 [stock_status] => Array ( [value] => In Stock [data] => Array ( [key] => 1 [value] => In Stock ) ) [description] => baju [requires_shipping] => Array ( [value] => Yes [data] => Array ( [key] => 1 [value] => Yes ) ) [weight] => 0 [height] => 0 [width] => 0 [depth] => 0 [catalog_only] => Array ( [value] => No [data] => Array ( [key] => 0 [value] => No ) ) [tax_band] => Array ( [value] => Default [data] => Array ( [id] => 1134518260142703561 [title] => Default [description] => [rate] => 20 [created_at] => [updated_at] => ) ) [collection] => [brand] => [price] => Array ( [value] => £1.20 [data] => Array ( [formatted] => Array ( [with_tax] => £1.20 [without_tax] => £1.00 [tax] => £0.20 ) [rounded] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) [raw] => Array ( [with_tax] => 1.2 [without_tax] => 1 [tax] => 0.2 ) ) ) [is_variation] => [modifiers] => Array ( ) [images] => Array ( ) ) ) [pagination] => Array ( [total] => 1 [current] => 1 [limit] => 10 [offset] => 0 [from] => 1 [to] => 1 [offsets] => Array ( [first] => [previous] => [next] => [last] => ) [links] => Array ( [first] => [previous] => [next] => [last] => ) ) ) status = 1
How to get specific array from this list?
I already try
<?php
echo $product[0]['id'];
?>
it didn't work, show error Notice: Undefined offset: 0
you have a multidimensional array under the result key
access it the following way:
$product['result'][0]['id']
Give this a try:
echo $product['result'][0]['id'];
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I have this array
Array (
[0] => stdClass Object
(
[field_commerce_total] => Array
(
[0] => Array
(
[rendered] => Array
(
[#markup] => 64.00 HRK
[#access] => 1
)
[raw] => Array
(
[amount] => 6400
[currency_code] => HRK
[data] => Array
(
[components] => Array
(
[0] => Array
(
[name] => base_price
[price] => Array
(
[amount] => 5120
[currency_code] => HRK
[data] => Array
(
)
)
[included] => 1
)
[1] => Array
(
[name] => tax|sample_french_vat_tax
[price] => Array
(
[amount] => 1280
[currency_code] => HRK
[data] => Array
(
[tax_rate] => Array
(
[name] => sample_french_vat_tax
[display_title] => Croatia VAT (PDV) 25%
[description] =>
[rate] => 0.25
[type] => vat
[rules_component] => commerce_tax_rate_sample_french_vat_tax
[default_rules_component] => 1
[price_component] => tax|sample_french_vat_tax
[calculation_callback] => commerce_tax_rate_calculate
[module] => commerce_tax_ui
[title] => Croatia VAT (PDV) 25%
[admin_list] => 1
)
)
)
[included] => 1
)
)
[include_tax] => sample_french_vat_tax
)
)
)
)
)
)
This array structure is from print_r($results); from drupal view global:PHP. I need to retrieve the value of total amount so it can bee converted to other currency.
I tried to get to the single value like this
echo $results[0]->$field_commerce_total[0]->raw->amount;
It does not work. Andy idea where is my mistake?
Deeper field_commerce_total they are arrays not objects
echo $results[0]->field_commerce_total[0]['raw']['amount'];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
This my code ..... I need output of hotel name, id, selling price etc
array value will be loop because I have get result in bulk manner .
Array
(
[TestMode] => true
[HotelAvailability] => Array
(
[0] => Array
(
[Hotel] => Array
(
[Id] => 3027405
[Name] => Royal Plaza
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 3
[Rank] => 1
)
[Result] => Array
(
[QuoteId] => 12249136-10
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1002196
[Text] => Double Standard
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1824.26
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645204
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 912.13
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645205
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 912.13
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
[1] => Array
(
[Hotel] => Array
(
[Id] => 2118726
[Name] => Days Inn Deccan Plaza
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 3
[Rank] => 1
)
[Result] => Array
(
[QuoteId] => 12249136-36
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1002196
[Text] => Double Standard
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3192.90
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645292
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1596.45
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645293
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1596.45
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
[2] => Array
(
[Hotel] => Array
(
[Id] => 723729
[Name] => Green Park
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 4
[Rank] => 1
)
[Result] => Array
(
[0] => Array
(
[QuoteId] => 12249136-33
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1004527
[Text] => Double Or Twin Deluxe
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3273.82
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645286
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1636.91
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645287
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1636.91
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
[1] => Array
(
[QuoteId] => 12249136-34
[Room] => Array
(
[RoomType] => Array
(
[Code] => 2062742
[Text] => Double or Twin BUSINESS-CLUB
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3863.18
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645288
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1931.59
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645289
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1931.59
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
)
If I understood your question correctly, you would like to extract the Hotel Id, Name, and SellingPrice keys from this array, in a loop.
If we assume the above array is stored in a variable called $HotelData, and that the array structure is consistent with the above output in your question, then the following code should allow you to do what you want...
foreach($HotelData['HotelAvailability'] as $hotel) {
$id = $hotel['Hotel']['Id'];
$name = $hotel['Hotel']['Name'];
$price = $hotel['Result']['Room']['SellingPrice']['Currency'] . ' ' . $hotel['Result']['Room']['SellingPrice']['Amount'] ;
echo "$id - $name: $price<br>\n";
}
/* This should output something like
*
* 3027405 - Royal Plaza: INR 1824.26
* 2118726 - Days Inn Deccan Plaza - INR 3192.90
* ...
*/
foreach($data['HotelAvailability'] AS $record) {
echo 'Id: '; echo $record['Hotel']['Id'];
echo '<br />';
echo 'Name: '; echo $record['Hotel']['Name'];
echo '-------------'; echo '<br />';
}
I am trying to access data inside a php object. I am confused about what the exact stynax is. Here is the output of the object:
stdClass Object ( [vid] => 10 [uid] => 1 [title] => Beachhead Don [log] =>
[status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [ds_switch] => [nid] =>
10 [type] => product [language] => und [created] => 1357668665 [changed] => 1358198386
[tnid] => 0 [translate] => 0 [revision_timestamp] => 1358198386 [revision_uid] => 1
[model] => B 3000 [list_price] => 0.00000 [cost] => 0.00000 [sell_price] => 1.00000
[weight] => 0 [weight_units] => lb [length] => 0 [width] => 0 [height] => 0
[length_units] => in [pkg_qty] => 1 [default_qty] => 1 [unique_hash] =>
2eec0fcc8483f3a3541870fb24223296 [ordering] => 0 [shippable] => 0 [price] => 1.00000
[body] => Array ( [und] => Array ( [0] => Array ( [value] => Description info
))))))))))))))))))))))))))))))))))))))0 [summary] => [format] => filtered_html
[safe_value] =>Description info ))))))))))))))))))))))))))))))))))))))0[safe_summary]
=> ) ) ) [uc_product_image] => Array ( [und] => Array ( [0] => Array ( [fid] => 11
[alt] => [title] => [width] => 90 [height] => 120 [uid] => 1 [filename] =>
beachhead_don.jpg [uri] => public://beachhead_don.jpg [filemime] => image/jpeg
[filesize] => 14342 [status] => 1 [timestamp] => 1357668665 [rdf_mapping] => Array ( )
) ) ) [field_author] => Array ( [und] => Array ( [0] => Array ( [value] => John B.
Romesier [format] => [safe_value] => John B. Romesier ) ) ) [field_publisher] => Array
( [und] => Array ( [0] => Array ( [value] => Unknown [format] => [safe_value] =>
Unknown ) ) ) [field_release_date] => Array ( [und] => Array ( [0] => Array ( [value]
=> Mar 2012 [format] => [safe_value] => Mar 2012 ) ) ) [field_number_of_pages] => Array
( [und] => Array ( [0] => Array ( [value] => 123 ) ) ) [field_catagory] => Array (
[und] => Array ( [0] => Array ( [tid] => 4 ) ) ) [field_book_type] => Array ( [und] => Array ( [0] => Array ( [tid] => 1 ) ) ) [field_isbn] => Array ( ) [field_about_the_author] => Array ( [und] => Array ( [0] => Array ( [value] => Beachhed Don authoer............................ [format] => [safe_value] => Beachhed Don authoer............................ ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1357668665 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 )
I am trying to retrive the [value] of Description info ))))))))))))))))))))))))))))))))))))))0
I tried this
<?php
$node = menu_get_object(); //drupal code
if ( !empty($node) ) {
print $node ->body=>und=>0=>value;
}?>
and got an error about the '=' sign. What is the proper way to get my data?
[body] and [und] are arrays, so access them like this:
echo $node->body['und'][0]['value'];
It should be:
$node->body['und'][0]['value']
'Body' and 'und' are Arrays and need to be accessed with array syntax([]).
Accessing these values directly is sometimes frowned upon in drupal. You may want to look into: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_render/7