I have the following data from print_r($_SESSION):
Array (
[totalprice] => 954
[cart] => Array (
[115] => Array (
[name] => MÅNESKINN
[price] => 268.00
[count] => 1 )
[80] => Array (
[name] => DELFINLEK
[price] => 268.00
[count] => 1 )
[68] => Array (
[name] => OPPDAGELSEN
[price] => 418.00
[count] => 1 )
)
[shipping] => 65 )
Now I want to pull out all the price 268.00 and 418.00 from this session.
How can I do it?
I tried $_SESSION['cart']['price'], but it does not work.
$_SESSION['cart'] is an array. If you need only one row - it would be
$_SESSION['cart'][115]['price']
But you'd better walk through array:
foreach($_SESSION['cart'] as $item){
echo $item['price'];
}
Related
I want to combine same variant_id with its quantity and price from multiple array to one new array in PHP.
Currently, I have same variant_ids are '30929149493321, 19663805153353' in the sample data.
The Output Array should be combined the same variant_id with its quantity and price.
Input:
Array (
[0] => Array
(
[variant_id] => 30929149493321
[quantity] => 1
[price] => 299.00
)
[1] => Array
(
[variant_id] => 19663805153353
[quantity] => 1
[price] => 99.00
)
[2] => Array
(
[variant_id] => 19663804989513
[quantity] => 6
[price] => 99.00
)
[3] => Array
(
[variant_id] => 30929149493321
[quantity] => 1
[price] => 299.00
)
[4] => Array
(
[variant_id] => 19663805153353
[quantity] => 1
[price] => 99.00
)
[5] => Array
(
[variant_id] => 31108834754633
[quantity] => 1
[price] => 379.05
)
)
Desired output:
Array (
[0] => Array
(
[variant_id] => 30929149493321
[quantity] => 2
[price] => 299.00
)
[1] => Array
(
[variant_id] => 19663805153353
[quantity] => 2
[price] => 99.00
)
[2] => Array
(
[variant_id] => 19663804989513
[quantity] => 6
[price] => 99.00
)
[3] => Array
(
[variant_id] => 31108834754633
[quantity] => 1
[price] => 379.05
)
)
This code example below creates a new empty array $combined. It iterates over each element of your array ($array in the foreach should be replaced with your array name) and searches in the new $combined array if there is already an entry with the actual quantity_id. If it exists, it combines the two quantities. If it not exists, it pushes the actual values to the $combined array. Your desired result is stored in $combined.
$combined = array();
foreach( $array as $values ) {
if( ( $key = array_search( $values['variant_id'], array_column( $combined, 'variant_id') ) ) !== false ) {
$combined[$key]['quantity'] += $values['quantity'];
} else {
$combined[] = $values;
}
}
I have an array, which I have created by importing a csv file with the parsecsv library.
Array (
[0] => Array ( [cubcode] => EC023 [qty] => 2 )
[1] => Array ( [cubcode] => EC026A [qty] => 3 )
[2] => Array ( [cubcode] => EC025 [qty] => 7 )
[3] => Array ( [cubcode] => EC027 [qty] => 67 )
[4] => Array ( [cubcode] => EC031 [qty] => 567 )
[5] => Array ( [cubcode] => EC033 [qty] => 78 )
[6] => Array ( [cubcode] => EC034 [qty] => 234 )
[7] => Array ( [cubcode] => EC038 [qty] => 67 )
[8] => Array ( [cubcode] => EC038A [qty] => 67 )
[9] => Array ( [cubcode] => EC039 [qty] => 60 )
[10] => Array ( [cubcode] => EC039A [qty] => 100 )
)
I need to loop through the array to create two variables "cubcode" and "qty" and use them in a function that I have already created.
I am struggling to get to them because the seem to be nested in the array. I have not done much work with arrays, so please be gentle with me.
Thanks.
Sorry for the code only answer, but you literally just need to write it in PHP, and not in English. Since $csv->data (from comments) is the name of the array:
foreach ($csv->data as $item) {
function_you_already_created($item['cubcode'], $item['qty']);
}
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 have try to remove whole parent array using PHP unset(key) function based on value from a nested array in it.
Nested array looks like
Array (
[0] => Array (
[ID] => 9909
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 49.97
[ITEMS_AMOUNT] => 44.97
[ITEMS] => Array (
[0] => Array (
[CODE] => TEST
[QTY] => 1
[UNIT_PRICE] => 14.99 )
[1] => Array (
[CODE] => NNKIT
[QTY] => 1
[UNIT_PRICE] => 9.99 )
[2] => Array (
[CODE] => MAINKIT
[QTY] => 1
[UNIT_PRICE] => 19.99 )
)
)
[1] => Array (
[ID] => 9910
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 74.96
[ITEMS_AMOUNT] => 69.96
[ITEMS] => Array (
[0] => Array (
[CODE] => NNKIT
[QTY] => 1
[UNIT_PRICE] => 9.99 )
[1] => Array (
[CODE] => MAINKIT
[QTY] => 3
[UNIT_PRICE] => 19.99 )
)
)
[2] => Array (
[ID] => 9911
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 44.98
[ITEMS_AMOUNT] => 39.98
[ITEMS] => Array (
[0] => Array (
[CODE] => MAINKIT
[QTY] => 2
[UNIT_PRICE] => 19.99 )
)
)
[3] => Array (
[ID] => 9912
[SHIPPING_AMOUNT] => 5
[TOTAL_TAX] => 0
[GRAND_AMOUNT] => 29.98
[ITEMS_AMOUNT] => 24.98
[ITEMS] => Array (
[0] => Array (
[CODE] => TEST
[QTY] => 1
[UNIT_PRICE] => 14.99 )
[1] => Array (
[CODE] => NEWTEST
[QTY] => 1
[UNIT_PRICE] => 9.99 )
)
)
and value which I check for is CODE = MAINKIT. If not exists in a nested array then main array should be removed (in this case parent array[3]) but somehow returned key is from nested array not a parent one.
PHP code:
foreach($array as $key => $value){
if(is_array($value) && $value['CODE'] != 'MAINKIT')
unset($key);
}
The CODE elements are inside the $value['ITEMS'] array, not directly in $value.
To tell whether MAINKIT isn't in any of the items, you have to loop through all the items, testing whether any of them matches. If none do, then delete the array element.
Try:
foreach($array as $key => $value){
if (is_array($value) && is_array($value['ITEMS']))
$found_mainkit = false;
foreach ($value['ITEMS'] as $item) {
if($item['CODE'] == 'MAINKIT') {
$found_mainkit = true;
break;
}
}
if (!$found_mainkit) {
unset($array[$key]);
}
}
}
I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
I want to remove duplicate entry of [Import][product_id]. So my expected result is :
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
Would you write a function to filter this type of array and produce expected result. I have been googling for 2 days but no luck.
This is a handy one liner that should do the trick:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
If the underlying arrays are not identical, that won't work, in which case I think you could do:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
Tested: http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d
Here is a quick stable sort and reduce which runs in linearithmic time. First-encountered product Id's are kept, and entries with duplicate product Id's are ignored.
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== #$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
Demo: http://ideone.com/BP0eUJ
Update: Here is an even better linear-time algorithm that does the same as above using a fast "hash table" lookup. Again, the first-encountered product Id is kept and subsequent ones of the same Id are ignored.
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
Demo: http://ideone.com/5RF0og