1

I'm working with an API that returns a numerically indexed array when there are multiple items, but not when there is a single item.

Here is what is returned when there are multiple items in the response.

array(
'LineItem' => array(
    (int) 0 => array(
        'Description' => 'Advanced SEO programme',
        'UnitAmount' => '90.00',
        'TaxType' => 'OUTPUT2',
        'TaxAmount' => '18.00',
        'LineAmount' => '90.00',
        'AccountCode' => '223',
        'Tracking' => array(
            'TrackingCategory' => array(
                'Name' => 'Client',
                'Option' => 'G&Ts World of Cards',
                'TrackingCategoryID' => 'ae873c49-da89-42a5-8955-d269c5bd2dcd'
            )
        ),
        'Quantity' => '1.0000'
    ),
    (int) 1 => array(
        'Description' => 'Advanced Links programme',
        'UnitAmount' => '125.00',
        'TaxType' => 'OUTPUT2',
        'TaxAmount' => '25.00',
        'LineAmount' => '125.00',
        'AccountCode' => '223',
        'Tracking' => array(
            'TrackingCategory' => array(
                'Name' => 'Client',
                'Option' => 'G&Ts World of Cards',
                'TrackingCategoryID' => 'ae873c49-da89-42a5-8955-d269c5bd2dcd'
            )
        ),
        'Quantity' => '1.0000'
    )
)

)

And here is what is returned when there is only a single item returned:

array(
'LineItem' => array(
    'Description' => 'Monthly office IT support (Email, file system and printer)',
    'UnitAmount' => '95.00',
    'TaxType' => 'OUTPUT2',
    'TaxAmount' => '19.00',
    'LineAmount' => '95.00',
    'AccountCode' => '221',
    'Tracking' => array(
        'TrackingCategory' => array(
            'Name' => 'Client',
            'Option' => 'Naturewood Flooring Company Ltd',
            'TrackingCategoryID' => 'ae873c49-da89-42a5-8955-d269c5bd2dcd'
        )
    ),
    'Quantity' => '1.0000'
)

)

This is what I want to do:

foreach ($response['LineItem'] as $line) {                            

                        $invoices['Lineitem'][] = array(
                            'description' => $line['Description'],
                            'amount' => $line['LineAmount'],
                            'account' => $line['AccountCode'],
                            'client' => $line['Tracking']['TrackingCategory']['Name']
                        );
                    }      

This isn't working when the single item is returned, I get an index not found error.

My current solution looks like this:

if(isset($response['LineItem'][0])){
                    foreach ($response['LineItem'] as $line) {

                        //debug($line);

                        $invoices['Lineitem'][] = array(
                            'description' => $line['Description'],
                            'amount' => $line['LineAmount'],
                            'account' => $line['AccountCode'],
                            'client' => $line['Tracking']['TrackingCategory']['Name']
                        );
                    }        
                } else {
                    $invoices['Lineitem'][] = array(
                        'description' => $response['LineItem']['Description'],
                        'amount' => $response['LineItem']['LineAmount'],
                        'account' => $response['LineItem']['AccountCode'],
                        'client' => $response['LineItem']['Tracking']['TrackingCategory']['Name']
                    );
                }

But I don't think this is the best way to go about it. Is there a better, cleaner way?

Kirk WSI
  • 173
  • 1
  • 1
  • 9

1 Answers1

0
if(!isset($response['LineItem'][0]))
    $response['LineItem'] = array($response['LineItem']);

And then proceed with a single foreach loop for both situations.

Also I recommend checking if the array is associative

Community
  • 1
  • 1
Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117