PHP array types and values

0

How do I transform this type of data $pessoas->nome to $pessoas['nome'] and vice versa with php and why are they different?

    
asked by anonymous 20.08.2015 / 22:53

5 answers

6

You can change the typing using (array) .

$pessoas = new stdClass;
$pessoas->nome = 'Papa Charlie';
$pessoas = (array) $pessoas;


//output: Array( [nome] => Papa Charlie )
print_r( $pessoas );


// output: Papa Charlie
echo $pessoas['nome'];

The reverse process, changing the typing using (object) .

$pessoas = array( 'nome' => 'Papa Charlie' );
$pessoas = (object) $pessoas;


// output: stdClass Object( [nome] => Papa Charlie )
print_r( $pessoas );


// output: Papa Charlie
echo $pessoas-> nome;

See an example on Ideone .

    
20.08.2015 / 23:15
3

$pessoas->nome indicates that people are an object. Already $pessoas['nome'] indicates an array. You can transform the names present in objects into arrays (as long as they are not static) using the get_object_vars( object $object ) function.

Read more about objects in link

    
20.08.2015 / 22:58
2

With "setinha" you are referencing the attribute of an object, with the brackets, you are referencing the array key. To switch typing, you would either have to force typing or cast:

class Test {

public $valor = 'teste';

}

$objeto = new Test();

$array['valor'] = 'teste';

/* 1. a variável se torna um array $variavelArray['valor']
   2. O valor do atributo referenciado passará a ser
      tratado como array. */ 
$variavelArray  = (array) $objeto;
/* 1. a variável se torna um objeto $variavelObject->valor
   2. O valor do array passa a ser um atributo uma new StdClass; */
$variavelObject = (object) $array;

Here's an example: link

In many cases, it is necessary to do this conversion, but there are people who do this as a "gambiarra" to solve problems that could be handled in a much more elegant way.

    
20.08.2015 / 23:40
2

Array vs. Objects

This "setinha" in $pessoas->nome is about Object Separator . It is responsible for accessing members belonging to an object (either property or method).

In this first case $pessoas->nome , objeto could be of any class. In PHP, the default object is usually stdClass .

You can use var_dump in this case to know the name of the class from which the object comes from.

In the case of $pessoas['nome'] you are accessing the members of a array in PHP.

So, look at these examples to be able to fix better:

Example of Array

$array = array('nome' => 'wallace');

echo $array['nome']; // Imprime: 'wallace'

Example of objeto (stdClass);

$object = new stdClass;

$object->nome = 'Wallace';

echo $object->nome; // Imprime: 'wallace';

get_class($object); // Imprime: 'stdClass'

ArrayAccess interface

There are cases where you can use both shapes, both for objects and for arrays . This happens in any class that implements the ArrayAccess interface.

PHP defaults to a class that implements ArrayAccess , which is ArrayObject (see what a more demonstrative name!)

See:

$arr_and_obj = new ArrayObject();

$arr_and_obj->nome = 'wallace';

$arr_and_obj['idade'] = 25;

echo $arr_and_obj->nome; // Imprime: 'wallace';

echo $arr_and_obj['idade']; // Imprime: 25

In this case, only classes that implement this interface accept this "double access form". In other cases, an error will be generated:

See:

$obj = new stdClass;

$obj[1];

Output:

  

Can not use object of type stdClass as array

Conversion between types

As quoted in @IvanFerrer's answer, you can convert these types.

Object for array :

$obj = new stdClass;

$obj->nome = 'wallace';

var_dump((array)$obj);

Array for object:

$obj = (object) array('nome' => 'Wallace');

These two forms are called cast .

There is also another conversion, which is through the function settype .

See:

$object = new stdClass;

settype($object, 'array');

var_dump($object); // Imprime: Array(0){}
    
24.08.2015 / 17:58
-4

If you use the class PDO of php itself you will have the options when doing a query for example, you can use the:

PDO::FETCH_OBJ for object, in case it will be returned and access the attributes through $pessoas->nome or using PDO::FETCH_ASSOC returns in array form as $pessoas['nome'] .

Follow the link

    
21.08.2015 / 01:22