I have this variable: $pattern = "73,1,72";
//string in quotes
I want to create 3 new variables, first one should contain first value, second the second, and the third should contain the third.
So eventually should have these values:
$var1 == 73
$var2 == 1
$var3 == 72
How can I extract those numbers?
Use list()
and explode()
functions to achieve the desired result.
list($var1, $var2, $var3) = explode(",", $pattern);
Here's the demo, https://eval.in/836182
$parts= explode(",", $pattern);
$var1 = $parts[0];
$var2 = $parts[1];
$var3 = $parts[2];
<?php
$pattern = "73,1,72";
$strings = explode(",",$pattern));
$ints = array();
for($i=0;$<count($strings);$i++)
{
array_push($ints,intval($strings[$i]));
}
$var1 =$ints[0];
$var2 =$ints[1];
$var3 =$ints[2];
?>