PHP create multi dimensional array

Richard

I am querying my database with a LEFT JOIN to get the following array:

Array
(
    [invoice_number] => 000010
    [invoice_date] => 1432764000
    [country] => 115
    [fao] => Rick
    [company_name] => Chubbs
    [address_line_1] => kjhk
    [address_line_2] => jh
    [town] => kjh
    [postcode] => kjh
    [filename] => INV-1432820860.pdf
    [id] => 11
    [description] => dfgh
    [rates] => 2
    [quantity] => 3
    [price] => 6
    [created] => 0
    [country_name] => Kazakhstan
)
Array
(
    [invoice_number] => 000010
    [invoice_date] => 1432764000
    [country] => 115
    [fao] => Rick
    [company_name] => Chubbs
    [address_line_1] => kjhk
    [address_line_2] => jh
    [town] => kjh
    [postcode] => kjh
    [filename] => INV-1432820860.pdf
    [id] => 18
    [description] => biscuits
    [rates] => 2
    [quantity] => 3
    [price] => 6
    [created] => 0
    [country_name] => Kazakhstan
)

I want to loop through this and remove duplicates, so something like this:

Array
    (
        ['inv_details'] => array(
            [invoice_number] => 000010
            [invoice_date] => 1432764000
            [country] => 115
            [fao] => Rick
            [company_name] => Chubbs
            [address_line_1] => kjhk
            [address_line_2] => jh
            [town] => kjh
            [postcode] => kjh
            [filename] => INV-1432820860.pdf
        )
        ['items'] => array(
            [0] => array(
                [description] => dfgh
                [rates] => 2
                [quantity] => 3
                [price] => 6
                [created] => 0
            )
            [1] => array(
                [description] => biscuits
                [rates] => 2
                [quantity] => 3
                [price] => 6
                [created] => 0
            )

        )
    )

I have this code at the moment but the items array is not adding two arrays the last item ovewrites the first one and I dont end up with 2 arrays in items, just one:

$i = 0;

foreach($res->fetchAll(PDO::FETCH_ASSOC) as $row){

    $form = array();

    $form['title'] = $row['invoice_number'];
    $form['inv_details']['invoice_date'] = array('value'=>gmdate('d/m/Y', $row['invoice_date']), 'type'=>'date');
    $form['inv_details']['company_name'] = array('value' => $row['company_name'], 'type' => 'text');
    $form['inv_details']['fao'] = array('value' => $row['fao'], 'type' => 'text');
    $form['inv_details']['address_line_1'] = array('value' => $row['address_line_1'], 'type' => 'text');
    $form['inv_details']['address_line_2'] = array('value' => $row['address_line_2'], 'type' => 'text');
    $form['inv_details']['town'] = array('value' => $row['town'], 'type' => 'text');
    $form['inv_details']['postcode'] = array('value' => $row['postcode'], 'type' => 'text');
    //$form['inv_details']['countries'] = $this->crm_account_address->getCountries();
    $form['country'] = $row['country_name'];
    $form['country_id'] = $row['id'];

    $form['items'][$i]['description'] = $row['description'];
    $form['items'][$i]['rates'] = $row['rates'];
    $form['items'][$i]['quantity'] = $row['quantity'];
    $form['items'][$i]['price'] = $row['price'];

    //counter
    $i++;

}

Any help would be great! Thanks

Aman Rawat

In your code the array $form is initialized every time when iteration occurs. So the previous data is re writes with blank array.

So keep the array initialization in above you for loop so that it will initialize at first like this

$i = 0;

$form = array();    

foreach($res->fetchAll(PDO::FETCH_ASSOC) as $row){

    $form['title'] = $row['invoice_number'];
    $form['inv_details']['invoice_date'] = array('value'=>gmdate('d/m/Y', $row['invoice_date']), 'type'=>'date');
    $form['inv_details']['company_name'] = array('value' => $row['company_name'], 'type' => 'text');
    $form['inv_details']['fao'] = array('value' => $row['fao'], 'type' => 'text');
    $form['inv_details']['address_line_1'] = array('value' => $row['address_line_1'], 'type' => 'text');
    $form['inv_details']['address_line_2'] = array('value' => $row['address_line_2'], 'type' => 'text');
    $form['inv_details']['town'] = array('value' => $row['town'], 'type' => 'text');
    $form['inv_details']['postcode'] = array('value' => $row['postcode'], 'type' => 'text');
    //$form['inv_details']['countries'] = $this->crm_account_address->getCountries();
    $form['country'] = $row['country_name'];
    $form['country_id'] = $row['id'];

    $form['items'][$i]['description'] = $row['description'];
    $form['items'][$i]['rates'] = $row['rates'];
    $form['items'][$i]['quantity'] = $row['quantity'];
    $form['items'][$i]['price'] = $row['price'];

    //counter
    $i++;

}

This will work

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related