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?
Related
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']
);
}
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'];
I am trying to merge an array containing a multidimensional array using the value inside the multidimensional array called [color_id] key and combining the values inside the [details] key, Then, sort the values in the [model] key from least to greatest.
Here is the multidimensional array:
Array (
[0] => Array
(
[color_id] => 5
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 387110
[size] => small
)
[1] => Array
(
[quantity] => 1
[model] => 771410
[size] => medium
)
)
[1] => Array
(
[color_id] => 5
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 811110
[size] => medium
)
[1] => Array
(
[quantity] => 1
[model] => 958010
[size] => large
)
)
)
[2] => Array
(
[color_id] => 36
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 811310
[size] => small
)
[1] => Array
(
[quantity] => 1
[model] => 101901
[size] => large
)
)
)
[3] => Array
(
[color_id] => 36
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 387010
[size] => medium
)
[1] => Array
(
[quantity] => 2
[model] => 564310
[size] => medium
)
[2] => Array
(
[quantity] => 1
[model] => 864328
[size] => small
)
)
)
[4] => Array
(
[color_id] => 74
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 469871
[size] => large
)
[1] => Array
(
[quantity] => 2
[model] => 368599
[size] => medium
)
[2] => Array
(
[quantity] => 1
[model] => 785958
[size] => small
)
)
)
[5] => Array
(
[color_id] => 74
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 958741
[size] => small
)
[1] => Array
(
[quantity] => 2
[model] => 123688
[size] => medium
)
)
)
)
I'm trying to get this result without any luck.
Array (
[0] => Array
(
[color_id] => 5
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 387110
[size] => small
)
[1] => Array
(
[quantity] => 1
[model] => 771410
[size] => medium
)
[3] => Array
(
[quantity] => 1
[model] => 811110
[size] => medium
)
[4] => Array
(
[quantity] => 1
[model] => 958010
[size] => large
)
)
[1] => Array
(
[color_id] => 36
[details] => Array
(
[0] => Array
(
[quantity] => 1
[model] => 101901
[size] => large
)
[1] => Array
(
[quantity] => 1
[model] => 387010
[size] => medium
)
[3] => Array
(
[quantity] => 2
[model] => 564310
[size] => medium
)
[4] => Array
(
[quantity] => 1
[model] => 811310
[size] => small
)
[5] => Array
(
[quantity] => 1
[model] => 864328
[size] => small
)
)
)
[2] => Array
(
[color_id] => 74
[details] => Array
(
[0] => Array
(
[quantity] => 2
[model] => 123688
[size] => medium
)
[1] => Array
(
[quantity] => 2
[model] => 368599
[size] => medium
)
[2] => Array
(
[quantity] => 1
[model] => 469871
[size] => large
)
[3] => Array
(
[quantity] => 1
[model] => 785958
[size] => small
)
[4] => Array
(
[quantity] => 1
[model] => 958741
[size] => small
)
)
)
)
Greatly appreciate the help. Thank you.
I would do it like this (expecting your array as $array):
// output array
$newArray = array();
// loop through the $array
for($i=0; $i<count($array); $i++) {
// check if this is the first iteration
if($i == 0) {
// if so, push the first element into the new array
array_push($newArray,$array[$i]);
// and continue with the next iteration
continue;
}
// found flag for the color_id key
$found = false;
// loop through the new array to check if the color_id is in there yet
foreach($newArray as &$subArr) {
// check if there is the color id of the current iteration (of the for loop)
if($subArr['color_id'] == $array[$i]['color_id']) {
// if it is, push all the details in this color_id, flag as found and break out
foreach($array[$i]['details'] as $details) array_push($subArr['details'],$details);
$found = true;
break;
}
}
// check if the color_id was found
if(!$found) {
// if not, push the current color_id into the new array
array_push($newArray,$array[$i]);
}
}
// loop through the new array
foreach($newArray as &$newSubArray) {
// sort by model
uasort($newSubArray['details'], function($a,$b){return $a['model']-$b['model'];});
}
By the way: is it necessary to save the color_name in this array? Because it is redundant ;)
I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}
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