天天看點

PHP四維數組、三維數組封裝周遊

<?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