Actually i want to add both the values of ids 2 and the ids 3

<form method="post" name="fo" action="#">
    <input type="text" name="ids[]" value="2"/>
    <input type="text" name="amount[]" value="100"/>
    <input type="text" name="ids[]" value="3"/>
    <input type="text" name="amount[]" value="200"/>
    <input type="text" name="ids[]" value="2"/>
    <input type="text" name="amount[]" value="300"/>
    <input type="text" name="ids[]" value="3"/>
    <input type="text" name="amount[]" value="50"/>
    <input type="submit" name="submit"/>
</form>

<?php
$id=$_POST['ids'];
$am=$_POST['amount'];

$arr=array(
           array('id'=> $id,'am'=>$am)

           );

$heads = array();
$amounts = array();
$i = 0;
foreach($arr as $sub)
{
    $key = array_search($sub['id'], $heads);
    if($key)
    {
        $amounts[$key] += $sub['am'];
    }
    else
    {
        $heads["'" . $i . "'"] = $sub['id'];
        $amounts["'" . $i . "'"] = $sub['am'];
        $i++;
    }
}

print_r($heads);
print_r($amounts);
}

?>

Dani AI

Generated

You want to group the submitted rows by ID and sum the amounts for duplicate IDs. was right that PHP already gives you arrays for fields named with [], and ’s flag about how the form is submitted is also important — make sure the POST actually reaches your processing script (check method and action, or handle POST on the same page with a request-method check).

A simple, robust server-side approach for parallel arrays (ids[] and amount[]) — validate lengths, cast numbers, then accumulate by id:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $idsList    = array_values($_POST['ids'] ?? []);
    $amountList = array_values($_POST['amount'] ?? []);
    $totals     = [];
    $limit      = min(count($idsList), count($amountList));

    for ($i = 0; $i < $limit; $i++) {
        $id  = trim((string)$idsList[$i]);
        if ($id === '') continue;
        $amt = floatval(str_replace(',', '', $amountList[$i]));
        $totals[$id] = ($totals[$id] ?? 0) + $amt;
    }

    print_r($totals); // keyed by id with summed amounts
}
?>

Alternatively, send a true multidimensional structure from the form and PHP will already give you an array of rows (often easier when adding/removing rows dynamically):

<!-- example inputs -->
<input name="items[0][id]"     value="2">
<input name="items[0][amount]" value="100">
<input name="items[1][id]"     value="3">
<input name="items[1][amount]" value="200">
<?php
$totals = [];
foreach ($_POST['items'] ?? [] as $row) {
    $id = trim((string)($row['id'] ?? ''));
    if ($id === '') continue;
    $amt = floatval($row['amount'] ?? 0);
    $totals[$id] = ($totals[$id] ?? 0) + $amt;
}
print_r($totals);
?>

Quick tips: always validate/sanitize input, handle mismatched counts, prefer integer cents for money to avoid float rounding, and use action="" or an explicit handler so the POST goes where you expect.

Recommended Answers

All 2 Replies

What are you trying to accomplish? When you say " Multidementional Array" from form, what are you doing with the array? The for post data is aready array.

Give your input fields names like id1, id2, id3 etc and amount 1, amount2, amount3, etc
THEN build your array by putting these values into their corresponding places in your array. You do this by having an action file for when the sumbit button is clicked.
Currently your form has no action, therefore no action will take place as the form input isn't being processed in any way. It's a dead form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.