To create array from 1 php variable Why out put not same array ?
in this code i create array using php variable
<?PHP
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
$xxx_array = array($xxx);
echo '<pre>'; print_r($xxx_array); echo '</pre>';
?>
and echo is
Array
(
[0] => 'Free', 'Include', 'Offer', 'Provide'
)
how to echo like this
Array
(
[0] => Free
[1] => Include
[2] => Offer
[3] => Provide
)
<?php
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
// Split by ","
$separatedValues = explode(',', $xxx);
// Remove the single quotation marks
for($i = 0; $i < count($separatedValues); ++$i) {
$separatedValues[$i] = str_replace("'", '', $separatedValues[$i]);
}
var_dump($separatedValues);
?>
It is all about the explode()
( http://de.php.net/explode ) function.
Read up on the explode() function. The below code accomplishes what you ask.
<?PHP
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
$xxx_array = array(explode(",", $xxx);
echo '<pre>';
print_r($xxx_array);
echo '</pre>';
?>
If you want to mimic actually creating the array (and not having the values quoted within the array), use this:
$xxx_array = array_map( function( $el) { return trim( $el, "' "); }, explode(',', $xxx));
This trims the '
and spaces from the beginning and ends of the elements after converting the string to an array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.