I converted a two-dimensional array to an object in PHP. How to access the values?

Asked

Viewed 119 times

0

Hello, I have the following matrix:

$array[0]["nome"] = "nome exemplo";
$array[0]["idade"] = "idade exemplo";
$array[1]["nome"] = "nome exemplo 2";
$array[0]["idade"] = "idade exemplo 2";

I created an object from that matrix:

$obj = (object) $array;

I would like to know how to access these values with this object, when the matrix has only one dimension I simply do $obj->name, but and when it has two dimensions?

  • Converting an array that is not associative to an object is not a good idea. If it was an associative array, just call the properties, in sequence. Here is a response where several examples are presented, and suggestions of how to proceed in this case. http://stackoverflow.com/questions/10333016/how-to-access-object-properties-with-names-like-integers

  • You can even define numerical properties on an object but you can’t access them this is the problem, you need to change the way to address the problem.

2 answers

1

As @rray said in their comment

You can even define numeric properties on an object but you can’t access them this is the problem.

Using the functionality of get_object_vars that is very clear:

var_dump(get_object_vars($obj));
array(0) {
}

In other words, it did not generate any attribute in the class, but just doing :

var_dump($obj);
object(stdClass)#48 (2) {
  [0]=>
  array(2) {
    ["nome"]=>
    string(12) "nome exemplo"
    ["idade"]=>
    string(13) "idade exemplo"
  }
  [1]=>
  array(2) {
    ["nome"]=>
    string(14) "nome exemplo 2"
    ["idade"]=>
    string(15) "idade exemplo 2"
  }
}

Possible solution

$array[0]["nome"] = "nome exemplo";
$array[0]["idade"] = "idade exemplo";
$array[1]["nome"] = "nome exemplo 2";
$array[1]["idade"] = "idade exemplo 2";

foreach ($array as $k => $attributes){
    foreach ($attributes as $attribute => $value) {
        $array[$attribute][$k] = $value;
        unset($array[$k]);
    }
}

$obj = (object) $array;

This way you will be inverting the order and generating an associative array. Which in the conversion generates attributes in the class.

var_dump(get_object_vars($obj));
array(2) {
  ["nome"]=>
  array(2) {
    [0]=>
    string(12) "nome exemplo"
    [1]=>
    string(14) "nome exemplo 2"
  }
  ["idade"]=>
  array(2) {
    [0]=>
    string(13) "idade exemplo"
    [1]=>
    string(15) "idade exemplo 2"
  }
}

However as you can notice it doesn’t make much sense to make this conversion since as you have several name values you will still have an array.

0

There is not a good reason to make this conversion you want, but to be able to capture the elements of cast of the array, it is necessary to convert it to an accessible element, one of the ways to do this is by using the json_encode() and json_decode(), there are also other ways to do this process, for example: $data = get_object_vars($obj); and extract($data);, a priori, for what you need, the bass example already works:

$array = array();
$array[0]["nome"] = "nome exemplo";
$array[0]["idade"] = "idade exemplo";
$array[1]["nome"] = "nome exemplo 2";
$array[0]["idade"] = "idade exemplo 2";

$obj = (object) $array;
$extractObj = json_decode(json_encode($obj));

echo $extractObj->{0}->nome;

To see the structure of your object, use the function var_dump($obj).

See this demo on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.