Cast Variable to Array in PHP



To cast variable to array, use the below syntax −

$yourNewVariableName=(array)$yourVariableName;

The PHP code is as follows −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $nameArray=Array('Mike','Sam','David');
   $valueArray=(array)$nameArray;
   print_r($valueArray);
?>
</body>
</html>

Output

Array ( [0] => Mike [1] => Sam [2] => David )
Updated on: 2020-11-20T05:35:37+05:30

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements