Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php数组分解字符_Php_Arrays - Fatal编程技术网

Php数组分解字符

Php数组分解字符,php,arrays,Php,Arrays,这是我的阵列: array:4 [ 0 => "Family Deluxe for 4 Person x 1" 1 => "Studio Deluxe for 2 Person x 1" 2 => "Additional Adult x 2" 3 => "Additional Children x 2" ] 当我进入foreach循环以删除x并获取计数时,我会遇到以下输出结果: array:4 [ 0 => array:1 [ "Fam

这是我的阵列:

array:4 [
  0 => "Family Deluxe for 4 Person x 1"
  1 => "Studio Deluxe for 2 Person x 1"
  2 => "Additional Adult x 2"
  3 => "Additional Children x 2"
]
当我进入
foreach
循环以删除
x
并获取计数时,我会遇到以下输出结果:

array:4 [
  0 => array:1 [
    "Family Delu" => "e for 4 Person "
  ]
  1 => array:1 [
    "Studio Delu" => "e for 2 Person "
  ]
  2 => array:1 [
    " Additional Adult " => " 2"
  ]
  3 => array:1 [
    " Additional Children" => " 2"
  ]
]
问题是,字符串
Deluxe
中有
x
char,因此循环开始混淆从何处开始
explode()
。以下是我的
foreach
代码:

foreach($a as $s) {
            list($size, $quantity) = explode('x', $s);
            $data[] = array($size => $quantity);
            $sumVariant += $quantity;
        }

如何让
explode()
识别两边都有空格的
x
?谢谢

explode()的分隔符参数不必是单个字符,它可以是任何字符串。因此,使用
x
并在其周围加空格作为分隔符

list($size, $quantity) = explode(' x ', $s);

explode(“x”,$s)应该可以做到这一点。或者您可以获得分解数组的最后一项。

在其周围放置空格:
explode('x',$s)