1

这里主要令人头疼,我厌倦了迭代键和值并操纵指针来比较数组键中整数之间的差异。我试图根据减去大于 1 的数组键来拆分和排列这个数组,我很早就注意到这是一种将值分成簇的有效方法,如本问题底部所述。

例如:([0] => cat[2] =>fruit [3] => apple) 键等于:

第二 - 第一键:2-0 = 2 (2>1)

第 3 - 2 键:3-2 = 1 (1=1)

我已经在问题的底部简要说明了我要完成的工作。

数组代码如下:

$arr1 = array("cat", "Van", "fruit", "apple", "orange", "banana", "car", "car", "boat", "Bike", "truck", "cat", "dog", "bus", "van", "truck", "Sea", "ocean", "sea", "ship", "train", "Land");
$arr2 = array("cat", "fruit", "apple", "orange", "banana", "cat", "dog", "sea", "ocean", "land");
$array = array_uintersect($arr1, $arr2, 'strcasecmp');

//returns:

Array
(
    [0] => cat
    [2] => fruit
    [3] => apple
    [4] => orange
    [5] => banana
    [11] => cat
    [12] => dog
    [16] => Sea
    [17] => ocean
    [18] => sea
    [21] => Land
)

(下面)是我迄今为止编写的代码,因为我尝试实现分离,如本问题底部所述:

$arr1 = array("cat", "Van", "fruit", "apple", "orange", "banana", "car", "car", "boat", "Bike", "truck", "cat", "dog", "bus", "van", "truck", "Sea", "ocean", "sea", "ship", "train", "Land");
$arr2 = array("cat", "fruit", "apple", "orange", "banana", "cat", "dog", "sea", "ocean", "land");
foreach($array as &$val){
$array2 = $array;
prev($array);
if(key($array2)-key($array)!==1) { //bool to determine if the difference in keys is only 1 integer apart. 
$val = array($val);
$val[] = "";
}}
unset($val);
print_r(collapse_multi_arrays($array)); //collapse_multi_arrays is a custom function I have to form a single array from multidimensional ones. 

outputs:
Array
(
    [0] => cat
    [1] => 
    [2] => fruit
    [3] => apple
    [4] => orange
    [5] => banana
    [6] => 
    [7] => cat
    [8] => dog
    [9] => 
    [10] => Sea
    [11] => ocean
    [12] => sea
    [13] => 
    [14] => Land
    [15] => 
)

(上面的输出)不确定这是否是迭代数组的最明智的方法,但我想使用空值以某种方式分块或拆分,以获得如下结果:

Array
(
    [0] => Array
        (
            [0] => cat
        )

    [1] => Array
        (
            [2] => fruit
            [3] => apple
            [4] => orange
            [5] => banana
        )

    [2] => Array
        (
            [11] => cat
            [12] => dog
        )

    [3] => Array
        (
            [16] => Sea
            [17] => ocean
            [18] => sea
        )

    [4] => Array
        (
            [21] => Land
        )

)

我认为我可以将每个集群附加到一个值,如下所示:

Array
(
    [0] => cat
    [1] => fruit apple orange banana
    [2] => cat dog
    [3] => Sea ocean sea
    [4] => Land

)

//================================================= ========

总体解释:我总体上想做的是,

由此:

Array
(
    [0] => cat
    [2] => fruit
    [3] => apple
    [4] => orange
    [5] => banana
    [11] => cat
    [12] => dog
    [16] => Sea
    [17] => ocean
    [18] => sea
    [21] => Land
)

对此:

Array
(
    [0] => cat
    [1] => fruit apple orange banana
    [2] => cat dog
    [3] => Sea ocean sea
    [4] => Land

)

当数组键的差异仅相隔 1 个整数时,将数组值聚集在一起。如果数组键整数的差值大于 1,则将簇附加的数组值传递给下一个数组键,如上面的from this *to this* key-values 示例

任何帮助、建议、代码都将不胜感激,因为我发现很难迭代键和值来实现这个目标,干杯。

4

1 回答 1

0

我想到的是一个简单的解决方案,如下所示(经过测试并产生想要的结果):

$a = array(
    0 => 'cat',
    2 => 'fruit',
    3 => 'apple',
    4 => 'orange',
    5 => 'banana',
    11 => 'cat',
    12 => 'dog',
    16 => 'Sea',
    17 => 'ocean',
    18 => 'sea',
    21 => 'Land',
);

$last = 0;
$stack = $grouped = array();
foreach ($a as $key => $val) {
    if ($key - $last > 1) {
        $grouped[] = implode(' ', $stack);
        $stack = array();
    }
    $stack[] = $val;
    $last = $key;
}
$grouped[] = implode(' ', $stack);
print_r($grouped);

我不确定你想在这里实现什么,因为它看起来有点奇怪;)无论你试图解决的潜在问题是什么,我认为有一个比这更优雅的解决方案。

于 2012-04-28T03:58:39.713 回答