1

I am looking for a solution to handle an exception. In API I have, details about Orders are saved in arrays of objects: This in an example when there are at least two items:

Array
(
[0] => stdClass Object
    (
        [Cena] => 60,66
        [CenaJ] => 60,66
        [Czas] => 2017-07-10 08:01:42
        [FromAgency] => 1
        [Ilosc] => 1
        [ProdKod] => 2322501
        [RecNo] => 3163466
        [Status] => 
        [TowKod] => 205 658
        [TowNaz] => HAM.KLOCKI /P/CORSA 01-
        [Typ] => Z
        [VAT] => 23
        [WazneDni] => 5
    )

However, when there is only one item on the order it goes deeper, returns only an object, not an array:

stdClass Object
(
[Cena] => 60,66
[CenaJ] => 60,66
[Czas] => 2017-07-10 08:01:42
[FromAgency] => 1
[Ilosc] => 1
[ProdKod] => 2322501
[RecNo] => 3163466
[Status] => 
[TowKod] => 205 658
[TowNaz] => HAM.KLOCKI /P/CORSA 01-
[Typ] => Z
[VAT] => 23
[WazneDni] => 5
)

I am trying to extract a table of items in this order, and everything is great until there is more than 2 items. Here is the exmaple of my code, I use count() to count how many items are there, to create a loop for a specific numer of rows in the table:

$tablica_zam=($buforinfo->GetOrdersResult->BuforInfo);
print_r($buforinfo->GetOrdersResult->BuforInfo);
$count= count((array)$tablica_zam);
echo"</br> Ilość pozycji w buforze zamówień :";
print_r($count);
echo "</br>";
 $usun=array();
for ($i=0;$i<$count;$i++){
echo"<tr><td>";
print_r($buforinfo->GetOrdersResult->BuforInfo[$i]->ProdKod);
echo"</td><td>";
print_r($buforinfo->GetOrdersResult->BuforInfo[$i]->TowKod);
echo"</td><td>";
print_r($buforinfo->GetOrdersResult->BuforInfo[$i]->TowNaz);
echo"</td><td>";
print_r($buforinfo->GetOrdersResult->BuforInfo[$i]->Ilosc);
echo"</td><td>";

But it doesn't work when there is only one item, cause it is counting deeper items and returns 13, whole code has no application. Could you please give me some hints or solution on how to handle this exception?

Thanks in advance.

1 Answer 1

1

First off, you have a bunch of database wrapping code that you didn't disclose. The issue of whether you get an array of objects, or just an object is built into your libraries and database handling code. It seems like a bad design to me.

With that said, it is very easy to figure out what to do, given only the information you've provided.

  $tablica_zam=($buforinfo->GetOrdersResult->BuforInfo);
  if (is_array($tablica_zam)) {
    // An array of objects
    foreach ($tablica_zam as $row) {
        echo $row->ProdKod;
        // whatever else
    }
  } else {
    // It's one object apparently
    // Just pointing out that the object is the same- call it $row
    $row = $tablica_zam;
    echo $row->ProdKod;      

  }

You could also conform it if it's an object, so that it always appears as an array of objects:

$tablica_zam=($buforinfo->GetOrdersResult->BuforInfo);
if (is_object($tablica_zam)) {
    $tablica_zam = array($tablica_zam);
}
// Now the code works the same for either situation
Sign up to request clarification or add additional context in comments.

2 Comments

I recommend the second solution, so there's no code duplication.
I Agree with @Barmar

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.