#Can't check to see if array key has any value, anyone that can help?

1 messages · Page 1 of 1 (latest)

fringe thorn
#

You should read the #rules about sharing code, it should be wrapped inside ```php ```.

#

Not screenshots.

gray sparrow
#

Oke, momento please

#
// Sets al the invoices (drafts) in the database.
        $invoices = $this->GetInvoicesFromAPI();
        $count = 0;
        foreach ($invoices as $invoice) {
            if(!empty($invoices['details']) && !empty($invoices['contact']) ) {
                $count++;
                foreach ($invoice['details'] as $detail) {
                    $toevoeging = new Invoice();
                    $toevoeging->id = strval($invoice['id']);
                    $toevoeging->administration_id = strval($invoice['administration_id']);
                    $toevoeging->company_name = strval($invoice['contact']['company_name']);
                    $toevoeging->customer_id = strval($invoice['contact']['customer_id']);
                    $toevoeging->invoice_workflow_id = strval($invoice['contact']['invoice_workflow_id']);
                    $toevoeging->price = strval($detail['price']);
                    $toevoeging->amount = strval($detail['amount']);
                    $toevoeging->tax_rate_id = strval($detail['tax_rate_id']);
                    $toevoeging->description = strval($detail['description']);
                    $toevoeging->save();
                }
                echo "Factuur in database gezet uit moneybird, klantennummer: <b>" . $invoice['contact']['customer_id'] . "</b> ✅ ".$count."</br>";
            }
        }```
#
details: [
{
id: "377308226859829057",
administration_id: "342225896400225467",
tax_rate_id: "342225897800074538",
ledger_account_id: "342225896712701148",
project_id: null,
product_id: null,
amount: null,
amount_decimal: "1.0",
description: "*Hardware*",
price: "0.0",
period: null,
row_order: 0,
total_price_excl_tax_with_discount: "0.0",
total_price_excl_tax_with_discount_base: "0.0",
tax_report_reference: [
"NL/1a"
],
mandatory_tax_text: "",
created_at: "2023-01-18T13:39:47.045Z",
updated_at: "2023-01-18T13:39:47.045Z"
},
{
id: "377308226870314818",
administration_id: "342225896400225467",
tax_rate_id: "342225897800074538",
ledger_account_id: "342408295609271670",
project_id: null,
product_id: null,
amount: "1",
amount_decimal: "1.0",
description: "HP EliteDesk 800 G4 Mini
- Intel Core i5-8500
- 16GB DDR4-SDRAM
- 512GB NVMe SSD
- Intel® UHD Graphics 630
- Windows 10 Pro
_Refurbished_",
price: "439.0",
period: null,
row_order: 1,
total_price_excl_tax_with_discount: "439.0",
total_price_excl_tax_with_discount_base: "439.0",
tax_report_reference: [
"NL/1a"
],
mandatory_tax_text: null,
created_at: "2023-01-18T13:39:47.055Z",
updated_at: "2023-01-18T13:39:47.055Z"
},```
gray sparrow
#

Thats why i'd prefer hastebin

fringe thorn
#

What exactly are you looking to do then?

gray sparrow
#

To check if $invoice['details'] is not details: [ ]

#

Like checking if its empty

#

Since array_key_exists can't be done because there are more then 1 keys under details

novel pagoda
#

Maybe convert json to array and check if the details is_null?

fringe thorn
#

You can just check if its empty or even do a count and check if its zero.

gray sparrow
gray sparrow
fringe thorn
#

What does work about it? It reports false positive?

#

doesnt*

#

Try count then.

#
if(count($invoices['details']) && count($invoices['contact'])) {
novel pagoda
#

What's the output when you do dd($invoice);

#

Moneybird tends to mix objects and arrays together, been there and it was a struggle

#

It ain't pretty but I did it like this:

     $details = $invoice->details;

     //Kijken of er een referentie is meegegeven
     if (substr($details, 0, 1) == "["){
     }```
#

Or you could just do substr($invoice->details, 0, 1) == "["

gray sparrow
gray sparrow
novel pagoda
#

count($invoice['details']) should work

#

within the foreach loop that is

#

@gray sparrow Je gebruikt de verkeerde variabele bij de if- statement. Je moet $invoice['details'] hebben in plaats van $invoices['details']

gray sparrow
#

Ik zie het inderdaad..😂

#

Ook dat werkt alsnog niet met de code die ik had gestuurd of die werd verstuurd door het persoon hierin

#

Ik zou dat van jou maar eens moeten proberen denk ik

novel pagoda
#

Hmm weird

gray sparrow
#

Oh nee werkt nu!

#

🙂

#

Super

#

@fringe thorn @novel pagoda Thank u guys for the help, it works now!

fringe thorn
#

Glad one of the solutions worked for oyu.

rose goblet
#

@gray sparrow Why have you deleted the original post?

gray sparrow
#

What?

#

@rose goblet

rose goblet
gray sparrow
#

I think because it contained specific private date