PHP - loop through 2 arrays

Hi,
I have 2 fields posted to a page separated by commas; both contain the same number of values when exploded.

How do I create a foreach loop to include both arrays?

I want page with id of 1 to have a position of 1
I want page with id of 154 to have a position of 3
I want page with id of 137 to have a position of 6
Etc...

$page_id = "1,154,137,202,14,157";
$position = "1,3,6,2,4,5";


$page_id_array = explode(",",$page_id);
$pos_array = explode(",",$position);


//what should go below???
foreach($pos_array as $key => $value){

$query_upd_menu = "UPDATE menu SET position = '".$key."' WHERE page_id = '".$value."'";
      
}

Thanks in advance for your help.
sabecsAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
$page_id = explode(',', '1,154,137,202,14,157');
$position = explode(',', '1,3,6,2,4,5');
$newArr=array_combine($position,$page_id);
print_r($newArr);

http://php.net/manual/en/function.array-combine.php
0
 
hieloCommented:
If you REALLY want to do this via foreach:

$page_id = explode(',', '1,154,137,202,14,157');
$position = explode(',', '1,3,6,2,4,5');
$newArr=array();
foreach($positions as $i=>$index){
  $newArr[$index] = $page_id[$i];
}
0
 
sabecsAuthor Commented:
Thanks Hielo.
0
 
hieloCommented:
Glad to help.

Regards,
Hielo
0
All Courses

From novice to tech pro — start learning today.