[英]To create array from 1 php variable Why out put not same array?
To create array from 1 php variable Why out put not same array ?从 1 个 php 变量创建数组 为什么输出不同的数组?
in this code i create array using php variable在这段代码中,我使用 php 变量创建数组
<?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.这是关于
explode()
( http://de.php.net/explode ) 函数的全部内容。
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.这会在将字符串转换为数组后从元素的开头和结尾修剪
'
和空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.