【问题标题】:PHP create multi dimensional arrayPHP创建多维数组
【发布时间】:2023-03-16 19:14:01
【问题描述】:

我正在使用 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 数组没有添加两个数组,最后一项覆盖了第一个数组,我最终没有在 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++;

}

任何帮助都会很棒!谢谢

【问题讨论】:

  • 我正在查询我的数据库...并删除重复项修复您的查询语句,看看 group by
  • 只需将 $form = array(); 移到 for 循环上方而不是它内部

标签: php loops multidimensional-array foreach


【解决方案1】:

在您的代码中,数组$form 在每次迭代时都会被初始化。所以之前的数据是用空白数组重写的。

所以将数组初始化保持在你上面的 for 循环中,这样一开始它就会像这样初始化

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

}

这会起作用

【讨论】:

  • 不敢相信我把 $form 数组放在了循环中!愚蠢的错误!感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多