-1

Possible Duplicate:
foreach with three variables add

i have following code i want to use multiple array in foreach. please note that i need $yourSiteContent[] as it is.

foreach ($pieces as $id and $pieces1 as $id_date) {
    $yourSiteContent[] = array('permalink' => $id, 'updated' => $id_date);
}

as you can see i have two arrays ($pieces and $pieces1), please check a loop and let me know where i am doing mistake.

$pieces1 contains dates and $pieces contains urls.

thanks for your help.

Community
  • 1
  • 1
Hannah Pink
  • 81
  • 2
  • 7

4 Answers4

4

Assuming both $pieces and $pieces1 have the same keys you can do the following:

for (array_keys($pieces) as $key) {
    $yourSiteContent[] = 
        array('permalink' => $pieces[$key], 'updated' => $pieces2[$key]);
}
snakile
  • 52,936
  • 62
  • 169
  • 241
2

You should use a for loop and an indexer, then access each array.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

To keep to the foreach format you can also do it like (Assuming both $pieces and $pieces1 have the same keys):

$yourSiteContent = array();
foreach ($pieces as $key=>$value) {
    $yourSiteContent[] = array('permalink'=>$value,
                               'updated'=>$pieces1[$key]);
}
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

You can use array_combine() to combine the two arrays into a single array, with one being used for keys and the other for values.

foreach (array_combine($pieces, $pieces1) as $id => $id_date) {
    $yourSiteContent[] = array('permalink' => $id, 'updated' => $id_date);
}

See array_combine() for more information.

theunraveler
  • 3,254
  • 20
  • 19