Sometimes we have a requirement that to display all array values through array named indexes, for example
$automobile = array(
"type" => "car",
"color"=> "red",
"cost" => "$10000"
);
If you want to show the values of array with the respective named index we try foreach most but try this now onwards
Now we can display the values as
print $type; //car
print $color; //red
print $cost; //$10000
It raises a problem sometimes that is, if I have already a variable called $color what to do...?
If you think like that try
you may change the EXTR_PREFIX_ALL with EXTR_PREFIX_SAME if you want to add prefix for only repeated values.
you may change the "automobile" with any starting prefix name you want
Now the output is as below
print $automobile_type; //car
print $automobile_color; //red
print $automobile_cost; //$10000
If I add a variable with $type, $color, $cost not at all problem :)
$automobile = array(
"type" => "car",
"color"=> "red",
"cost" => "$10000"
);
If you want to show the values of array with the respective named index we try foreach most but try this now onwards
extract($automobile);
Now we can display the values as
print $type; //car
print $color; //red
print $cost; //$10000
It raises a problem sometimes that is, if I have already a variable called $color what to do...?
If you think like that try
extract($automobile,EXTR_PREFIX_ALL,'automobile');
you may change the EXTR_PREFIX_ALL with EXTR_PREFIX_SAME if you want to add prefix for only repeated values.
you may change the "automobile" with any starting prefix name you want
Now the output is as below
print $automobile_type; //car
print $automobile_color; //red
print $automobile_cost; //$10000
If I add a variable with $type, $color, $cost not at all problem :)

0 comments:
Post a Comment