1
class A {
  public $a;
  public $b;

  function f1 () {
     // Code
  }
}

$obj = new A();

$arr = array ("a" => 1, "b" => 2);

foreach ($arr as $name => $value) {
  $obj->$name = $value;
}

return $obj;

I am not able to understand foreach section. How can we pass array as object and fetch that data as array?

6
  • 1
    Hi, this might help How foreach works Commented Feb 6, 2019 at 7:16
  • i know about foreach, but question is how can we store value for variable and returning that as object and that object behave as array? Commented Feb 6, 2019 at 7:22
  • "Pass array as object" where? Commented Feb 6, 2019 at 7:22
  • 1
    It's not a very good example of variable variables, it's using $arr (an array) to set the properties of $obj (an object) and returning the object. Commented Feb 6, 2019 at 7:25
  • return $obj; when we return $obj; it passes a array right? if i am wrong just explain what it passes. Commented Feb 6, 2019 at 7:31

2 Answers 2

2

Explanation of your foreach loop:

  1. Each loop you get key name of array and value of array element
  2. Than you set $obj property here $obj->$name = $value;
  3. $name is replaced with array key name therefore it looks like $obj->a = $value;

To pass array to object, use magic setter method:

class A {
  public $a;
  public $b;

  public function __set($property, $value) {
    if (property_exists($this, $property)) {
      $this->$property = $value;
    }

    return $this;
  }
}

$obj = new A();

$arr = array ("a" => 1, "b" => 2);

foreach ($arr as $name => $value) {
  $obj->__set($name, $value);
}

All of the object are as array automatically, so to fetch it back as array, you can loop object directly:

foreach ($obj as $key => $value) {
  print "$key => $value\n";
}

To get directly full array from object you can make another method which will do this for you:

public function getAsArray() {
   $arr = array();
   foreach ($this as $key => $value) {
       $arr[$key] = $value;
   }

   return $arr;
}
Sign up to request clarification or add additional context in comments.

4 Comments

my question was can we pass array into object and we can fetch that object as array despite of loop and methods
just want to understand this code, as i was not able to understand
Edited answer with explanation of your foreach loop.
If it helped you, please set my answer as correct one. Thank you!
1

You can please check below code.

class A {
  public $a;
  public $b;

  function f1 () {
     // Code
  }
}

$obj = new A();
$arr = array ("a" => 1, "b" => 2);
foreach ($arr as $name => $value) {
  $obj->$name = $value;
}

return $obj;
// This return obj output is :-
A Object
(
    [a] => 1
    [b] => 2
)

Update your code with below code your will get array.

return (array)$obj;
//Output 

Array
(
    [a] => 1
    [b] => 2
)

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.