简体   繁体   中英

how do i do a quick loop here

Array
(
[product_id] => Array
    (
        [0] => 61
        [1] => 62
        [2] => 63
    )

[product_name] => Array
    (
        [0] => 44" jesson WIDESCREEN LCD 
        [1] => 19" jesson WIDESCREEN LCD 
        [2] => Touchscreen monitor
    )

)

I am sort of confused on how to do this simple loop where product_id[0] always matches product_name[0] and so on ....i tried

if i do a foreach i get the first loop is all the product_id and i need the names to be printed also....any ideas

For ease of explanation, let's set:

$product_id = $myarray['product_id'];
$product_name = $myarray['product_name'];

As long as you're sure that $product_id and $product_name have the same keys (and it looks like they do), you can make your loop like this:

foreach ($product_id as $i => $id) {
  $name = $product_name[$i];
     .
     .
     .
 }
foreach($array['product_id'] as $k=>$v){
    echo $v." - ".$array['product_name'][$k];
}

This will echo out the product id, followed by the corresponding product name. You can change that to do what you like but the important values are there.

foreach($array['product_id'] as $key => $prodid) {
  $prodname = $array['product_name'][$key];

  //do what you want with $prodid and $prodname here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM