0

I have a query that returns either an object or an array based on the number of items. If there are zero or one items being returned, then it returns the item as an object. If there are two or more items, then it returns an array of objects.

I'm wanting to do a foreach on the query result, but that is throwing an error if it is an object that is returned, rather than an array. So I've created a function that would convert an object to an array.

function to_array($unknown) {
  if (count($unknown) === 0) {
    $array=array($unknown);
  } elseif (!is_array($unknown)) {
    $array = array();
    $array[0] = $unknown;
  } else {
    $array = $unknown;
  }
  return $array;
}

It can be used by simply calling $query_array = to_array($query_return).

Is this be best way to do this? Is there a better way? Might this create any problems?

6
  • 1
    why not change the query return results? Commented Jun 8, 2015 at 20:47
  • 1. Why don't you just always return an array, why? 2. Just simply cast it to an array?! $array = (array)$object; Commented Jun 8, 2015 at 20:47
  • This is silly. Change the function so that it always returns an array. Commented Jun 8, 2015 at 20:48
  • 2
    return is_object($unknown) ? array($unknown) : $unknown; Commented Jun 8, 2015 at 20:49
  • I am unable to return the return from the query. I'm using a SOAP-based call to an API. Commented Jun 8, 2015 at 20:50

2 Answers 2

2

Your way looks OK, however I suggest that you should check if $unknown is foreachable and convert it accordingly. Of course if you're doing this more than once then put it in a function.

<?php

$unknown = ...; // call to API

if (!is_array($unknown) && !($unknown instanceof \Traversable)) {
    $unknown = array($unknown);
}

foreach ($unknown as $item) {
    // ... do things
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm wanting to do a foreach on the query result, but that is throwing an error if it is an object that is returned, rather than an array.

It seems your data source is somewhat unclear or unclean. We are talking about queries, objects and arrays. Who knows what else is possible, the more your data source evolves. Therefore, I would check the data type first and handle all possibilities.

The gettype function gives you a good starting point. Then you can use a switch to handle all possibilities, even future PHP Modifications as below.

For the object to array conversion, I would use the get_object_vars function instead of a simple type cast, because it is unclear to me what that type cast really would do. But the get_object_vars function is well documented.

$type = gettype ( $unknown );
switch ($type) {
  case "array":
    $array = $unknown;
    break;
  case "object":
    $array = get_object_vars ( $unknown );
    break;
  case "boolean":
  case "integer":
  case "double": 
  case "string":
  case "resource":
  case "NULL":
  case "unknown type":
  default: throw new Exception("Unknown data type");
}

/* Do something here with $array */

Comments

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.