PHP add values from two arrays - php

I have two php arrays that are in different orders. I want to find the correct array from array one and array two and add them together based on the values from currency, itemsSold and total. I also need to group them by currency and PaymentType.
First Array
array (
0 =>
array (
'currency' => 'USD',
'department' => 'Mens',
'itemsSold' => 4566,
'paymentType' => 'Cash;Card;Voucher',
'total' => 4800,
),
1 =>
array (
'currency' => 'USD',
'department' => 'womens',
'itemsSold' => 6221,
'paymentType' => 'Cash;Card;Voucher',
'total' => 9888,
),
2 =>
array (
'currency' => 'USD',
'department' => 'Home',
'itemsSold' => 20,
'paymentType' => 'Cash;Card;Voucher',
'total' => 17233,
),
3 =>
array (
'currency' => 'GBP',
'department' => 'Home',
'itemsSold' => 20,
'paymentType' => 'Voucher',
'total' => 677,
),
4 =>
array (
'currency' => 'USD',
'department' => 'Home',
'itemsSold' => 20,
'paymentType' => 'Voucher',
'total' => 17233,
)
)
Second Array
array (
0 =>
array (
'currency' => 'USD',
'department' => 'Mens',
'itemsSold' => 12,
'paymentType' => 'Cash;Card;Voucher',
'total' => 11,
),
1 =>
array (
'currency' => 'USD',
'department' => 'Home',
'itemsSold' => 20,
'paymentType' => 'Cash;Card;Voucher',
'total' => 500,
),
2 =>
array (
'currency' => 'USD',
'department' => 'womens',
'itemsSold' => 8,
'paymentType' => 'Cash;Card;Voucher',
'total' => 1277,
),
3 =>
array (
'currency' => 'GBP',
'department' => 'Home',
'itemsSold' => 20,
'paymentType' => 'Voucher',
'total' => 677,
),
4 =>
array (
'currency' => 'USD',
'department' => 'Home',
'itemsSold' => 40,
'paymentType' => 'Voucher',
'total' => 17233,
)
)
Desired Array
array (
0 =>
array (
'currency' => 'USD',
'department' => 'Mens',
'itemsSold' => 4578,
'paymentType' => 'Cash;Card;Voucher',
'total' => 4811,
),
1 =>
array (
'currency' => 'USD',
'department' => 'Home',
'itemsSold' => 40,
'paymentType' => 'Cash;Card;Voucher',
'total' => 1856,
),
2 =>
array (
'currency' => 'USD',
'department' => 'womens',
'itemsSold' => 6229,
'paymentType' => 'Cash;Card;Voucher',
'total' => 11165,
),
3 =>
array (
'currency' => 'GBP',
'department' => 'Home',
'itemsSold' => 40,
'paymentType' => 'Voucher',
'total' => 1354,
),
4 =>
array (
'currency' => 'USD',
'department' => 'Home',
'itemsSold' => 60,
'paymentType' => 'Voucher',
'total' => 34466,
)
As you can see, the values have been added together to give the total in a new array.
I have tried array_merge() but that just adds the arrays in each-other so the desired array is 6 items long, it doesn't add the values of the arrays together.
I tried looping foreach though first array and then using the index to find the items in the second array but the index changes as the arrays can be in any order.
I'm very new to programming and especially PHP, so I would appreciate a detailed answer that helps me learn it.
Thanks

Short version of code, $a1 and $a2 are your arrays:
$newarray = [];
// rebuild array so as key is unique value,
// in your case it is 'department':
foreach ($a1 as $item) {
$newarray[$item['department']] = $item;
// Or if you group by both currency and department:
$key = $item['department'] . $item['currency'];
$newarray[$key] = $item;
}
foreach ($a2 as $item) {
$department = $item['department'];
if (isset($newarray[$department])) {
$newarray[$department]['itemsSold'] += $item['itemsSold'];
$newarray[$department]['total'] += $item['total'];
}
// Or if you group by both currency and department:
$key = $item['department'] . $item['currency'];
if (isset($newarray[$key])) {
$newarray[$key]['itemsSold'] += $item['itemsSold'];
$newarray[$key]['total'] += $item['total'];
}
}
$newarray = array_values($newarray);
print_r($newarray);
Full fiddle here.

Related

iterate over 2 arrays in PHP to display items with same account id

I have two classes containing informations about accounts and invoices like this
The accounts list
array (
0 =>
'id' => 1,
'username' => 'abc',
'company' => 'My Corporation',
)),
1 =>
'id' => 2,
'username' => 'cde',
'company' => 'My Company',
))
The invoice list
array (
0 =>
'account_id' => 1,
'invoiceId' => '15',
)),
1 =>
'id' => 2,
'account_id' => '2',
'invoiceId' => '17',
)),
2 =>
'account_id' => 1,
'invoiceId' => '20',
)),
3 =>
'id' => 2,
'account_id' => '2',
'invoiceId' => '30',
))
When iterating over both collections each invoice to be shown once. My issues is that everything will be duplicated
My code is
<?php
foreach ($invoices as $invoice) {
foreach($accounts as $account) {
echo $account->getUsername($invoice->getAccountId())."<BR>";
echo $account->getCompany($invoice->getAccountId());
}
}
I am not sure what I should do
I think this sould solve tour problem:
<?php
$accounts = array (
0 => array (
'id' => 1,
'username' => 'abc',
'company' => 'My Corporation',
),
1 => array (
'id' => 2,
'username' => 'cde',
'company' => 'My Company',
));
$invoices = array (
0 => array (
'account_id' => 1,
'invoiceId' => '15',
),
1 => array (
'id' => 2,
'account_id' => '2',
'invoiceId' => '17',
),
2 => array (
'account_id' => 1,
'invoiceId' => '20',
),
3 => array (
'id' => 2,
'account_id' => '2',
'invoiceId' => '30',
));
foreach($accounts as $account) {
foreach ($invoices as $invoice) {
echo "account id : ";
echo $account['id'];
echo "invoices: ";
if ( $invoice['account_id'] === $account['id'] ) {
echo $invoice['account_id'];
// echo $account->getUsername($invoice->getAccountId())."<BR>";
// echo $account->getCompany($invoice->getAccountId());
}
}
}

Issue with Unset (PHP)

I am trying to unset an item from session array, it unset once then brings back again.
for exg :-
if i click to remove itemId = 5, it removes from session array,
then i click to remove itemId = 6, then i see the old itemId = 5 is back in array again.
/*** delete item from cart ***/
public function deleteCartItem($id, $customerId, $cartId, $wishlistFlag=false) // Param: ProductID, CustomerID, CartID, WishlistFlag
{
$conn = $_SESSION['conn'];
$itemId = false;
$chk = array();
$sql = "DELETE FROM cart_items WHERE product_id=? AND cart_id=?";
$stmt = mysqli_prepare($conn, $sql);//$result = $conn->query($sql);
$stmt->bind_param('ii', $item['id'],$cartId);
$stmt->execute();
$itemId = $i;
echo $itemId;
$chk[] = $_SESSION['cart'];
echo'</br>';
print_r($chk);
}
}
if (isset($_SESSION['cart'][$itemId])){
unset($_SESSION['cart'][$itemId]);
}
}
<?php
$_SESSION['cart'] = array (
0 =>
array (
'id' => 6154353459,
'name' => 'pro one Night Out Mesh Tee',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '28.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'XX-Large',
),
'option80' => 18,
'option125' => 3457,
'child_product_id' => 61535897,
'giftcard' => '',
'whole_preorder' => 0,
'preorder_shipping_date' => NULL,
'available_qty' => 5,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '65315459',
'product_id' => 'P19335935',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Tee',
'brand' => 'Widow',
'price' => 28.0,
'special_price' => NULL,
'regular_price' => 28.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Mesh Shirt',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493660',
'child_stock_quantity' => 1,
'size' => 'XX-Large',
'swatch' => 'BLACK',
),
),
5 =>
array (
'id' => 615345465,
'name' => 'pro one Night Out Mesh Leggings',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '30.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Medium',
),
'option80' => 18,
'option125' => 3454,
'child_product_id' => 6315890,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 37,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '64334515465',
'product_id' => 'P193936',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Leggings',
'brand' => 'Widow',
'price' => 30.0,
'special_price' => NULL,
'regular_price' => 30.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Printed Leggings',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493664',
'child_stock_quantity' => 10,
'size' => 'Medium',
'swatch' => 'BLACK',
),
),
2 =>
array (
'id' => 615445348,
'name' => 'pro two My Lashes Sunglasses',
'display_brand' => '',
'qty' => 1,
'price' => '15.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'ONE SIZE',
),
'option80' => 18,
'option125' => 6212,
'child_product_id' => 6154345358,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 295,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '6153453448',
'product_id' => 'P19645642180',
'quantity' => 1,
'name' => 'pro two My Lashes Sunglasses',
'brand' => '',
'price' => 15.0,
'special_price' => NULL,
'regular_price' => 15.0,
'size_type' => 'Regular',
'manufacturer' => 'CIEL',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Cat Eye Sunglasses',
'category' => 'Accessories,Sunglasses,Cat Eye Sunglasses',
'special_category' => 'What\'s New,Char Test Category,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S486477',
'child_stock_quantity' => 10,
'size' => 'ONE SIZE',
'swatch' => 'BLACK',
),
),
3 =>
array (
'id' => 61465645461,
'name' => 'pro one Night Out goodie Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '45.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'X-Large',
),
'option80' => 18,
'option125' => 3456,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 12,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '616465461',
'product_id' => 'P1935446934',
'quantity' => 1,
'name' => 'pro one Night Out goodie Dress',
'brand' => 'Widow',
'price' => 45.0,
'special_price' => NULL,
'regular_price' => 45.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'goodie Dresses',
'category' => 'Clothing,Dresses,Mini,goodie Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493652',
'child_stock_quantity' => 10,
'size' => 'X-Large',
'swatch' => 'BLACK',
),
),
4 =>
array (
'id' => 6154566225,
'name' => 'Feelings Maxi Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '58.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Large',
),
'option80' => 18,
'option125' => 3455,
'child_product_id' => 61456455677,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 30,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '614565225',
'product_id' => 'P1945652050',
'quantity' => 1,
'name' => 'Phantom Feelings Maxi Dress',
'brand' => 'Widow',
'price' => 58.0,
'special_price' => NULL,
'regular_price' => 58.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Maxi Dresses',
'category' => 'Dresses,Midi & Maxi,Maxi Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S485794',
'child_stock_quantity' => 10,
'size' => 'Large',
'swatch' => 'BLACK',
),
)
);
$itemId = 5; // comes dybamically;
if ( isset($_SESSION['cart'][$itemId])) {
unset($_SESSION['cart'][$itemId]);
}
?>
I am not getting it,
Any thoughts ?
You can also take a look at the sandbox- http://sandbox.onlinephpfunctions.com/code/6bab394d1da39a301ce82b974022da7b7efaface
Please refer to the whole code here - PHP SESSION UPDATE ISSUE
Thankyou
You're proably re-reading session-data so that deletion you're making is overridden.
<?php
$cart = $_SESSION['cart'];
unset($cart[5]);
echo 'deletion confirmed';
//later in execution..
//accidental re-read of cart from session-variable.
//BAD CODE STARTS HERE
$cart = $_SESSION['cart']; //error since you're re-reading cart from session.
//BAD CODE END
//calc totals in basket or something..
$totals = 0;
foreach($cart as $item) {
$totals = $totals + $item['qty'] * $item['price'];
}
//somewhere in the script, the session variable is updated.
$_SESSION['cart'] = $cart;
GOOD TO KNOW
PHP passes most variables by value as default, meaning that
$cart = $_SESSION['cart'];
copies the content of $_SESSION['cart'] into $cart.
Any change made to $cart will not automatically be updated in $_SESSION['cart'].
If you don't want a copy but a reference, you may use:
$cart = &$_SESSION['cart'];
and you will not need to save $cart into $_SESSION.
(Just make sure $_SESSION['cart'] is initialized when making the reference)
** AND **
Session variables tend to grow to a mess.. Use getters and setters in your application.
The best approach is to after unset get the updated array from session and declare it again
$cart_array = array (
0 =>
array (
'id' => 6154353459,
'name' => 'pro one Night Out Mesh Tee',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '28.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'XX-Large',
),
'option80' => 18,
'option125' => 3457,
'child_product_id' => 61535897,
'giftcard' => '',
'whole_preorder' => 0,
'preorder_shipping_date' => NULL,
'available_qty' => 5,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '65315459',
'product_id' => 'P19335935',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Tee',
'brand' => 'Widow',
'price' => 28.0,
'special_price' => NULL,
'regular_price' => 28.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Mesh Shirt',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493660',
'child_stock_quantity' => 1,
'size' => 'XX-Large',
'swatch' => 'BLACK',
),
),
5 =>
array (
'id' => 615345465,
'name' => 'pro one Night Out Mesh Leggings',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '30.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Medium',
),
'option80' => 18,
'option125' => 3454,
'child_product_id' => 6315890,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 37,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '64334515465',
'product_id' => 'P193936',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Leggings',
'brand' => 'Widow',
'price' => 30.0,
'special_price' => NULL,
'regular_price' => 30.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Printed Leggings',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493664',
'child_stock_quantity' => 10,
'size' => 'Medium',
'swatch' => 'BLACK',
),
),
2 =>
array (
'id' => 615445348,
'name' => 'pro two My Lashes Sunglasses',
'display_brand' => '',
'qty' => 1,
'price' => '15.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'ONE SIZE',
),
'option80' => 18,
'option125' => 6212,
'child_product_id' => 6154345358,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 295,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '6153453448',
'product_id' => 'P19645642180',
'quantity' => 1,
'name' => 'pro two My Lashes Sunglasses',
'brand' => '',
'price' => 15.0,
'special_price' => NULL,
'regular_price' => 15.0,
'size_type' => 'Regular',
'manufacturer' => 'CIEL',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Cat Eye Sunglasses',
'category' => 'Accessories,Sunglasses,Cat Eye Sunglasses',
'special_category' => 'What\'s New,Char Test Category,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S486477',
'child_stock_quantity' => 10,
'size' => 'ONE SIZE',
'swatch' => 'BLACK',
),
),
3 =>
array (
'id' => 61465645461,
'name' => 'pro one Night Out goodie Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '45.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'X-Large',
),
'option80' => 18,
'option125' => 3456,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 12,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '616465461',
'product_id' => 'P1935446934',
'quantity' => 1,
'name' => 'pro one Night Out goodie Dress',
'brand' => 'Widow',
'price' => 45.0,
'special_price' => NULL,
'regular_price' => 45.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'goodie Dresses',
'category' => 'Clothing,Dresses,Mini,goodie Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493652',
'child_stock_quantity' => 10,
'size' => 'X-Large',
'swatch' => 'BLACK',
),
),
4 =>
array (
'id' => 6154566225,
'name' => 'Feelings Maxi Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '58.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Large',
),
'option80' => 18,
'option125' => 3455,
'child_product_id' => 61456455677,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 30,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '614565225',
'product_id' => 'P1945652050',
'quantity' => 1,
'name' => 'Phantom Feelings Maxi Dress',
'brand' => 'Widow',
'price' => 58.0,
'special_price' => NULL,
'regular_price' => 58.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Maxi Dresses',
'category' => 'Dresses,Midi & Maxi,Maxi Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S485794',
'child_stock_quantity' => 10,
'size' => 'Large',
'swatch' => 'BLACK',
),
)
);
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = $cart_array;
} else {
$_SESSION['cart'] = (array) $_SESSION['cart'];
}
$itemId = 3;
if (isset($_SESSION['cart'][$itemId])) {
unset($_SESSION['cart'][$itemId]);
}
print_r($_SESSION['cart']);
Now it will be not back the remove item again, unless if you destroy the session
Why is there $item['id'] used in $stmt->bind_param( (instead of $id?). $item is not a parameter of the function.
Similarly, where does the variable $i come from in $itemId = $i;?
I have written a simplified version of your problem and it works as expected. It uses a form and buttons to give the array indices to the PHP script. If you have a different setup (like using Ajax) you need to clarify that.
session_start();
if (!isset($_SESSION['cart']) || sizeof($_SESSION['cart']) === 0)
{
echo('Resetting cart.' . PHP_EOL);
$_SESSION['cart'] = [0 =>'zero', 1 =>'one', 2 =>'two', 3 =>'three', 4 =>'four'];
}
echo('<pre>before ' . print_r($_SESSION['cart'], TRUE) . '</pre>');
$itemId = isset($_GET['itemid']) ? $_GET['itemid'] : FALSE;
if ($itemId !== FALSE)
{
if (isset($_SESSION['cart'][$itemId]))
{
echo('Unsetting »' . $itemId . '«.' . PHP_EOL);
unset($_SESSION['cart'][$itemId]);
}
else
echo('»' . $itemId . '« not in cart.' . PHP_EOL);
}
else
echo('No item ID given.' . PHP_EOL);
echo('<pre>after ' . print_r($_SESSION['cart'], TRUE) . '</pre>');
echo('<form method="get" action="' . $_SERVER['PHP_SELF'] . '">' . PHP_EOL);
foreach ($_SESSION['cart'] as $key=>$value)
echo('<button name="itemid" value="' . $key . '">' . $value . '</button>' . PHP_EOL);
echo('</form>' . PHP_EOL);
The manual has made it clear
If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
To unset() a global variable inside of a function, then use the $GLOBALS array to do so:
<?php
function foo()
{
unset($GLOBALS['bar']);
}
$bar = "something";
foo();
?>
$stmt->bind_param('ii', $item['id'],$cartId);
$stmt->execute();
where is $item['id'] from?
Try using different variable names on your bind_param and declare their value before query execute() method.

Tell PayPal to automatically process the monthly payment

ref: https://stackoverflow.com/a/25730860/2735734
How to Tell PayPal to automatically process the monthly payment?
UPDATE 1
here is the PayPal Reponses:
CreateAgreement result:
array (
'name' => 'my name',
'description' => 'my description',
'plan' =>
array (
'id' => 'P-95307423V8719480UI4T4SGG',
'state' => 'ACTIVE',
'name' => 'title here',
'description' => 'description here',
'type' => 'INFINITE',
'payment_definitions' =>
array (
0 =>
array (
'id' => 'PD-140035022V340531AI4T4SGG',
'name' => 'Regular Payments',
'type' => 'REGULAR',
'frequency' => 'Month',
'amount' =>
array (
'currency' => 'USD',
'value' => '15.5',
),
'cycles' => '0',
'charge_models' =>
array (
0 =>
array (
'id' => 'CHM-9G272533RU378412KI4T4SGG',
'type' => 'TAX',
'amount' =>
array (
'currency' => 'USD',
'value' => '1',
),
),
),
'frequency_interval' => '1',
),
),
'merchant_preferences' =>
array (
'setup_fee' =>
array (
'currency' => 'USD',
'value' => '0',
),
'max_fail_attempts' => '0',
'return_url' => 'http://example.com/ok',
'cancel_url' => 'http://example.com/cancel',
'auto_bill_amount' => 'YES',
'initial_fail_amount_action' => 'CONTINUE',
),
),
'links' =>
array (
0 =>
array (
'href' => 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-4N736816FP8632455',
'rel' => 'approval_url',
'method' => 'REDIRECT',
),
1 =>
array (
'href' => 'https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-4N736816FP8632455/agreement-execute',
'rel' => 'execute',
'method' => 'POST',
),
),
'start_date' => '2014-10-25T11:54:20-00:00',
)
AND after the client approved.
ExecuteAgreement result:
array (
'id' => 'I-CD3VD66KJKXX',
'state' => 'Active',
'description' => 'my description',
'plan' =>
array (
'payment_definitions' =>
array (
0 =>
array (
'type' => 'REGULAR',
'frequency' => 'Month',
'amount' =>
array (
'currency' => 'USD',
'value' => '15.50',
),
'cycles' => '0',
'charge_models' =>
array (
0 =>
array (
'type' => 'TAX',
'amount' =>
array (
'currency' => 'USD',
'value' => '1.00',
),
),
1 =>
array (
'type' => 'SHIPPING',
'amount' =>
array (
'currency' => 'USD',
'value' => '0.00',
),
),
),
'frequency_interval' => '1',
),
),
'merchant_preferences' =>
array (
'setup_fee' =>
array (
'currency' => 'USD',
'value' => '0.00',
),
'max_fail_attempts' => '0',
'auto_bill_amount' => 'YES',
),
),
'links' =>
array (
0 =>
array (
'href' => 'https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-CD3VD66KJKXX/suspend',
'rel' => 'suspend',
'method' => 'POST',
),
1 =>
array (
'href' => 'https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-CD3VD66KJKXX/re-activate',
'rel' => 're_activate',
'method' => 'POST',
),
2 =>
array (
'href' => 'https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-CD3VD66KJKXX/cancel',
'rel' => 'cancel',
'method' => 'POST',
),
3 =>
array (
'href' => 'https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-CD3VD66KJKXX/bill-balance',
'rel' => 'self',
'method' => 'POST',
),
4 =>
array (
'href' => 'https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-CD3VD66KJKXX/set-balance',
'rel' => 'self',
'method' => 'POST',
),
),
'start_date' => '2014-10-25T07:00:00Z',
'agreement_details' =>
array (
'outstanding_balance' =>
array (
'currency' => 'USD',
'value' => '0.00',
),
'cycles_remaining' => '0',
'cycles_completed' => '0',
'next_billing_date' => '2014-10-25T10:00:00Z',
'final_payment_date' => '1970-01-01T00:00:00Z',
'failed_payment_count' => '0',
),
)
note the start_date for the first reponse to the second...
The first payement isn't made... (And I don't know for the other, I've try today with a daily payement... I have to wait now...)
In the Pre-Approved area on the Sandbox Account, is in Active and inform about next billing correctly.
Once the user completes and submits subscription purchase page you have created, the subscription will begin and will automatically rebill the customer's account on whatever interval you have selected. You do not have to do anything.
Updated: The REST API also automatically processes subscriptions. You do not have to resubmit monthly. The PayPal sandbox was updated in July of this year to enable you to test and process subscriptions. There are no issues being reported in the dev community about this not working, so I suspect it is something on your side.
If you haven't already. please review this:
https://developer.paypal.com/docs/integration/direct/test-the-api/
There are billing plan and billing subscription sections. If you still have a problem, post your billing plan code and the response you are receiving.

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

How to get all the specific key value from multidimensional array in php?

How can I get the all the 'name' and 'city' value back in an array from the following multidimensional array?
$myarray=Array(Array('name' => 'A','id' => '1', 'phone' => '416-23-55',
Base => Array ('city' => 'toronto'),'EBase' => Array('city' => 'North York'),
'Qty' => '1'), (Array('name' =>'B','id' => '1','phone' => '416-53-66','Base' =>
Array ('city' => 'qing'), 'EBase' => Array('city' => 'chong'),'Qty' => '2')));
I expect the returned value be
$namearray=Array('A','B');
$basecityarray=Array('toronto','qing');
$Ebasecityarray=Array('North York','chong');
Thank you!
You can definitely try something like this:
$shop = array( array( 'Title' => "rose",
'Price' => 1.25,
'Number' => 15
),
array( 'Title' => "daisy",
'Price' => 0.75,
'Number' => 25,
),
array( 'Title' => "orchid",
'Price' => 1.15,
'Number' => 7
)
);
You can access the first row as
echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
or $shop[0]->Title will return to you rose.

Categories