2

What I want

I want an Array looking like this:

Array
(
    [item1] => Array
    (
        [0] => Array
            (
                [paid] => 500
                [nname] => Lastname
                [vname] => Firstname
                [mail] => [email protected]
            )
        [1] => Array
            (
                [paid] => 200
                [nname] => Lastname2
                [vname] => Firstname2
                [mail] => [email protected]
            )

    )
    [item2] => Array
    (
        [0] => Array
            (
                [paid] => 100
                [nname] => Lastname3
                [vname] => Firstname3
                [mail] => [email protected]
            )
)

It's an array where I have like multiple payments for one item. And I want to save all details from each payment as array into an array of the item.

What I have

$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);
                }
}

The result of my code

Now I stuck with my code snippet. The problem is, that my code doesn't paste an existing items payment into the right array. This code section: $paidgift[$gift][] = $paymentdetails;

Instead it always creates a new item array and pushes the paymentdetails array into the newly created array.

Hope this is enough information to understand my problem. Please ask if something isn't clear.

1
  • $paidgift[$gift][] this isn't going to work. You need to specify a key or use array_push() Commented Dec 26, 2014 at 0:04

2 Answers 2

1

You don't need to check if the key exists it will be created if it doesn't in this case, see for example this code is perfectly fine

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

 var_dump($data);

There was no need to check if $data['test'] exists.

So you don't need all of the code above this is simply enough

 $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;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

that's totally right! Thank you. you saved me some lines of code. It really works without testing if the item array already exists.
0

This should work (untested)

If an array with key $gift (=title) exists it pushes the array with payment data into the array with that key.

If not it creates a new entry in $paidgift (an array) with key $gift for future storing.

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

}

1 Comment

I should notice that title isn't a really good key. If your title are unique than you can mark this comment as never written. Else you need to supply unique ids.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.