关键词搜索

源码搜索 ×
×

PHP四维数组、三维数组封装遍历

发布2020-08-06浏览582次

详情内容

<?php

header("Content-type: text/html; charset=utf-8");
$gold = [];
$m_array = array();
$all_gold = array();
// 1维数组
$m_date1 = array(
    'price' => '279',
    'product' => '金条',
    'shop' => '老庙',
);
$m_date2 = array(
    'price' => '322',
    'product' => '金条',
    'shop' => '老凤祥',
);
$m_date3 = array(
    'price' => '299',
    'product' => '黄金',
    'shop' => '老庙',
);
$m_date4 = array(
    'price' => '300',
    'product' => '金条',
    'shop' => '六福',
);
$m_date5 = array(
    'price' => '299',
    'product' => '黄金',
    'shop' => '老凤祥',
);
// 组装2维数组
array_push($m_array, $m_date1, $m_date2, $m_date3, $m_date4, $m_date5);
echo '二维数组:',"\n";
var_dump($m_array);

// 组装3维数组;
for ($i = 0; $i < count($m_array); $i++) {
    if (array_key_exists($m_array[$i]['shop'], $gold)) {
        //  echo "该数组中包含了'key'";
        array_push($gold[$m_array[$i]['shop']], $m_array[$i]);
    } else {
        $gold[$m_array[$i]['shop']][0] = $m_array[$i];
    }
}
echo "三维数组:","\n";
var_dump($gold);
// 定义一个3维数组
$pt = array(
    '六福' =>
        array(

            'price' => '310',
            'product' => 'pt999',
            'shop' => '六福',

        ),
    '老凤祥' =>
        array(
            array(
                'price' => '300',
                'product' => 'pt995',
                'shop' => '老凤祥',
            ),
            array(
                'price' => 'pt',
                'product' => '黄金',
                'shop' => '老凤祥',
            )
        )
);
// 组装成4维数组
$all_gold = array(
    'pt' => $pt,
    'gold' => $gold
);
echo "四维数组:","\n";
var_dump($all_gold);
?>

    测试运行:

    二维数组:
    array(5) {
      [0]=>
      array(3) {
        ["price"]=>
        string(3) "https://cdn.jxasp.com:9143/image/279"
        ["product"]=>
        string(6) "金条"
        ["shop"]=>
        string(6) "老庙"
      }
      [1]=>
      array(3) {
        ["price"]=>
        string(3) "322"
        ["product"]=>
        string(6) "金条"
        ["shop"]=>
        string(9) "老凤祥"
      }
      [2]=>
      array(3) {
        ["price"]=>
        string(3) "https://cdn.jxasp.com:9143/image/299"
        ["product"]=>
        string(6) "黄金"
        ["shop"]=>
        string(6) "老庙"
      }
      [3]=>
      array(3) {
        ["price"]=>
        string(3) "300"
        ["product"]=>
        string(6) "金条"
        ["shop"]=>
        string(6) "六福"
      }
      [4]=>
      array(3) {
        ["price"]=>
        string(3) "https://cdn.jxasp.com:9143/image/299"
        ["product"]=>
        string(6) "黄金"
        ["shop"]=>
        string(9) "老凤祥"
      }
    }
    三维数组:
    array(3) {
      ["老庙"]=>
      array(2) {
        [0]=>
        array(3) {
          ["price"]=>
          string(3) "https://cdn.jxasp.com:9143/image/279"
          ["product"]=>
          string(6) "金条"
          ["shop"]=>
          string(6) "老庙"
        }
        [1]=>
        array(3) {
          ["price"]=>
          string(3) "https://cdn.jxasp.com:9143/image/299"
          ["product"]=>
          string(6) "黄金"
          ["shop"]=>
          string(6) "老庙"
        }
      }
      ["老凤祥"]=>
      array(2) {
        [0]=>
        array(3) {
          ["price"]=>
          string(3) "322"
          ["product"]=>
          string(6) "金条"
          ["shop"]=>
          string(9) "老凤祥"
        }
        [1]=>
        array(3) {
          ["price"]=>
          string(3) "https://cdn.jxasp.com:9143/image/299"
          ["product"]=>
          string(6) "黄金"
          ["shop"]=>
          string(9) "老凤祥"
        }
      }
      ["六福"]=>
      array(1) {
        [0]=>
        array(3) {
          ["price"]=>
          string(3) "300"
          ["product"]=>
          string(6) "金条"
          ["shop"]=>
          string(6) "六福"
        }
      }
    }
    四维数组:
    array(2) {
      ["pt"]=>
      array(2) {
        ["六福"]=>
        array(3) {
          ["price"]=>
          string(3) "310"
          ["product"]=>
          string(5) "pt999"
          ["shop"]=>
          string(6) "六福"
        }
        ["老凤祥"]=>
        array(2) {
          [0]=>
          array(3) {
            ["price"]=>
            string(3) "300"
            ["product"]=>
            string(5) "pt995"
            ["shop"]=>
            string(9) "老凤祥"
          }
          [1]=>
          array(3) {
            ["price"]=>
            string(2) "pt"
            ["product"]=>
            string(6) "黄金"
            ["shop"]=>
            string(9) "老凤祥"
          }
        }
      }
      ["gold"]=>
      array(3) {
        ["老庙"]=>
        array(2) {
          [0]=>
          array(3) {
            ["price"]=>
            string(3) "https://cdn.jxasp.com:9143/image/279"
            ["product"]=>
            string(6) "金条"
            ["shop"]=>
            string(6) "老庙"
          }
          [1]=>
          array(3) {
            ["price"]=>
            string(3) "https://cdn.jxasp.com:9143/image/299"
            ["product"]=>
            string(6) "黄金"
            ["shop"]=>
            string(6) "老庙"
          }
        }
        ["老凤祥"]=>
        array(2) {
          [0]=>
          array(3) {
            ["price"]=>
            string(3) "322"
            ["product"]=>
            string(6) "金条"
            ["shop"]=>
            string(9) "老凤祥"
          }
          [1]=>
          array(3) {
            ["price"]=>
            string(3) "https://cdn.jxasp.com:9143/image/299"
            ["product"]=>
            string(6) "黄金"
            ["shop"]=>
            string(9) "老凤祥"
          }
        }
        ["六福"]=>
        array(1) {
          [0]=>
          array(3) {
            ["price"]=>
            string(3) "300"
            ["product"]=>
            string(6) "金条"
            ["shop"]=>
            string(6) "六福"
          }
        }
      }
    }
    
      81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198

    其他:

    /**
     * 将二维数组组装成三维数组(根据某个key)
     * @param $arr
     * @param $key
     * @date 2020/8/6
     * @time 10:13
     * @return array
     */
    function changeTwoToThree($arr, $key) {
        $new = [];
        for ($i = 0; $i < count($arr); $i++) {
            if (array_key_exists($arr[$i][$key], $new)) {
                array_push($new[$arr[$i][$key]], $arr[$i]);
            } else {
                $new[$arr[$i][$key]][0] = $arr[$i];
            }
        }
        return $new;
    }
    
    /**
     * 将数组的key恢复成数字序列(兼容多维数组)
     * @param $arr
     * @return array
     * @date 2020/8/6
     * @time 10:46
     * @author zzy
     */
    function restore_array($arr){
        if (!is_array($arr)){ return $arr; }
        $c = 0; $new = array();
        foreach ($arr as $key => $value) {
            if (is_array($value)){
                $new[$c] = restore_array($value);
            }
            else { $new[$c] = $value; }
            $c++;
        }
        return $new;
    }
    /**
    将一维数组的key恢复成数字序列
    */
    function restore_array2($arr) {
        foreach($arr as $value){
            $new[] = $value;
        }
        return $new;
    }
    // 二维
    function restore_array3($arr) {
        for($i=0;$i<count($arr);$i++) {
             $arr[$i] = restore_array2($arr[$i]);
        }
        return $arr;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    参考链接:

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载