猿问

多维搜索未显示所有结果

我有一个多维数组搜索


我的数组看起来像这样


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,没有任何问题。有任何想法吗?


阿晨1998
浏览 265回答 1
1回答

慕婉清6462132

您遇到此问题的原因是您致电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) {&nbsp; &nbsp; echo "key $k<br>" . PHP_EOL;&nbsp; &nbsp; if ($v['gross'] == $search['gross'] ) {&nbsp; &nbsp; &nbsp; &nbsp; $location_fees = $v['location_fees'];&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $gross_percentage = $v['gross_percentage'];&nbsp; &nbsp; &nbsp; &nbsp; echo "The gross_percentage is: ".$gross_percentage ."% <br><br>" . PHP_EOL;&nbsp; &nbsp; }&nbsp; else&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $tran_perc = $v['transaction_percentage'];&nbsp; &nbsp; &nbsp; &nbsp; echo "The locations percentage is: ".$tran_perc ." % <br>" . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; $the_loc = $v['location'];&nbsp; &nbsp; &nbsp; &nbsp; echo "The location is: ".$the_loc ."&nbsp; <br>" . PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; $location_fees = $v['location_fees'];&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp; "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;&nbsp; &nbsp; }}输出: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&nbsp; <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 上的演示
随时随地看视频慕课网APP
我要回答