add a default value to an array - php

I have the following code in my opencart product.php control file.
$product_option_value_data = array();
foreach ($product_option['product_option_value'] as $product_option_value) {
$product_option_value_data[] = array(
'product_option_value_id' => $product_option_value['product_option_value_id'],
'option_value_id' => $product_option_value['option_value_id'],
'quantity' => isset ($product_option_value['quantity']) ? $product_option_value['quantity'] : '1',
'subtract' => $product_option_value['subtract'],
'price' => isset ($product_option_value['price'])? $product_option_value['price'] : '27.99',
'price_prefix' => $product_option_value['price_prefix'],
'points' => $product_option_value['points'],
'points_prefix' => $product_option_value['points_prefix'],
'weight' => $product_option_value['weight'],
'weight_prefix' => $product_option_value['weight_prefix']
);
}
I need to add default value to quantity, price, subtract.
Any help will be highly appreciated.

You are almost there. You have done that for two of your elements. Just add the same check for the subtract one.
$product_option_value_data = array();
foreach ($product_option['product_option_value'] as $product_option_value) {
$product_option_value_data[] = array(
'product_option_value_id' => $product_option_value['product_option_value_id'],
'option_value_id' => $product_option_value['option_value_id'],
'quantity' =>
isset ($product_option_value['quantity']) ?
$product_option_value['quantity'] :
'1', // THE DEFAULT FOR QUANTITY
'subtract' =>
isset ($product_option_value['subtract']) ?
$product_option_value['subtract'] :
'22', // THE DEFAULT FOR SUBTRACK
'price' =>
isset ($product_option_value['price']) ?
$product_option_value['price'] :
'27.99', //THE DEFAULT FOR PRICE
'price_prefix' => $product_option_value['price_prefix'],
'points' => $product_option_value['points'],
'points_prefix' => $product_option_value['points_prefix'],
'weight' => $product_option_value['weight'],
'weight_prefix' => $product_option_value['weight_prefix']
);
}
Hope this helps

Related

Php array not working. whats wrong?

I want to work with api.
Here is my code:
// create a new purchase bill
$purchase = $parasut->make('sale')->create(array (
'description' => $siparis,
'invoice_id' => null,
'invoice_series' => null,
'currency' => 'TRL',
'item_type' => 'invoice',
'issue_date' => $order_date_created,
'due_date' => $order_date_created,
'contact_id' => $contactToken,
'category_id' => null,
'archived' => false,
'billing_address' => $user_address,
'billing_fax' => null,
'billing_phone' => $user_phone,
'details_attributes' => array (
$parasut->make('product')->getProductFromOrder(), // the products
),
));
Here is getProductFromOrder() function:
public function getProductFromOrder()
{
$sku = array(5542003,5542004);
$qty = array("3","1");
$total = array("11.29","12.00");
for($i=0; $i<count($sku); $i++){
$urunler[$i] = array(
'product_id' => $sku[$i], // the products
'quantity' => '3',
'unit_price' => '12.99',
'vat_rate' => '18',
'discount_type' => 'amount',
'discount_value' => '0',
);
print ($urunler[$i]);
}
}
Its not working. When i run the first code, products not showing. Where is the mistake?
Thanks in advance for help.
Change the function getProductFromOrder() to this instead:
public function getProductFromOrder()
{
$sku = array(5542003,5542004);
$qty = array("3","1");
$total = array("11.29","12.00");
for($i=0; $i<count($sku); $i++){
$urunler[$i] = array(
'product_id' => $sku[$i], // the products
'quantity' => '3',
'unit_price' => '12.99',
'vat_rate' => '18',
'discount_type' => 'amount',
'discount_value' => '0',
);
}
return $urunler;
}
Use return instead of print and write the return outside the for loop.

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;
}
}

Looping Array in wordpress

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'];
}

Extracting data from complicated associative array in php and put into new array

I have an complicated array that looks like this:
$input=array(
(int) 0 => array(
'XXX' => array(
'id' => '7',
'p_id' => '1',
'address' => '9463',
'arrival_time' => '2014-05-01 03:30:00'
),
'YYY' => array(
'id' => '1',
'iden' => '1111',
'name' => 'Tom'
)
),
(int) 1 => array(
'XXX' => array(
'id' => '9',
'p_id' => '2',
'address' => '9469',
'arrival_time' => '2014-05-27 16:43:58'
),
'YYY' => array(
'id' => '2',
'iden' => '2222',
'name' => 'Sam'
)
),
(int) 2 => array(
'XXX' => array(
'id' => '3',
'p_id' => '3',
'address' => '9462',
'arrival_time' => '2014-04-21 14:05:00'
),
'YYY' => array(
'id' => '3',
'iden' => '3333',
'name' => 'James'
)
)
)
I would like to convert it such that it looks like this;
$output=array(
(int) 0 => array(
'name' => 'Tom',
'iden' => '1111',
'address' => '9463'
),
(int) 1 => array(
'name' => 'Sam',
'iden' => '2222',
'address' => '9469'
),
(int) 2 => array(
'name' => 'James',
'iden' => '3333',
'address' => '9462'
)
I wrote some code to solve this problem:
foreach ( $input as $key => $value)
{
$output['name']=$input[$key]['YYY']['name'];
$output['iden']=$input[$key]['YYY']['iden'];
$output['address']=$input[$key]['XXX']['address'];
}
Unfortunately, it retrieves only the last element of the input array.
Can someone more experienced help?
Thank you very much.
You are overwriting the values in each iteration, as you always write to $output['name'] etc.
foreach ( $input as $key => $value)
{
$output[$key] = array(
'name' => $value['YYY']['name'],
'iden' => $value['YYY']['iden'],
'address' => $value['XXX']['address']
);
}
The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.
Also $input[$key] and $value are equivalent, so I used the shorter variant ;)
Try this in your foreach loop :-
foreach ( $input as $key=>$value)
{
$output[$key]['name']=$value['YYY']['name'];
$output[$key]['iden']=$value['YYY']['iden'];
$output[$key]['address']=$value['XXX']['address'];
}
You have to add an index to the array in the foreach: $output[$key]["name"] = ...;

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']);

Categories