If the keys in the two arrays are the same, how do I add the values and create a new array?


Array keys are preserved
If the keys are the same add the values and create a single array with the same key
If the keys are different, merge the array keeping the keys values

//If the keys are the same, sum the values
$a = array('5'=>100);
$b = array('5'=>50);

// New array
$ab = array('5'=>150);

If the keys are different, concatenate keeping keys and values
$c = array('3'=>30);
$d = array('6'=>70);

//New array
$cd = array('6'=>70,'3'=>30);

Keys are product IDs, values product quantity
How can I do that


Where is this data coming from, what is the actual format of the input data, and what are you going to do with the result? Is this something like an add to shopping cart operation?

This is not a shopping cart but similar

F Connector selection field is available in two tables one below the other
Types of F connectors available

  1. Table
    RG6 U/4
    RG6 U/6
    Can choose any
    For Multiswitch and Headend use OR Disabled

  2. Table
    RG6 U/4
    RG6 U/6
    RG11 U/4
    RG11 U/6
    Can choose any
    For use on Main Line OR Disabled

  3. The amount is calculated automatically in the code for the product selected in the table.

  4. The amount is calculated automatically in the code for the product selected in the table.

If F Connector is selected and IDs are the same, add items
Merge two arrays if the IDs are different

The disable checkbox value="0"

That still doesn’t show the actual format and any naming of the submitted data.

Regardless of how many occurrences of each id, you are adding the submitted quantity per id to the resultant array -



	// define an empty cart
	$_SESSION['cart'] = [];

// examine the submitted data
echo '<pre>'; print_r($_POST); echo '</pre>';

// post method form processing
	// add to cart
	foreach($_POST['qty'] as $key=>$arr)
			// if item is not in the cart, create with a zero quantity
			$_SESSION['cart'][$key] = 0;
		// add the submitted quantity to whatever is already in the cart
		$_SESSION['cart'][$key] += array_sum($arr);

// display the cart contents
echo '<pre>'; print_r($_SESSION); echo '</pre>';

// form field naming -
<form method='post'>
<input type='text' name='qty[5][]' value='100'>
<input type='text' name='qty[5][]' value='50'>
<input type='text' name='qty[3][]' value='30'>
<input type='text' name='qty[4][]' value='70'>
<input type='submit'>

I am using radio button for selection
Ekran görüntüsü 2021-05-30 124740
To continue without selecting the product, it can be continued by disabling it with the checkbox.
Ekran görüntüsü 2021-05-30 124515

I was using json to move product id and quantity to last page
Now I learned that there is no need for it, it is possible to move it as name=“aaa[id]” value=“amount”

However, even if F Connector is a product in both tables, name="" must be different so that it can be selected.

Let me try this way

name[id] value=“amount” good to know that thank you
json was creating manually json_encode ($ _ POST [‘fconnector’]); it’s easier like that

Our main topic is your example does not work for me
I just explained it simply, I thought it would be easy with array_xxxx_xxx(), sorry

I cannot determine the f connector quantity selected in the F connector A table section on its own page.
Because I will calculate and determine the quantity for the selected ID on the last page according to the products to be selected on the next page.

I determine the amount of f connector selected in the F connector B table section on its own page. There is no problem here.

The question is:
The last page has this data
B of the table: b_fconnector {"5":"25"}
A of the table: a_fconnector 7
I calculated amount for a_fconnector and got 100 results
Add the amount of 100 for ID 7
If the IDs are the same, add the amounts and create an array or json
Merge strings if IDs are different
I will write to database as json

i think like this inexperienced
b_fconnector = json_decode(b_fconnector)
if(key(b_fconnector) == a_fconnector)
sum =
elseif(key(b_fconnector) != a_fconnector)
as ?


Sponsor our Newsletter | Privacy Policy | Terms of Service