Foreach重复代码

时间:2016-02-26 16:40:03

标签: php arrays multidimensional-array

嗨,我有这个数组:

array(2117) {
  [0]=>
  array(3) {
    ["code"]=>
    string(21) "AAAAAAA"
    ["format"]=>
    string(7) "160x600"
    ["count"]=>
    int(3)
  }
  [1]=>
  array(3) {
    ["code"]=>
    string(21) "AAAAAAA"
    ["format"]=>
    string(7) "300x250"
    ["count"]=>
    int(135)
  }
  [2]=>
  array(3) {
    ["code"]=>
    string(22) "BBBBB"
    ["format"]=>
    string(7) "300x250"
    ["count"]=>
    int(3)
  }
  [3]=>
  array(3) {
    ["code"]=>
    string(28) "BBBBB"
    ["format"]=>
    string(7) "120x600"
    ["count"]=>
    int(1)
  }

我有这个数组:

$ izes = array(“300x250”,“160x600”,“728x90”,“300x600”,“300x300”,“120x600”,“100x72”,“970x250”,“750x200”,“120x60”,“200x600 “);

我试图做一个foreach,如果数组的元素包含大小不会改变,如果没有应该添加大小count = 0

欲望结果:

 array(2117) {
      [0]=>
      array(3) {
        ["code"]=>
        string(21) "AAAAAAA"
        ["format"]=>
        string(7) "160x600"
        ["count"]=>
        int(3)
      }
      [1]=>
      array(3) {
        ["code"]=>
        string(21) "AAAAAAA"
        ["format"]=>
        string(7) "300x250"
        ["count"]=>
        int(135)
      }
 array(3) {
        ["code"]=>
        string(21) "AAAAAAA"
        ["format"]=>
        string(7) "728x90"
        ["count"]=>
        int(0)
      }
 array(3) {
        ["code"]=>
        string(21) "AAAAAAA"
        ["format"]=>
        string(7) "300x600"
        ["count"]=>
        int(0)
      }
 array(3) {
        ["code"]=>
        string(21) "AAAAAAA"
        ["format"]=>
        string(7) "300x300"
        ["count"]=>
        int(0)
      }
 array(3) {
        ["code"]=>
        string(21) "AAAAAAA"
        ["format"]=>
        string(7) "120x600"
        ["count"]=>
        int(0)
      }
(...) with all sizes
     }

我正在尝试这样做:

$tmp22222=array();
    foreach ($result as $k=>$v){
        foreach ($std_crt_size as $size) {
            if($v['format']==$size) {
                array_push($tmp22222, $v);
            }
            else{
                $v['format'] = $size;
                $v['count'] = 0;
                array_push($tmp22222, $v);
            }
        }
    }

但它总是重复两次

由于

0 个答案:

没有答案