PHP创建多维数组

时间:2015-05-29 11:17:34

标签: php loops multidimensional-array foreach

我使用LEFT JOIN查询我的数据库以获取以下数组:

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
)

我想循环遍历这个并删除重复项,所以像这样:

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
            )

        )
    )

我现在有这个代码,但是items数组没有添加两个数组,最后一个项目超过了第一个项目,我最终没有在项目中有2个数组,只有一个:

$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++;

}

任何帮助都会很棒!感谢

1 个答案:

答案 0 :(得分:0)

在代码中,每次迭代发生时都会初始化数组mysql> UPDATE users SET password = ''' WHERE username='admin'; '> '; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'admin'; '' at line 1 。所以以前的数据是用空数组重写。

所以保持数组初始化在你上面for循环,这样它最初会像这样初始化

$form

这将有效