2010-07-26 5 views
0

Vous cherchez à parcourir un tableau d'URL et à injecter chaque mot clé d'un deuxième tableau dans chaque URL, mais vous n'arrivez pas à comprendre les tableaux. Par exemple:Mise en boucle d'un tableau à travers un deuxième tableau

$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="), 

Je voudrais ci-dessus à la sortie:

google.co.uk/#hl=en & q = mots-clés + 1
google.co.uk/#hl = en & q = 2 + mot-clé
& google.co.uk/#hl=en q = 3 + mot-clé

bing.com/search?q=Keyword+1 bing.com/search?q=Keyword +2
bing.com/recherche?q=K eyword + 3
& uk.search.yahoo.com/search?vc= p = + 1 mots-clés
& uk.search.yahoo.com/search?vc= p = + 2 mots-clés
uk.search.yahoo .com/search? vc = & p = Keyword + 3

y at-il un moyen efficace pour atteindre cet objectif? :)

+0

Vous pouvez voir un certain avantage de http://stackoverflow.com/questions/2246493/concatenate-values-of-n-arrays-in-php –

Répondre

0

Voici comment vous pouvez le faire:

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

Résultat:

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 
) 
2
foreach($url as $currenturl) 
{ 
    foreach($key as $currentkey) 
    { 
     echo $currenturl . $currentkey . '\n'; 
    } 
} 

essayer cette

0

Vous voulez d'abord faire une boucle sur le tableau $url, puis pour chaque élément du tableau $url, vous voulez aussi faire une boucle sur toutes les clés du tableau $key et les append à l'article que vous avez choisi de $url,

foreach ($url as $u) 
{ 
    foreach ($key as $k) 
    { 
     echo $u.$k."\n"; 
    } 
} 
0

Qu'est-ce que vous décrivez est une généralisation du produit extérieur.

Il serait plus intéressant de définir une fonction d'ordre supérieur pour cela:

/** 
* 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"))); 

donne:

 
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 
     ) 

) 

Voir Mathematica de Outer.

Questions connexes