简体   繁体   中英

php iterate over both keys and values and split into sub-array based on array-keys integer difference

major headache here, I'm tiring to iterate over both keys and values and manipulate the pointer to make a comparison on the difference between the integer in the array-keys. I'm trying to split and arrange this array based on subtraction of the array-keys being greater than 1, I noticed this early on as a valid method to separate the values into clusters as explained at the bottom of this question.

eg:([0] => cat[2] => fruit [3] => apple) keys equate to:

2nd - 1st keys: 2-0 = 2 (2>1)

3rd - 2nd keys: 3-2 = 1 (1=1)

I've given a concise explanation of what I'm trying to accomplish at the bottom of the question.

array code below:

$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
)

(below) is code I've so far written as my attempted implementation to separate as explained at the bottom of this question:

$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] => 
)

(output above) not sure if it's the wisest way to iterate over the array but I'm wanting to use the empty values to chunk or split somehow in order to get something like this:

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
        )

)

which I think I can append to each cluster to one value each like this:

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

)

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

overall explanation: what I'm trying to do overall is something like,

from this:

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

to this:

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

)

which is to cluster-append array values together when the difference in array-keys are only 1 integer apart. if the difference in array-key integers is greater than 1 then cluster-appended array values are passed to next array-key, as in the from this *to this* key-values above example

Any help, advice, code would be most grateful as I'm finding it difficult to iterate over both keys and values to achieve this goal, cheers.

What jumps to my mind is a simple solution like the following (tested and producing the wanted result):

$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);

I'm not sure what you are trying to achieve here though as it looks kind of strange ;) Whatever is the underlying problem your trying to solve, I'd think theres a more elegant solution than this.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM