3

Using PHP (7.1) with the following data and nested loops, I'm trying to get each host to match the corresponding number in the COUNTS array.

HOSTS:
Array ( 
  0 => 'example/search?results1'
  1 => 'thisone/search?results2'
  2 => 'thesetoo/search?results3'
)

COUNTS:
Array (
  0 => '3'
  1 => '5'
  2 => '7'
)

foreach ( $counts as $count ) {
  foreach ( $hosts as $host ) {
  $t = $count;
    for ($n=0; $n<$t; $n++) {
      $results[] = ++$host;
    }
  continue 2;
  }
}

echo 'THESE ARE ALL THE RESULTS:',PHP_EOL,PHP_EOL,var_dump($results);

RESULTS I'M LOOKING FOR: MULTIDIMENSIONAL ARRAY

Array (
0 => Array (
    0 => 'example/search?results1'
    1 => 'example/search?results1'
    2 => 'example/search?results1'
    )
1 => Array (
    0 => 'thisone/search?results2'
    1 => 'thisone/search?results2'
    2 => 'thisone/search?results2'
    3 => 'thisone/search?results2'
    4 => 'thisone/search?results2'
    )
2 => Array (
    0 => 'thesetoo/search?results3'
    1 => 'thesetoo/search?results3'
    2 => 'thesetoo/search?results3'
    3 => 'thesetoo/search?results3'
    4 => 'thesetoo/search?results3'
    5 => 'thesetoo/search?results3'
    6 => 'thesetoo/search?results3'
    )
)

Notice the number of results per HOSTS corresponds to the COUNTS array.

In the nested for loops above, I'm either getting just one host for all counts or every count for all hosts in a single dimension array. What I need is a multi-dimensional array, but the nested for loop logic is escaping me. I've tried both continue and break in the loops, but no luck. If the loop gets another count, then it skips the host. If it gets another host, then it skips the count.

There is no pattern to either the hosts or the counts array. These will always correspond to each other but they will be random strings/numbers. Thank you for your help.

4
  • What is the expected out of the array? Commented Nov 1, 2017 at 16:09
  • What you see above is the desired output from the 2 corresponding arrays. Commented Nov 1, 2017 at 16:12
  • Wow! All great answers below. I'm running tests on all of them. Just need a few hours for final results. Thank you! Commented Nov 1, 2017 at 18:19
  • Please upvote this question so I can get a couple more reputation points. Thank you! Commented Nov 1, 2017 at 21:13

4 Answers 4

2

if count of $hosts and $counts equals:

$result = [];
foreach ($hosts as $i => $host) {
    $result[] = array_fill(0, $counts[$i], $host);
}
Sign up to request clarification or add additional context in comments.

6 Comments

foreach ($hosts as $i => $host) {
The simplicity of your answer is the best. What would you suggest for appending strings to the end of each host string in this? For example, instead of returning only 3 of the same string from the first array combination, what about returning a unique string for each of the 3. Like this: array(3) { [0]=> array(3) { [0]=> string(67) "example/search?results100" [1]=> string(67) "example/search?results200" [2]=> string(67) "example/search?results300" } } Notice the 3 different integers on each tail.
you can append any string to $host like this $result[] = array_fill(0, $counts[$i], $host . 'example');
Thank you. That would work for a static append, but what about a dynamic integer like 100, 200, 300 for each result?
for this we can't use array_fill this function generates array with same values, so use for loop to fill arrays
|
2

This question is the perfect usage example for array_map() and array_fill().

$hosts = array(
  0 => 'example/search?results1',
  1 => 'thisone/search?results2',
  2 => 'thesetoo/search?results3',
);

$counts = array(
  0 => '3',
  1 => '5',
  2 => '7',
);

$result = array_map(
    function($host, $count) {
        return array_fill(0, $count, $host);
    },
    $hosts,
    $counts
);

6 Comments

Can a dynamic integer be appended to the host string with your array_map function? For example, can each result have a 100, 200, 300 or any other integer appended dynamically?
Anything can be done if it is specified clearly. Take into consideration that I cannot read your mind.
I know I didn't include this in the question, but specifically, can your array_map with array_fill handle dynamic code? Could I take another array of dynamic numbers and assign them to each result?
Some things can be done (by passing more arguments to array_map() or by using closures or by other means), others cannot be done this way. Do you have in mind a specific use case? Describe it in a new question.
Thank you, I'm posting the new question right now. I'll send you the link.
|
0

Try this:

$hosts = array ( 
  0 => 'example/search?results1',
  1 => 'thisone/search?results2',
  2 => 'thesetoo/search?results3'
);

$counts = array (
  0 => '3',
  1 => '5',
  2 => '7'
);

$results =array();
foreach ( $counts as $count ) { 
    $key_of_count = array_search( $count, $counts ); 
    for ($i=0; $i < (int)$count; $i++) {
        $results[$key_of_count][] = $hosts[$key_of_count];
    }
}
echo "<pre>"; print_r($results); echo "</pre>";

1 Comment

In my data, I have multiple counts that are the same integer. In your example, this combined similar integer values together into one, but anything different produced correct results. For example, if you add another 3 to the COUNTS array above, it will combine with the adjacent 3 and make it one large array of only 1 URL.
0

If you are looking for way that is done using only loops and not using any fancy array functions, then this might be the answer you are looking for:

$result = [];

foreach($counts as $k=>$count){
  $result[$k]='';
    for($i=0; $i < $count; $i++){
        $result[$k][] = $hosts[$k];
    }
}

1 Comment

This returns only 1 result per URL, and not the designated number from the COUNTS array.

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.