开发者

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 pr开发者_C百科inted 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 $key => $prodid) {
  $prodname = $array['product_name'][$key];

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜