在PHP中创建多维数组的问题

时间:2014-12-26 00:01:34

标签: php arrays multidimensional-array

我想要什么

我想要一个看起来像这样的数组:

Array
(
    [item1] => Array
    (
        [0] => Array
            (
                [paid] => 500
                [nname] => Lastname
                [vname] => Firstname
                [mail] => mail@mail.com
            )
        [1] => Array
            (
                [paid] => 200
                [nname] => Lastname2
                [vname] => Firstname2
                [mail] => mail2@mail.com
            )

    )
    [item2] => Array
    (
        [0] => Array
            (
                [paid] => 100
                [nname] => Lastname3
                [vname] => Firstname3
                [mail] => mail3@mail.com
            )
)

这是一个数组,我想为一个项目多次付款。我希望将每个付款的所有详细信息保存为数组中的数组。

我有什么

$paidgift = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {              
                $nname = $row['nname'];
                $vname = $row['vname'];
                $paid = $row['paidprice'];
                $mail = $row['mail'];
                $gift = $row['title'];

                if (array_key_exists($gift, $paidgift)) {
                    //Item already exists in $paidgift
                    $paymentdetails = array('paid' => $paid,
                                            'nname' => $nname,
                                            'vname' => $vname,
                                            'mail' => $mail,    
                                            );
                    $paidgift[$gift][] = $paymentdetails;
                } else {
                    //Item doesn't exist in array $paidgift
                    $giftarray = array();
                    $paymentdetails = array('paid' => $paid,
                                            'nname' => $nname,
                                            'vname' => $vname,
                                            'mail' => $mail,    
                                            );

                    array_push($giftarray, $paymentdetails);
                    array_push($paidgift, $giftarray);
                }
}

我的代码

的结果

现在我坚持使用我的代码片段。问题是,我的代码没有将现有的商品付款粘贴到正确的数组中。此代码部分:$paidgift[$gift][] = $paymentdetails;

相反,它总是创建一个新的项目数组,并将paymentdetails数组推送到新创建的数组中。

希望这是了解我的问题的足够信息。请问是否有事情不清楚。

2 个答案:

答案 0 :(得分:1)

您不需要检查密钥是否存在,如果在这种情况下它不会被创建,请参阅例如此代码完全正常

 $data = array();
 $data['test'][] = array("name","price","testing");
 $data['test'][] = array("other name","price","testing");

 var_dump($data);

无需检查$data['test']是否存在。

所以你不需要上面的所有代码就足够了

 $paidgift = array();

 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // your data
    $data = array('paid' => $row['paidprice'],'nname' => $row['nname'],
                'vname' => $row['vname'], 'mail' => $row['mail']);
     // the gift
    $gift = $row['title'];
    // this is perfectly fine that's it
    $paidgift[$gift][] = $data;
 }

答案 1 :(得分:0)

这应该有效(未经测试)

如果存在具有键$gift(= title)的数组,则会将带有付款数据的数组推送到具有该键的数组中。

如果没有,则会在$paidgift(数组)中创建一个新条目,其key $gift用于将来存储。

$paidgift = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {              
                $gift = $row['title'];

                $paymentdetails = array('paid' => $row['paidprice'],
                                            'nname' => $row['nname'],
                                            'vname' => $row['vname'],
                                            'mail' => $row['mail']    
                                            );


                array_key_exists($gift, $paidgift) ? array_push($paidgift[$gift], $paymentdetails) : $paidgift[$gift] = array($paymentdetails);

}