0

I have two array.

$ids = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110];
$users = [1,2,3,4,5];

for ($index = 0; $index < count($ids ); $index++) {
     echo $ids [$index] . $users [$index];
}        

My Result;

100 - 1
101 - 2
102 - 3
103 - 4
104 - 5

I would like to merge it with duplicates ids.

100 - 1
101 - 2
102 - 3
103 - 4
104 - 5
105 - 1
106 - 2
107 - 3
108 - 4
109 - 5
110 - 1
3
  • sorry if my question is not correct, but I posted my current code with array and foreach. Commented Apr 18, 2022 at 13:39
  • Sounds like you want to loop over IDs, start to finish, and combine them with users, restarting that array once you reach the end. Is that correct? If so, can you explain where 5 is coming from? Commented Apr 18, 2022 at 14:28
  • yes! sorry I edited my array with 5. Commented Apr 18, 2022 at 14:30

2 Answers 2

3

If your arrays are indeed this small, array_shift is a good tool. You can copy your array, pop an item off it continuously, and then re-copy the array as needed. The performance of array_shift gets worse as arrays grow large (as far as I've been told, but haven't personally witnessed).

$ids = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110];
$users = [1,2,3,4,5];

// Temporary copy of users
$user_ids = [];
foreach( $ids as $id ){
    
    // If our copy is empty, reload it
    if(!$user_ids){
        $user_ids = $users;
    }
    
    // Use array_shift() to grab and remove from the front
    echo sprintf('%1$d - %2$d', $id, array_shift($user_ids)), PHP_EOL;
}

If you users is indeed linear like that, you can also use modular arithmetic to repeat things:

foreach( $ids as $idx => $id ){
    echo sprintf('%1$d - %2$d', $id, ($idx % 5) + 1 ), PHP_EOL;
}

Both demoed here: https://3v4l.org/LuRQj

Sign up to request clarification or add additional context in comments.

2 Comments

Work perfect! $users array have max 10/20 users, ids array sometimes can have about 100/300 ids.
I use array_shift and similar with tens of thousands, although I don't use it on pages that demand high performance, and don't have any issues, so I think you'll be fine with just this.
0
$ids = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110];

$users = [1,2,3,4];

for ($index = 0; $index < count($ids); $index++) {

    for ($i = 0; $i < count($users); $i++) {

        echo "<pre/>";

        echo $ids[$i] . "-".$users[$i];

    }

    $k=0;

    for ($j = $i; $j < $i + count($users); $j++) {

        echo "<pre/>";

        echo $ids[$j] . "-".$users[$k];

        $k++;

    }
}

5 Comments

What is <pre/>? When I run this I get what appears to be a lot of extra items: 3v4l.org/nlklb
you can remove it pre to show the items in separate lines
This solution, repeat ids a lot of times.
The OP's sample output has 11 items, your sample output has 88 items, a lot of them duplicated. I'm also not seeing any items that start with 108, 109 or 110.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.