0

I want to echo value from this array, so it'll say "true"

::object(stdClass)#25 (2) { ["Name"]=> string(3) "DPV" ["Value"]=> string(4) "true" } 

How can I do this in PHP?

koepitz
  • 5
  • 3

3 Answers3

0

Simply

<?php

echo $objectVarName->Value;

Note: $objectVarName is not an array, it is an object.

A very common example of arrays in PHP as following:

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);
Hossam
  • 1,126
  • 8
  • 19
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – John Conde Apr 19 '17 at 16:54
  • What about if there are 4 items in the array, and all 4 of them have values? – koepitz Apr 19 '17 at 16:56
  • First: this is not an array, this is an object. Second: then for each item/parameter, you'll have to `echo $objectVarName->parameterName;` or if this is just for debugging purpose, you can `var_dump($objectVarName);` or `print_r($objectVarName);` – Hossam Apr 19 '17 at 16:58
0

assuming the object property of Value is public ( which it is or it wouldn't be output ) you can use PHP's object operator ->

Such as:

echo $obj->Value;

In the case that it isn't public, you would want to use a get menthod, such as this

class obj{
      protected $Value = true;

      public function getValue(){
          return $this->Value;
      }
}

 $obj = new obj();
 echo $obj->getValue();

AS you can see both access the objects property the same basic way ( using the -> ). Which is different then how you access elements in an array. You can implement ArrayAccess on an object, which allows you to use it as if it was an array, but that's a post for another time.

http://php.net/manual/en/class.arrayaccess.php

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

Here is simple solution, might be helpful.

<?php
    //Let suppose you have this array
    $arr = array('id'=>1,'name'=>'alax','status'=>'true');

    //Check out array (Basically return array with key and value).
    print_r($arr);

    // Get value from the particular index
    echo "<br/>Your value is:".$arr['status'];
?>
Geee
  • 2,217
  • 15
  • 30