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

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

多維搜索未顯示所有結果

多維搜索未顯示所有結果

PHP
阿晨1998 2023-06-24 16:14:44
我有一個多維數組搜索我的數組看起來像這樣Array(    [0] => Array        (            [id] => 1            [location] => 3            [location_fees] => 3            [gross_percentage] => 25            [transaction_percentage] => 0            [user_name] => admin            [user_id] => 1            [gross] => yes            [transaction] => no        )    [1] => Array        (            [id] => 2            [location] => 5            [location_fees] => 5            [gross_percentage] => 0            [transaction_percentage] => 24            [user_name] => admin            [user_id] => 1            [gross] => no            [transaction] => yes        )    [2] => Array        (            [id] => 3            [location] => 2            [location_fees] => 5            [gross_percentage] => 10            [transaction_percentage] => 0            [user_name] => admin            [user_id] => 1            [gross] => yes            [transaction] => no        ))我使用下面的php,我知道它可能可以做得更干凈或更少的代碼,所以如果你有任何想法如何獲得相同的結果,我絕對會學習,我洗耳恭聽!這是我使用的 PHP:    $key = false;    $search = ['gross' => 'yes'];    foreach ($results as $k => $v) {    if ($v['gross'] == $search['gross'] ) {        $key = $k;        $location_fees = array_search('yes', array_column($results, 'gross','location_fees'));        echo  "The location fees: ". $location_fees ." % <br><br>";             $gross_percentage = array_search('yes', array_column($results, 'gross', 'gross_percentage'));        echo "The gross_percentage is: ".$gross_percentage ."% <br><br>";    }      }    }它拒絕顯示鍵 #1 的位置費用現在奇怪的是,如果我將地點費用更改為 3,它就會顯示出來。但它不會帶有數字“5”,這也是位置#這與數字“5”沖突是否有原因?注意鍵“0”具有位置“3”和位置費用“3”,并且它不會引起任何問題。我已經在這個問題上堅持了幾個小時了,它適用于鍵#0和#2,沒有任何問題。有任何想法嗎?
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

您遇到此問題的原因是您致電array_column:


array_column($results, 'gross','location_fees')

這會導致gross值被$results重新索引location_fees,對于您的數據來說,這會導致類似的結果


[3 => 'yes', 5 => 'no', 5 => 'yes']

正如您所看到的,您有兩個5無效的數字鍵,因此第二個數字鍵會覆蓋第一個數字鍵,最終得到


[3 => 'yes', 5 => 'yes']

而你失敗array_search了no,因此你沒有得到任何結果。在任何獲得復制值的地方都會遇到這個問題。


無論如何,我不確定你為什么要采取這種方法。看來你無論如何都有你想要的數字$v:


$search = ['gross' => 'yes'];

foreach ($results as $k => $v) {

    echo "key $k<br>" . PHP_EOL;

    if ($v['gross'] == $search['gross'] ) {

        $location_fees = $v['location_fees'];

        echo  "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;

     

        $gross_percentage = $v['gross_percentage'];

        echo "The gross_percentage is: ".$gross_percentage ."% <br><br>" . PHP_EOL;


    }  else  {


        $tran_perc = $v['transaction_percentage'];

        echo "The locations percentage is: ".$tran_perc ." % <br>" . PHP_EOL;


        $the_loc = $v['location'];

        echo "The location is: ".$the_loc ."  <br>" . PHP_EOL;


        $location_fees = $v['location_fees'];

        echo  "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;

    }

}

輸出:


key 0<br>

The location fees: 3 % <br><br>

The gross_percentage is: 25% <br><br>

key 1<br>

The locations percentage is: 24 % <br>

The location is: 5  <br>

The location fees: 5 % <br><br>

key 2<br>

The location fees: 5 % <br><br>

The gross_percentage is: 10% <br><br>

3v4l.org 上的演示


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 289 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號