第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除數(shù)組foreach mysql中的重復(fù)項

刪除數(shù)組foreach mysql中的重復(fù)項

PHP
www說 2021-11-13 17:22:30
如何創(chuàng)建沒有重復(fù)名稱的數(shù)組?$sqla = "SELECT * from categoriesb";$id = $mysql->Execute($sqla);foreach ($id as $keys) {    $sqlb = "SELECT * from productsb where parent = ".$keys["id"]."";    $cac = $mysql->Execute($sqlb);    foreach ($cac as $key) {        $tab[] = array("name" => $keys["name_c"],                        array("product" => $key["name"]));    }}我的陣列看起來像這樣var_dump($tab);array(2) {[0]=> array(2) {   ["name"]=> string(10) "Beer"   [0]=> array(1) {       ["product"]=> string(13) "Mild"   }}[1]=> array(2) {   ["name"]=> string(10) "Beer"   [0]=> array(1) {       ["product"]=> string(13) "Bitter"   }}如何刪除陣列上的重復(fù)名稱?我想創(chuàng)建數(shù)組array(1) {[0]=> array(2) {    ["name"]=> string(10) "Beer"    [0]=> array(1) {       ["product"]=> string(13) "Mild"    }    [1]=> array(1) {       ["product"]=> string(13) "Bitter"    }}
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻1833條經(jīng)驗 獲得超4個贊

使用 array_unique 函數(shù)避免列表中的重復(fù)

使用 array_unique 函數(shù)避免列表中的重復(fù)


查看完整回答
反對 回復(fù) 2021-11-13
?
哈士奇WWW

TA貢獻1799條經(jīng)驗 獲得超6個贊

注意:您的查詢存在N+1 問題。


如果 id 是用戶生成的字符串,它也可能遭受二次 SQL 注入。


這是一個潛在的解決方案:


$sqla = "SELECT * from categoriesb";

$categories = $mysql->Execute($sqla);

$ids = array_column($categories, 'id');

// Query with parameters

$sqlb = "SELECT * from productsb where parent IN (".implode(',', array_fill(0, count($ids), '?')).")";

// Not exactly sure what API is used here but this needs to execute the query that uses the $ids as the parameters

$products = $mysql->Execute($sqlb, $ids); 


foreach ($categories as $category) {

    $tab[$category['id']] = [ "name" => $category["name_c"] ];

}

foreach ($products as $product) {

    $tab[$product['parent']][] = array("product" => $product["name"]));

}

這會:

  1. 執(zhí)行一次查詢以獲取所有類別

  2. 執(zhí)行一個查詢以獲取所有相關(guān)產(chǎn)品,但僅針對檢索到的類別(在您的情況下,一個簡單的方法select *可以工作,但如果您稍后需要向第一個選擇添加過濾器,則需要縮小范圍

  3. 使用類別構(gòu)建數(shù)組并將 id 設(shè)置為鍵(供以后查找)

  4. 根據(jù)產(chǎn)品更新數(shù)組

它總是只執(zhí)行 2 次查詢而不是 1 + 產(chǎn)品數(shù)量,如果您使用準(zhǔn)備好的語句,您還可以避免第二次訂單注入(如果這可能是一個問題,通常如果id不是用戶定義的,那么它可能不會成為問題)


查看完整回答
反對 回復(fù) 2021-11-13
  • 2 回答
  • 0 關(guān)注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號