I have a PHP variable, let's say named $products
when I try to print the first element in that variable by printing $products[0], the array print the word (array) on the screen.
how I can access each element in this array? thank you
Array(
[0] => Array
(
[id] => 2356
[title] => REAR BRAKE
[price] => $0
[image] => 2356_BRS-10228-1.jpg
[quantity] => 1
)
[1] => Array
(
[id] => 2358
[title] => REAR BRAKE MITSUBITSHI
[price] => $0
[image] => 2358_BRS-10230-1.jpg
[quantity] => 1
)
[2] => Array
(
[id] => 2360
[title] => Axela CX-4
[price] => $0
[image] => 2360_BRP-10639-1.jpg
[quantity] => 1
)
)
you can use foreach loop :
foreach($data as $key =>$value){
echo $value['id'].'<br>';
}
result :
2356
2358
2360
Related
please someone will guide me to how to add the values of below conditions and then update the added value in code .
how to sum value of where code =low_order_fee and where code= goods_total then update the sum value in where code=goods_total.
Array
(
[0] => Array
(
[order_total_id] => 999
[order_id] => 194
[code] => goods_total
[title] => Goods-Total
[text] => £130.00
[value] => 130.0000
[sort_order] => 1
)
[1] => Array
(
[order_total_id] => 1000
[order_id] => 194
[code] => low_order_fee
[title] => * Carriage
[text] => £10.00
[value] => 10.0000
[sort_order] => 3
)
[2] => Array
(
[order_total_id] => 1001
[order_id] => 194
[code] => sub_total
[title] => Sub-Total
[text] => £130.00
[value] => 130
[sort_order] => 4
)
[3] => Array
(
[order_total_id] => 1002
[order_id] => 194
[code] => tax
[title] => VAT (20%)
[text] => £26.00
[value] => 26.0000
[sort_order] => 5
)
[4] => Array
(
[order_total_id] => 1003
[order_id] => 194
[code] => total
[title] => Invoice Total
[text] => £166.00
[value] => 166.0000
[sort_order] => 9
)
)
A rudimentary solution:
foreach ($array_var as $key => $item) {
if ($item['code'] == 'low_order_fee') {
$first_val = $item['value'];
}
if ($item['goods_total'] == 'low_order_fee') {
$sec_val = $item['value'];
$position = $key;
}
}
$array_var[$position]['goods_total'] = $first_val + $sec_val;
But maybe you should think about store the values in another way to make easier access to them.
I hope it helps.
I am working with a multidimensional array in PHP. I would like to detect the presence of similar values, then count the number of similar values and out put the results. For example, given the following array:
$products =
Array
(
[0] => Array
(
[price] => 100
[product] => cloths
[qty] => 3
)
[1] => Array
(
[price] => 101
[product] => cloths
[qty] => 10
)
[2] => Array
(
[price] => 102
[product] => cloths
[qty] => 16
)
[3] => Array
(
[price] => 103
[product] => cloths
[qty] => 1
)
[4] => Array
(
[price] => 108
[product] => cloths
[qty] => 6
)
[5] => Array
(
[price] => 107
[product] => cloths
[qty] => 4
)
[6] => Array
(
[price] => 109
[product] => cloths
[qty] => 5
)
[7] => Array
(
[price] => 105
[product] => cloths
[qty] => 2
)
[8] => Array
(
[price] => 104
[product] => cloths
[qty] => 5
)
[9] => Array
(
[price] => 106
[product] => cloths
[qty] => 2
)
[10] => Array
(
[price] => 111
[product] => cloths
[qty] => 1
)
)
how to approach this problem?
foreach ($products as $key => $product) {
$price = $product['price'];
//now using this price how can i get all keys which are equal to this price
}
EDIT
i have tried with this but no use
echo $key = array_search(100, array_column($products, 'price'));
try below:
$keys = array_keys(array_filter($product, function($val) {return $val['price'] == 100; })
I would create an array containing prices as keys and product keys as values:
$prices = array();
foreach ($products as $key => $product) {
$prices[$product['price']][] = $key;
}
Now, $prices[105] contains an array of all product keys with price = 105 (in the specific example, it's only one: 7):
var_dump($prices[105]);
# array(1) {
# [0]=>
# int(7)
# }
I have an array in php called $json. It looks like this
Array (
[0] => Array (
[name] => Knife
[url] => http://steamcommunity.com/market/listings/753/297120...
[price] => 0.16 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/imag...
[quantity] => 30
[game] => Counter-Strike: Global Offensive )
[1] => Array (
[name] => Strange Knife
[url] => http://steamcommunity.com/market/listings/440/Strange%2...
[price] => 0.55 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/imag...
[quantity] => 177
[game] => Team Fortress 2 )
[2] => Array (
[name] => Festive Knife
[url] => http://steamcommunity.com/market/listings/440/Festive%20Knife" id="resultlink_2
[price] => 3.72 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
[quantity] => 66
[game] => Team Fortress 2 )
[3] => Array (
[name] => â… Flip Knife
[url] => http://steamcommunity.com/market/listings/7
[price] => 3.72 USD
[image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
[quantity] => 24
[game] => Counter-Strike: Global Offensive )
...
How I can remove a part of an array where GAME != "Counter-Strike: Global Offensive" ?
$array; // Your array.
foreach($array as $subarray) {
if($subarray['game'] != "Counter-Strike: Global Offensive") {
unset($subarray);
}
}
I have this variable $products that I'm getting from the db. Now when I print what's in it it shows
Array ( [0] => Array ( [quantity] => 1 [product_id] => 5363 [category_id] => 209 [price] => 1 [tax] => 18.00 [tax_id] => 1 [description] => [product_name] => test2 [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:{} [href] => /kategorii/product/view/209/5363.html [free_attributes_value] => Array ( ) ) )
So my question is how can i get what is the product_id number?
I print this using print_r($products); but is gives me this error
Warning: current() [function.current]: Passed variable is not an array or object in ...
This is the code that is causing this problem:
$cart = JModel::getInstance('cart', 'jshop');
$cart->load();
$products=$cart->products;
//$productos[0]['product_id'];
print_r($products);
Assuming the variable you did a print_r of above is assigned to $products:
$products[0]['product_id']
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'];
}