amilajack
amilajack

Reputation: 127

Array to string conversion

I'm trying to use a simple case statement to check the value of $item, which is returned as an array. The code returns the default case statement even though the array is equal to "iphones". Is there a simple way that I can get the value as a string?

This is the value of the array

object(stdClass)#209 (1) {
  ["item"]=>
  string(7) "iphones"
}

and this is my code

class ItemController extends BaseController {

        public function item ($item) {

        $item = DB::table('Catagories')->where('item', $item)->first();

        if (isset($item)) {

        switch ($item) {
              case "iphones":
                echo "iphone!";
                break;
              case "phones":
                echo "phones!";
                break;
              case "tablets":
                echo "tablets!";
                break;
              default:
              echo "string";
             }
          }
       }
    }  

Upvotes: 0

Views: 103

Answers (2)

ObiWanShanobi
ObiWanShanobi

Reputation: 442

Array elements can be converted to a string like such:

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

Documentation

Upvotes: 0

elixenide
elixenide

Reputation: 44851

That's not an array; it's an object. You can just access it like this:

$item->item

$item is an object with a property named item, so you use the syntax above.

Upvotes: 1

Related Questions