how to add to value in a single array in php - php

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.

Related

Calculations in a multidimensional array

I have an array which contains sub-elements that have several occurrences of the same element with different quantity:
Example array:
Array
(
[7] => Array
(
[id] => 11399
[groupid] => 18
[name] => Morphite
[quantity] => 150
)
[8] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 44
)
[9] => Array
(
[id] => 16678
[groupid] => 429
[name] => Sylramic Fibers
[quantity] => 11
)
[10] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 17
)
[11] => Array
(
[id] => 16680
[groupid] => 429
[name] => Phenolic Composites
[quantity] => 6
)
[12] => Array
(
[id] => 33361
[groupid] => 429
[name] => Plasmonic Matamaterials
[quantity] => 2
)
[13] => Array
(
[id] => 16681
[groupid] => 429
[name] => Nanotransistors
[quantity] => 2
)
[14] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 22
)
)
What I want to do, is add all the elements with the same name / id together so that I'll be able to display one line with the right quantity.
Pretty simple to do with a loop.
//create new array to hold new array format
$new_array = [];
//loop through old array
foreach($old_array as $value) {
//check if $value['id'] key exists in `$new_array`
if(!array_key_exists($value['id'], $new_array)) {
//if key does not exist, create it
$new_array[$value['id']] = $value;
} else {
//if key does already exist, increment quantity
$new_array[$value['id']]['quantity'] += $value['quantity'];
}
}
Output from your example array:
Array
(
[11399] => Array
(
[id] => 11399
[groupid] => 18
[name] => Morphite
[quantity] => 150
)
[16673] => Array
(
[id] => 16673
[groupid] => 429
[name] => Fernite Carbide
[quantity] => 83
)
[16678] => Array
(
[id] => 16678
[groupid] => 429
[name] => Sylramic Fibers
[quantity] => 11
)
[16680] => Array
(
[id] => 16680
[groupid] => 429
[name] => Phenolic Composites
[quantity] => 6
)
[33361] => Array
(
[id] => 33361
[groupid] => 429
[name] => Plasmonic Matamaterials
[quantity] => 2
)
[16681] => Array
(
[id] => 16681
[groupid] => 429
[name] => Nanotransistors
[quantity] => 2
)
)
If you want the keys in this output array to be sequential numbers for some reason, you can achieve that by using array_values($new_array) after the loop.

Foreach to get data from array in wordpress php

Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}

Fetch values from an array using nested foreach loop

I have a long array from which I wish to fetch all the values and store it in a separate variable, and store each value in database.
The array that I have is:
Array
(
[success] => 1
[categories] => Array
(
[0] => Array
(
[category_id] => 39
[name] => BAGS
[categories] => Array
(
[0] => Array
(
[category_id] => 59
[name] => Handcrafted Purses
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 45
[parent_id] => 39
[name] => Laptop Bag
[categories] =>
[status] => 1
)
)
[status] => 1
)
[1] => Array
(
[category_id] => 40
[name] => BOXERS
[categories] => Array
(
[0] => Array
(
[category_id] => 56
[parent_id] => 40
[name] => Women Boxers
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 91
[parent_id] => 0
[name] => Business Corporate
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 92
[parent_id] => 91
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 93
[parent_id] => 92
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 60
[parent_id] => 0
[name] => Business Corporates
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 90
[parent_id] => 60
[name] => Art Cushions
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 67
[parent_id] => 60
[name] => Bags
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 77
[parent_id] => 67
[name] => Potli Bags
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 76
[parent_id] => 67
[name] => Smart Bags
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[2] => Array
(
[category_id] => 86
[parent_id] => 60
[name] => Fashion Jewellery
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 88
[parent_id] => 86
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[3] => Array
(
[category_id] => 61
[parent_id] => 60
[name] => Men Footwear
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 65
[parent_id] => 61
[name] => Canvas Loafers
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 87
[parent_id] => 60
[name] => Shawls And Stoles
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 89
[parent_id] => 87
[name] => Coming Soon - Products Uploading
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
[status] => 1
)
[4] => Array
(
[category_id] => 15
[parent_id] => 0
[name] => ETHNIC WEAR
[image] =>
[categories] => Array
(
[0] => Array
(
[category_id] => 28
[parent_id] => 15
[name] => Designer Lehngas
[image] =>
[categories] =>
[status] => 1
)
[1] => Array
(
[category_id] => 2
[parent_id] => 15
[name] => Suits
[image] =>
[categories] =>
[status] => 1
)
)
[status] => 1
)
)
)
I am able to fetch the outer values of this array by using this code:
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
}
}
I got values as:
39
40
91
60
15
16
38
57
But I also wish to access the inner most values of the array. Can anyone tell how I can create a nested loop?
If you now how deep your array is you can just simply add other foreach-loops within your main loop.
if (!empty($array))
{
foreach ($array['categories'] as $category)
{
echo $category['category_id'];
echo "<br>";
if(isset($category['categories'])){
foreach($category['categories'] as $category2)
{
echo $category2['category_id'];
echo "<br>";
if (isset($category2['categories'])){
foreach($category2['categories'] as $category3)
{
echo $category3['category_id'];
echo "<br>";
...
} }
}}
}
}

php error accessing double layer array

My data is in a second tier of a return value that I am trying to access the individual elements. Array ( [success] => 1 [return] => Array ( [0] => Array
I tried ['return'] since it is the key of that, is what I need but got the same error or obvious worse.
error:
Array to string conversion in orders.php on line 10
code:
<?php
$id = $argv[1]; //variable for inbound
require_once('phpPlay.php');
$result = api_query("mytrades", array("marketid" => $id));
foreach( $result as $x) {
echo $x;
}
?>
data top 5 rows:
Array
(
[success] => 1
[return] => Array
(
[0] => Array
(
[tradeid] => 74038377
[tradetype] => Sell
[datetime] => 2014-11-12 16:05:32
[tradeprice] => 0.00675000
[quantity] => 22.18670000
[fee] => -0.00007488
[total] => 0.14976023
[initiate_ordertype] => Buy
[order_id] => 197009493
)
[1] => Array
(
[tradeid] => 73687280
[tradetype] => Buy
[datetime] => 2014-11-09 03:38:13
[tradeprice] => 0.00816988
[quantity] => 0.00100000
[fee] => 0.00000002
[total] => 0.00000817
[initiate_ordertype] => Buy
[order_id] => 194824864
)
[2] => Array
(
[tradeid] => 73684313
[tradetype] => Sell
[datetime] => 2014-11-09 02:57:41
[tradeprice] => 0.00808034
[quantity] => 0.00100000
[fee] => 0.00000000
[total] => 0.00000808
[initiate_ordertype] => Buy
[order_id] => 194803992
)
[3] => Array
(
[tradeid] => 73653019
[tradetype] => Sell
[datetime] => 2014-11-08 17:53:12
[tradeprice] => 0.00793991
[quantity] => 0.00010000
[fee] => 0.00000000
[total] => 0.00000079
[initiate_ordertype] => Buy
[order_id] => 194559503
)
[4] => Array
(
[tradeid] => 73652717
[tradetype] => Sell
[datetime] => 2014-11-08 17:50:13
[tradeprice] => 0.00793989
[quantity] => 0.00100000
[fee] => 0.00000002
[total] => 0.00000794
[initiate_ordertype] => Sell
[order_id] => 194559596
)
...
The problem is that with the echo $x statement in your code above, $x is an array, not a string.
The echo function requires a string, so the error you're getting is because PHP is automatically trying to convert the parameter passed to echo to a string, but it is failing because you are passing an array.
Try this:
if (isset($result['return'])) {
foreach($result['return'] as $result_item) {
echo(var_export($result_item, true));
}
} else {
echo 'No results';
}

how to get array like this?

I have one array in php like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] => People
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] => People
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] => Facility
)
)
I want to replace the duplicate type with blank. but the first one should have that type name and others should have blank. so the result array should be like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
I want array in this format only. and i am using PHP zend.
I search for this but most of them showing to remove that element from array. but i don't want to remove it. i want to replace it with blank but want to show the first one.
Tried Code
$result = array();
$result1 = array();
$result2 = array();
$y = array();
$y1 = array();
foreach ($data as $entry) {
$type= $entry["type"];
if (!isset($y[$type])) {
$y[$type] = array();
unset($entry["type"]);
$result[$type][] = $entry;
}
}
can anyone tell me how to do that ?
Code
$i = 0;
$found = 0;
foreach($data as $key=>&$val) {
if($i != 0) {
if($data[$i]['Type'] == $data[$found]['Type']) {
$data[$i]['Type'] = "";
}
else {
$found = $i;
}
}
$i++;
}
echo "<pre>";
print_r($data);
Output
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
Example

Categories