0

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";
             }
          }
       }
    }  
1
  • Please post '$item' array format. Commented Jul 19, 2014 at 4:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! And I have another question. If I'm passing a view with an object attached, like so... 'return View::make('general.buy') ->with('item', $item);' , what is the first parameter of the with() method?
I'm not sure what your other question is, but you should ask it as a new question, using the "Ask Question" button at the top of the page. You can link to this question if it helps provide context.
0

Array elements can be converted to a string like such:

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

Documentation

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.