Looping Array in wordpress - php

I have problem with my code here, I want convert serialize data in wordpress like this
$data ='a:2:{i:0;a:8:{s:8:"order_id";s:2:"19";s:5:"print";s:18:"type-canvas-framed";s:4:"size";s:12:"08-x-10-inch";s:18:"frame_canvas_color";s:10:"blackframe";s:11:"orientation";s:8:"portrait";s:3:"qty";s:1:"1";s:5:"price";d:42.990000000000002;s:8:"shipping";d:13.800000000000001;}i:1;a:7:{s:8:"order_id";s:2:"19";s:5:"print";s:11:"type-poster";s:4:"size";s:12:"36-x-48-inch";s:11:"orientation";s:8:"portrait";s:3:"qty";s:1:"1";s:5:"price";d:42.990000000000002;s:8:"shipping";d:14.800000000000001;}}' ;
I do parse the data using unseriliaze using unserialize the result like this
$result=array (
0 =>
array (
'order_id' => '19',
'print' => 'type-canvas-framed',
'size' => '08-x-10-inch',
'frame_canvas_color' => 'blackframe',
'orientation' => 'portrait',
'qty' => '1',
'price' => 42.99,
'shipping' => 13.8,
),
1 =>
array (
'order_id' => '19',
'print' => 'type-poster',
'size' => '36-x-48-inch',
'orientation' => 'portrait',
'qty' => '1',
'price' => 42.99,
'shipping' => 14.8,
),
);
I want to looping the array, how to do that in wordpress.
Thanks

you dont need wordpress specific functions for that use:
foreach($result as $key => $value){
// process array
}

just use foreach
foreach($result as $key => $value ) {
echo $value['order_id'];
}

Related

How to access this array

array (
'_attributes' =>
array (
'name' => 'Rothco Product API - Variations',
),
'item_variations' =>
array (
0 =>
stdclass::__set_state(array(
'item_variation_id' => 2,
'item_index' => 3146,
'rothco_item_no' => '10002',
'upc' => '023063601212',
'inventory' => 99,
'created_date' => '2014-11-28 10:06:45.000',
'weight' => '.4000',
'image_filename' => '10002-A.jpg',
'catalog_page_no' => 183,
'msrp' => '20.9900',
'map' => '.0000',
'diameter' => '',
'price' => '8.100000',
'case_price' => NULL,
'case_quantity' => NULL,
'statuses' => '',
)),
),
)
This is my array $list.
I want to access 'item_variations' value from this array,
but if I try $list['item_variations'], or $list->['item_variations']
it is not working
If you want to reach just item_variations then
echo $list['item_variations'];
is sufficient. Things get more tricky if you would like to i.e. get value if created_date from your sample data as you got mix of arrays and objects so that require different access:
echo $list['item_variations'][0]->created_date;
which would output
2014-11-28 10:06:45.000

php insert key/value into associative array

I'm trying to insert a couple of new Key/Value pairs into an associative array at a specific place. From other reading I've done on SO, I'm pretty sure I have to loop through the array and insert the new values when a condition is set.
Here is the current array
array(
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
)
)
)
)
)
(int) 1 => array(
'Product' => array(
'id' => '60',
'title' => 'Red Dress',
'Review' => array()
)
)
)
The key Review does not always have data, but when it does I want to insert a new key-value similar to the following excerpt
(int) 0 => array(
'Product' => array(
'id' => '59',
'title' => ' Blue Dress',
'Review' => array(
'id' => '7',
'product_id' => '59',
'Review' => array(
(int) 0 => array(
'average' => '3.0000'
'some_value' => '5'
)
)
)
)
)
I've tried a few things without success.
Any help is much appreciated thanks.
You can do something like this:
if(!empty($your_array[index]['Product']['Review'])){
$your_array[index]['Product']['Review']['Review'][index]['some_value'] = 'new_value';
}
In your example it could be:
if(!empty($your_array[0]['Product']['Review'])){
$your_array[0]['Product']['Review']['Review'][0]['some_value'] = 'new_value';
}
Again, you didn't mention your code. So, it's hard to figure out what you want exactly!
You should iterate through Your array and pass current value be reference:
// Notice & sign before variable
foreach ($data as &$product)
{
if ($product['Product']['Review'])
{
// or iterate through Review array
$product['Product']['Review']['Review'][0]['some_value'] = 5;
}
}

php - check and correct related items?

So I have a huge array of parts.
With a lot of help, I managed to associate related parts with a singular reference key.
Thus I have the following data;
array(
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '1', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '2', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '50', 'uid' => '3', 'rid' =>'2' ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '250', 'uid' => '4', 'rid' =>'2' )
)
Now, if you pay attention to Price, UID and RID - you will see a pattern.
The first two array elements belong together, as do the last 2 elements (they have the same Related ID).
The problem is - the prices are divergent!
I need a way to do the following;
1) add a flag in the array for divergent pricing
2) change all prices to the lowest one
As a bonus - I'm also looking to create a "parent" element ... thus I'd add two new sub-arrays (one for RID1 and one for IRD2), combining the values of MDL, PART and MAKE etc.).
I've attempted this myself, and generally created a muddle.
I can foreach over and generate a new array with 1 child per RID - but I cannot append the values of multiple child arrays from the original :( (I thought I was getting close, but added the price values!).
$composite = array();
foreach($data as $value) {
if(array_key_exists($value['rid'], $composite)) {
$composite[$value['rid']] += $value['price'];
//$composite[$value['rid']] = array('prices'=>$value['price']);
} else {
$composite[$value['rid']] = $value['price'];
//$composite[$value['rid']] = array('prices'=>$value['price']);
}
}
foreach($composite as $key=>$out){
echo $key ." : "; print_r($out); echo "<br/>";
}
So ... for the sake of clarity ... I'd like to take this;
array(
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '1', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '2', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '70', 'uid' => '3', 'rid' =>'2' ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '70', 'uid' => '4', 'rid' =>'2' )
)
and end up with this;
array(
// New Parent (for rid=1's)
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1,prt2', 'price' => '50', 'uid' => '1', 'rid' =>'1', 'divprice' => true ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '2', 'rid' =>'1', 'divprice' => true ),
// Price changed from 150 to 50 - based on lowest price for matching rid
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '3', 'rid' =>'1', 'divprice' => true ),
// New Parent (for rid=2's)
array ( 'make' => 'co1, co2', 'model' => 'mdl2, mdl3', 'part' => 'prt3,prt4', 'price' => '70', 'uid' => '4', 'rid' =>'2', 'divprice' => false),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '70', 'uid' => '5', 'rid' =>'2', 'divprice' => false ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '70', 'uid' => '6', 'rid' =>'2', 'divprice' => false )
)
I know it looks complicated - but I've seen "similar" functionality in code - but for the life of me I cannot get any of it to work ... I end up merging and losing values, or adding them together (so the price ends up as 200 instead of 50 etc.)
Thank you.
Not sure what you mean by a flag for divergent prices, but for the bit where you want to update all the prices with the lowest one, you could do something like:
// Your data
$products = array(
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt1', 'price' => '50', 'uid' => '1', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl1', 'part' => 'prt2', 'price' => '150', 'uid' => '2', 'rid' =>'1' ),
array ( 'make' => 'co1', 'model' => 'mdl2', 'part' => 'prt3', 'price' => '70', 'uid' => '3', 'rid' =>'2' ),
array ( 'make' => 'co2', 'model' => 'mdl3', 'part' => 'prt4', 'price' => '70', 'uid' => '4', 'rid' =>'2' )
);
// Get lowest product
function get_lowest_product($products) {
$lowest = 0;
foreach ($products as $key => $product) {
$price = (int)$product["price"];
if ($price < $lowest || $lowest == 0) {
$lowest = (int)$product["price"];
}
}
return $lowest;
}
// Replace value in array
function replace_all_prices_to_lowest($products, $lowest) {
foreach ($products as $key => &$product) {
$product["price"] = (string)$lowest;
}
return $products;
}
// Testing
$lowest = get_lowest_product($products);
$replaced = replace_all_prices_to_lowest($products, $lowest);
var_dump($replaced);

PHP unserialize function get any number from 0-9

I need to get product id with the unserialize php funcion. I have this text
a:1:{i:4;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}
and I get the product_id with this PHP code:
$rslt = unserialize($data);
echo $rslt[4]["product_id"]);
So my question is there a way to do something like echo $rslt[x]["product_id"];where x is any number between 0-9
Also tried this but doesn't work
$i=0;
while($rslt[$i]["product_id"]!="")
{
echo $i;
//echo $rslt[4]["product_id"];
echo $rslt[$i]["product_id"];
$i++;
}
Once you unserialize your input you have a good old PHP array, equivalent to:
$rslt = array (
4 =>
array (
'quantity' => 1,
'product_id' => 5196,
'category_id' => '209',
'price' => 1,
'tax' => '18.00',
'tax_id' => '1',
'description' => '',
'product_name' => 'test',
'thumb_image' => '',
'ean' => '',
'attributes' => 'a:0:{}',
'attributes_value' =>
array (
),
'weight' => '0.0000',
'vendor_id' => '0',
'files' => 'a:0:{}',
'freeattributes' => 'a:0:{}',
'dependent_attr_serrialize' => 'a:0:{}',
),
);
To grab the first element, just call current() as with any other array:
$first_item = current($rslt);
print_r($first_item['product_id']);

Ubercart uc_cart_get_contents get data from standard class

I'm writing a custom module for one of my projects and I'm having a bit of an issue. What I'm trying to do is get a piece of data from uc_cart_get_contents() into a variable so that I can use it in a calculation:
Here's my var_export:
array ( )array ( 0 => stdClass::__set_state(array( 'cart_item_id' => '5', 'cart_id' => '1', 'nid' => '9', 'qty' => '10', 'changed' => '1287074120', 'data' => array ( 'attributes' => array ( 3 => '4', ), 'shippable' => '1', 'restrict_qty' => '0', 'module' => 'uc_product', ), 'title' => 'Chicago Canyon', 'vid' => '9', 'cost' => 1.16, 'price' => 11.16, 'weight' => 0, 'module' => 'uc_product', 'model' => 'chicago-canyon-p-card-p-env', )), )
But what if I want to put the model of each item in the cart into a variable, and based upon that variable adjust the item's price.
If it was an array I would do this:
$items= uc_cart_get_contents();
$model = $items[model];
but that doesn't work, it's a std class. I'm so so lost. Please help!
If your asking how to access object propertiers you do so like this:
$model = $items->model;
for starters, uc_cart_get_contents() returns an array of objects. so, if you did:
$items=uc_cart_get_contents();
you would have to iterate over $items, and then get the specific data elements you want from each item.
for example:
$items = uc_cart_get_contents();
foreach ($items as $item)
{
$model = $item->model;
// whatever ...
}
hope that helps

Categories