David Bélanger
David Bélanger

Reputation: 7438

Join multiple array together

I have a form who look like that :

<form method="POST">
    <p>Model 1</p>
        <input name="data_quantity[]">
        <input name="data_consignment[]">
        <input name="data_price[]">

    <p>Model 2</p>
        <input name="data_quantity[]">
        <input name="data_consignment[]">
        <input name="data_price[]">

    <p>Model 3</p>
        <input name="data_quantity[]">
        <input name="data_consignment[]">
        <input name="data_price[]">

    ...
</form>

Now. When I read the data on PHP, I have three array in the POST array ($_POST) : $_POST['data_quantity'], $_POST['data_consignment'], $_POST['data_price']...

I need to merge them together so I count the total in one array and I do a for and I recreate the array with the value in each array in the post. Quick example :

public function build_Lineitems($Quantity=array(), $Consignment=array(), $TransferFrom=array(), $TransferTo=array(), $Price=array(), $Attributes=array()){
    ...

    #   On va boucler sur un des 6 tableaux...
    $Lineitems = array();

    foreach($Quantity as $ModelID => $Lineitem){
        #   Entrée dans le tableau
        if(isset($Lineitems[$ModelID]) === FALSE){
            $Lineitems[$ModelID] = array();
        }

        #   Est-ce que c'est un tableau
        if(is_array($Lineitem) === TRUE){
            foreach($Lineitem as $Entry => $Clone){
                $Lineitems[$ModelID][] = array(
                    'qty'       => (isset($Quantity[$ModelID][$Entry]) === TRUE ? $Quantity[$ModelID][$Entry] : array()),
                    'con'       => (isset($Consignment[$ModelID][$Entry]) === TRUE ? $Consignment[$ModelID][$Entry] : array()),
                    'xferfrom'  => (isset($TransferFrom[$ModelID][$Entry]) === TRUE ? $TransferFrom[$ModelID][$Entry] : array()),
                    'xferto'    => (isset($TransferTo[$ModelID][$Entry]) === TRUE ? $TransferTo[$ModelID][$Entry] : array()),
                    'price'     => (isset($Price[$ModelID][$Entry]) === TRUE ? $Price[$ModelID][$Entry] : array()),
                    'attr'      => (isset($Attributes[$ModelID][$Entry]) === TRUE ? (empty($Attributes[$ModelID][$Entry]) === FALSE ? explode(',', $Attributes[$ModelID][$Entry]) : array()) : array())
                );
            }
        } else {
            #   Okay
            $Lineitems[$ModelID] = array(
                'qty'       => (isset($Quantity[$ModelID]) === TRUE ? $Quantity[$ModelID] : array()),
                'con'       => (isset($Consignment[$ModelID]) === TRUE ? $Consignment[$ModelID] : array()),
                'xferfrom'  => (isset($TransferFrom[$ModelID]) === TRUE ? $TransferFrom[$ModelID] : array()),
                'xferto'    => (isset($TransferTo[$ModelID]) === TRUE ? $TransferTo[$ModelID] : array()),
                'price'     => (isset($Price[$ModelID]) === TRUE ? $Price[$ModelID] : array()),
                'attr'      => array()
            );
        }
    }

    #   On renvoie
    return $Lineitems;
}

My question, is there a cleaner way to do this ? Thanks. (this: the merging part of multiple array.

Upvotes: 1

Views: 114

Answers (2)

Wolfgang Stengel
Wolfgang Stengel

Reputation: 2856

You can post the values multi-dimensional in the first place:

<input name="data[quantity][]">

Or in a different hierarchical order (while counting up $i):

<input name="data[<?php echo $i; ?>][quantity]">

Upvotes: 1

charly
charly

Reputation: 956

Have you thought about array_merge or its recursive brother that will merge everything inside too? Though you should be careful not to use it directly on $_POST not to get undesirable data

Upvotes: 0

Related Questions