Welli
Welli

Reputation: 1

XML to PHP Array?

I am trying to edit an open source PHP wrapper to export an XML.

Original:

$new_invoice = array(
    array(
        "Type"=>"ACCREC",
        "Contact" => array(
            "ContactID" => "[contact id]"
        ),
        "Date" => "2010-04-08",
        "DueDate" => "2010-04-30",
        "Status" => "SUBMITTED",
        "LineAmountTypes" => "Exclusive",
        "LineItems"=> array(
            "LineItem" => array(
                array(
                    "Description" => "Just another test invoice",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                )
            )
        )
    )
);

I've added another LineItem, so it become like this:

$new_invoice = array(
    array(
        "Type"=>"ACCREC",
        "Contact" => array(
            "ContactID" => "7937FF1D-B135-4BD0-A219-4B621EA3808C"
        ),
        "Date" => "2010-04-08",
        "DueDate" => "2010-04-30",
        "Status" => "DRAFT",
        "LineAmountTypes" => "Exclusive",
        "LineItems"=> array(
            "LineItem" => array(
                array(
                    "Description" => "Just another test invoice",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                )
            )
            "LineItem" => array(
                array(
                    "Description" => "Just another test invoice2",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                )
            )
        )
    )
);

but I got an error that said "expecting a closing bracket ) It seems that all brackets are there so I am confused.

Upvotes: 0

Views: 301

Answers (4)

Pankaj Prajapati
Pankaj Prajapati

Reputation: 96

Only Comma (,) is missing in your array after first "lineitem"

Upvotes: 0

John Flatness
John Flatness

Reputation: 33769

You've missed a comma after the first LineItem array.

Also, since your two arrays share the same key ("LineItem"), the second will overwrite the first, but that's unrelated to the syntax error.

Edit: To deal with that problem (here assuming that something like SimpleXML is in use):

"LineItems"=> array(
    "LineItem" => array(
        array(
            "Description" => "Just another test invoice",
            "Quantity" => "2.0000",
            "UnitAmount" => "250.00",
            "AccountCode" => "200"
        ),
        array(
            "Description" => "Just another test invoice2",
            "Quantity" => "2.0000",
            "UnitAmount" => "250.00",
            "AccountCode" => "200"
        )
    )
)

Upvotes: 1

Bob Baddeley
Bob Baddeley

Reputation: 2262

It's the missing comma, but like others said, the second one overwrites the first. Try this instead:

$new_invoice = array(
    array(
        "Type"=>"ACCREC",
        "Contact" => array(
            "ContactID" => "7937FF1D-B135-4BD0-A219-4B621EA3808C"
        ),
        "Date" => "2010-04-08",
        "DueDate" => "2010-04-30",
        "Status" => "DRAFT",
        "LineAmountTypes" => "Exclusive",
        "LineItems"=> array(
            "LineItem" => array(
                array(
                    "Description" => "Just another test invoice",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                ),
                array(
                    "Description" => "Just another test invoice2",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                )
            )
        )
    )
);
print_r($new_invoice);

Upvotes: 0

Garis M Suero
Garis M Suero

Reputation: 8169

You forgot a Comma... after the first line item...

your code should look like:

<?php 
$new_invoice = array(
    array(
        "Type"=>"ACCREC",
        "Contact" => array(
            "ContactID" => "7937FF1D-B135-4BD0-A219-4B621EA3808C"
        ),
        "Date" => "2010-04-08",
        "DueDate" => "2010-04-30",
        "Status" => "DRAFT",
        "LineAmountTypes" => "Exclusive",
        "LineItems"=> array(
            "0" => array(
                array(
                    "Description" => "Just another test invoice",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                )
            ),
            "1" => array(
                array(
                    "Description" => "Just another test invoice2",
                    "Quantity" => "2.0000",
                    "UnitAmount" => "250.00",
                    "AccountCode" => "200"
                )
            )
        )
    )
);
?>

Also your second line item is overriding the first one.

I will recommend a counter and be incrementing it after every lineitem is added...

Upvotes: 0

Related Questions