user1271208
user1271208

Reputation:

Determine the type of array element in php

I am calling a function that returns the an array. A for loop iteration gives the following output.

string(22) "text/xml;charset=UTF-8"

string(7) "chunked"

string(4) "gzip"

array(2) { ["Expect"]=> string(12) "100-continue" ["Content-Type"]=> string(48) "application/x-www-form-urlencoded; charset=utf-8" }

object(CFSimpleXML)#10 (1) { [0]=> string(6) "123456" }

How can i check whether an array element is an object or string ?

Upvotes: 4

Views: 12194

Answers (6)

Denis Ostrovsky
Denis Ostrovsky

Reputation: 569

You can get list of array elements types by this way

$types = array_map('gettype', $array);

Upvotes: 7

DhruvPathak
DhruvPathak

Reputation: 43235

You can use PHP gettype function, and then do required operations in a simple switch case code.

http://php.net/manual/en/function.gettype.php

see here : http://codepad.org/LRJcrKjJ

<?php

        $data = array(1, 1.,'hello', NULL, new stdClass);

        foreach($data as $item)
        {
          $currType = gettype($item);
          switch($currType){
          case "integer" :
            echo "I am integer ".$item." , double of me =  ".($item*2)."\n";
            break;
          case "string" :
            echo "I am string  ".$item." , reverse of me = ".strrev($item)."\n";
            break;
          default:
            echo "I am ".$currType ."\n" ;
            break;
          }

        }

    ?>

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88647

foreach ($array as $element) {
  if (is_array($element)) {
    // array
  } else if (is_string($element)) {
    // string
  } else if (is_int($element)) {
    // int
  } else if (is_float($element)) {
    // float
  } else if (is_bool($element)) {
    // bool
  } else if (is_object($element)) {
    // object
  } else if (is_resource($element)) {
    // resource
  } else {
    // null/invalid type (you could add an === NULL if you want, I suppose)
  }
}

There is also get_type() and the typeof operator, but since these return strings that may be subject to change in some future PHP version, the is_*() functions are more reliable.

Upvotes: 7

deed02392
deed02392

Reputation: 5022

I prefer the switch-trick solution:

foreach ($array as $element) {
    switch(true)
    {
        case is_array($element):
            // array
            break;
        case is_string($element):
            // string
            break;
        case is_int($element):
            // int
            break;
        case is_float($element):
            // float
            break;
        case is_bool($element):
            // bool
            break;
        case is_object($element):
            // object
            break;
        case is_resource($element):
            // resource
            break;
        default:
            // null
    }
}

Upvotes: 4

deceze
deceze

Reputation: 522091

if (is_object($arrayElement)) ...
if (is_array($arrayElement))  ...
if (is_string($arrayElement)) ...

Upvotes: 1

Related Questions