views:

59

answers:

4

Looking to loop through an array of URLs and inject each keyword from a second array into each URL but can't get to grips with the understanding of arrays. Eg:

$key = array("Keyword+1", "Keyword+2", "Keyword+3"),
$url =array("google.co.uk/#hl=en&q=", "bing.com/search?q=","uk.search.yahoo.com/search?vc=&p="),

I'd like the above to output:

google.co.uk/#hl=en&q=Keyword+1
google.co.uk/#hl=en&q=Keyword+2
google.co.uk/#hl=en&q=Keyword+3
bing.com/search?q=Keyword+1
bing.com/search?q=Keyword+2
bing.com/search?q=Keyword+3
uk.search.yahoo.com/search?vc=&p=Keyword+1
uk.search.yahoo.com/search?vc=&p=Keyword+2
uk.search.yahoo.com/search?vc=&p=Keyword+3

Is there an efficient way to achieve this? :)

+2  A: 
foreach($url as $currenturl)
{
    foreach($key as $currentkey)
    {
        echo $currenturl . $currentkey . '\n';
    }
}

try this

marduk
A: 

You first want to loop over the $url array, then for each item in the $url array, you also want to loop over all the keys in the $key array and append them to the item you picked from $url,

foreach ($url as $u)
{
    foreach ($key as $k)
    {
        echo $u.$k."\n";
    }
}
Rich Adams
A: 

Here is how you can do that:

$keys = array("Keyword+1", "Keyword+2", "Keyword+3");
$urls =array("google.co.uk/#hl=en&q=", "bing.com/search?q=","uk.search.yahoo.com/search?vc=&p=");

$my_array = array();

foreach($urls as $url)
{
    foreach($keys as $key)
    {
        $my_array[] = $url . $key;
    }
}

print_r($my_array);

Result:

Array
(
    [0] => google.co.uk/#hl=en&q=Keyword+1
    [1] => google.co.uk/#hl=en&q=Keyword+2
    [2] => google.co.uk/#hl=en&q=Keyword+3
    [3] => bing.com/search?q=Keyword+1
    [4] => bing.com/search?q=Keyword+2
    [5] => bing.com/search?q=Keyword+3
    [6] => uk.search.yahoo.com/search?vc=&p=Keyword+1
    [7] => uk.search.yahoo.com/search?vc=&p=Keyword+2
    [8] => uk.search.yahoo.com/search?vc=&p=Keyword+3
)
Sarfraz
A: 

What you're describing is a generalization of the outer product.

It would be more interesting to define a higher order function for this:

/**
 * A generalization of the  outer product, forming all the possible
 * combinations of the elements of the two arrays and feeding them
 * to $f.
 * The keys are disregarded
 **/
function array_outer($f, array $array1, array $array2) {
    $res = array();
    foreach ($array1 as $e1) {
        $cur = array();
        foreach ($array2 as $e2) {
            $cur[] = $f($e1, $e2);
        }
        $res[] = $cur;
    }
    return $res;
}

$f = function ($a,$b) { return $a.$b; };
print_r(array_outer($f, array("a","b","c"), array("1", "2", "3")));

gives:

Array
(
    [0] => Array
        (
            [0] => a1
            [1] => a2
            [2] => a3
        )

    [1] => Array
        (
            [0] => b1
            [1] => b2
            [2] => b3
        )

    [2] => Array
        (
            [0] => c1
            [1] => c2
            [2] => c3
        )

)

See Mathematica's Outer.

Artefacto