如何用令牌划分部分数组?

时间:2013-12-25 13:52:11

标签: php arrays

我有一个数组

foreach ($infos->find('.products-list-item .products-list-item__sizes') as $sizes_info) 
{
    while($size = $sizes_info->children($k++))
    {
        $sizes_arr[] = $size->plaintext;
    }
    $sizes_arr[] = '_';
    $k=0;
}

看起来像:

array(412) { [0]=> string(2) "XXL" [1]=> string(1) "S" [2]=> string(1) "M" [3]=> string(1) "L" [4]=> string(1) "_" [5]=> string(2) "43" [6]=> string(2) "44".....and so on

我需要在数组中用符号“_”来划分这个数组......换句话说我需要得到这个:

 array(4) { [0]=> string(2) "XXL" [1]=> string(1) "S" [2]=> string(1) "M" [3]=> string(1) "L"}
 array(2) { [0]=> string(2) "43" [1]=> string(1) "44"}

提前感谢!

1 个答案:

答案 0 :(得分:4)

如果您以其他方式需要它,为什么要以这种方式形成阵列?只需:

foreach ($infos->find('.products-list-item .products-list-item__sizes') as $sizes_info) {
    $sizes_tmp = []; //or $sizes_tmp = array(); for PHP<=5.3
    while ($size = $sizes_info->children($k++)) {
        $sizes_tmp[] = $size->plaintext;
    }
    $sizes_arr[] = $sizes_tmp;
    $k=0;//not sure what is it. leaving it as it is
}