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) "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) "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) "299"
    ["product"]=>
    string(6) "黄金"
    ["shop"]=>
    string(9) "老凤祥"
  }
}
三维数组:
array(3) {
  ["老庙"]=>
  array(2) {
    [0]=>
    array(3) {
      ["price"]=>
      string(3) "279"
      ["product"]=>
      string(6) "金条"
      ["shop"]=>
      string(6) "老庙"
    }
    [1]=>
    array(3) {
      ["price"]=>
      string(3) "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) "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) "279"
        ["product"]=>
        string(6) "金条"
        ["shop"]=>
        string(6) "老庙"
      }
      [1]=>
      array(3) {
        ["price"]=>
        string(3) "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) "299"
        ["product"]=>
        string(6) "黄金"
        ["shop"]=>
        string(9) "老凤祥"
      }
    }
    ["六福"]=>
    array(1) {
      [0]=>
      array(3) {
        ["price"]=>
        string(3) "300"
        ["product"]=>
        string(6) "金条"
        ["shop"]=>
        string(6) "六福"
      }
    }
  }
}

其他:

/**
 * 将二维数组组装成三维数组(根据某个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;
}

参考链接:

  • https://www.cnblogs.com/dengcw/p/5514453.html
  • https://www.jb51.net/article/65053.htm
  • https://www.cnblogs.com/richerdyoung/p/11765206.html

你可能感兴趣的:(PHP)