php convert array to object

An array can be converted to an object in PHP.

<?php

$a=array("aa","bb");

$a=(object)$a

?>

However you can not access the member of $a because there is not a name for the member of converted object. If you convert an array of  type key=>value (so called associative array) to an object, you will be able to access its members(properties) by their names which are the keys for the original array.

<?php

$a=array("aaname"=>"aa","bbname"=>"bb");

$a=(object)$a;

print($a->aaname);

?>

Note that the converted object is of class stdClass.

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply