简体   繁体   English

如何从php数组中选择特定记录

[英]how to select particular record from a php array

I have an multi dimensional array: 我有一个多维数组:

$array=array(
[0]=>array([0]=>"100",
           [1]=>"snd",
           [2]=>array(['id']="1",['name']=>"xperia")

),
[1]=>array([0]=>"100",
           [1]=>"snd",
           [2]=>array(['id']="2",['name']=>"xperia")
),

[2]=>array([0]=>"100",
           [1]=>"fkt",
           [2]=>array(['id']="3",['name']=>"xperia")
),
[3]=>array([0]=>"100",
           [1]=>"fkt",
           [2]=>array(['id']="4",['name']=>"xperia")
),
[4]=>array([0]=>"90",
           [1]=>"snd",
           [2]=>array(['id']="5",['name']=>"xperia")
),
[5]=>array([0]=>"90",
           [1]=>"fkt",
           [2]=>array(['id']="6",['name']=>"xperia")
)
);

Example what array contains: 示例数组包含的内容:

$array[0][0]=percentage
$array[0][1]=merchant
$array[0][2]=product info

i want to display any merchant once whose percentage is between 80-100. 我想显示百分比介于80-100之间的任何商人。 Even if two merchant has 100% match still i want to display it once. 即使两个商人具有100%的匹配率,我还是要显示一次。

i have used following logic and code: 我使用了以下逻辑和代码:

foreach($array as $final_array)
{
if($final_array[0]>=90)
{
echo $final_array[0];
echo $final_array[2]['id'];
echo $final_array[2]['name'];
}
}

how can i avoid same merchant information which is displaying double. 我如何才能避免显示两次的同一商家信息。 i have tried array_unique function as well but its showing me first product only. 我也尝试过array_unique函数,但是它只向我展示第一个产品。

Your code has some problems. 您的代码有一些问题。 I guess it needs to be like that: 我想它需要像这样:

foreach ($array as $final_array)
{
    if ($final_array[0]>=80) {
        echo $final_array[0];
        echo $final_array[2][0];
        echo $final_array[2][1];
    }
}

and if you need to avoid duplicates something like this: 如果您需要避免重复,例如:

$merchants = array();
foreach ($array as $final_array)
{
    $merchant = $final_array[1];
    if ( ($final_array[0]>=80) && !in_array($merchant, $merchants) ) {
        $merchants[] = $merchant;
        echo $final_array[0];
        echo $final_array[2][0];
        echo $final_array[2][1];
    }
}

Easiest and most elegant to filter an array is to use the power of php's array functions combined with a callback: 过滤数组最简单,最优雅的方法是结合使用PHP的数组函数和回调函数:

<?php

$input = [
    ["100%", "snd", ['id' =>1, 'name'=>"xperia"]],
    ["100%", "snd", ['id' =>2, 'name'=>"xperia"]],
    [ "90%", "fkt", ['id' =>3, 'name'=>"xperia"]],
    [ "80%", "fkt", ['id' =>4, 'name'=>"xperia"]],
    [ "70%", "snd", ['id' =>5, 'name'=>"xperia"]],
    [ "60%", "fkt", ['id' =>6, 'name'=>"xperia"]],
];

print_r(array_filter($input, function($val){
    return ((int)$val[0] >= 80);
}));

Note: I modified your input values to demonstrate the filter application. 注意:我修改了您的输入值以演示过滤器应用程序。


But as written above in the comments: Comparing the number 90 to the string "90%" is a pretty risky thing like that ($array[0]>=90). 但是正如上面的注释中所述:将数字90与字符串“ 90%”进行比较是很冒险的事情($ array [0]> = 90)。 You compare a number with a string and blindly trust that the automagical conversion will turn "90%" into 90. Maybe you want to invest into some regex number extraction here, unless you are 100% certain that those strings always will have exactly that format... 您将一个数字与一个字符串进行比较,并盲目地相信自动转换将把“ 90%”变成90。也许您想在这里投资一些正则表达式数字提取, 除非您100%确定这些字符串始终具有完全相同的格式...

You can create another array to store shown merchants names and check whether current merchant was shown: 您可以创建另一个数组来存储显示的商人名称,并检查是否显示了当前商人:

$shownMerchants = array();

foreach($array as $final_array)
{
    if($final_array[0]>=80 && !in_array($final_array[1], $shownMerchants))
    {
        echo $final_array[0];
        echo $final_array[2][0];
        echo $final_array[2][1];
        $shownMerchants[] = $final_array[1];
    }
}

Also, as @arkascha said, it would be better to store the percentage as integer. 同样,正如@arkascha所说,最好将百分比存储为整数。

$arrayInput =array(
    0=>array(0=>"100",
               1=>"snd",
               2=>array('id'=>"1",'name'=>"xperia")

    ),
    1=>array(0=>"100",
               1=>"snd",
               2=>array('id'=>"2",'name'=>"xperia")
    ),

    2=>array(0=>"100",
               1=>"fkt",
               2=>array('id'=>"3",'name'=>"xperia")
    ),
    3=>array(0=>"100",
               1=>"fkt",
              2=>array('id'=>"4",'name'=>"xperia")
    ),
    4=>array(0=>"90",
               1=>"snd",
               2=>array('id'=>"5",'name'=>"xperia")
    ),
    5=>array(0=>"90",
               1=>"fkt",
               2=>array('id'=>"6",'name'=>"xperia")
    )
);  

$Merchants = array();

foreach($arrayInput as $key => $val)
{
    $m_name = array_map(function ($ar) {return $ar[1].'-'.$ar[0];}, $Merchants); ///get all the merchant name and percentage from $Merchants new array

    if($val[0] >= 80  && !in_array($val[1].'-'.$val[0],$m_name)) /// if % greater than 80 and same merchant with same % not exists than push into array
    {
        $Merchants[] = $val;
    }
}
echo "<pre>"; print_r($Merchants);

This will give you: 这将为您提供:

Array
(
    [0] => Array
        (
            [0] => 100
            [1] => snd
            [2] => Array
                (
                    [id] => 1
                    [name] => xperia
                )

        )

    [1] => Array
        (
            [0] => 100
            [1] => fkt
            [2] => Array
                (
                    [id] => 3
                    [name] => xperia
                )

        )

    [2] => Array
        (
            [0] => 90
            [1] => snd
            [2] => Array
                (
                    [id] => 5
                    [name] => xperia
                )

        )

    [3] => Array
        (
            [0] => 90
            [1] => fkt
            [2] => Array
                (
                    [id] => 6
                    [name] => xperia
                )

        )

)

LIVE EXAMPLE : CLICK HERE 现场示例: 点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用数组“回显”中的记录以用于另一个php select中 - How to use a record from a “echo” of a array to use into another php select 如何从PHP中的字符串创建特定数组 - How to create particular array from string in php 如果不可用,如何选择具有特定ID的记录,然后默认选择记录 - how to select record of particular id if not availabe then select by default record 如何选择此特定php数组的最后一个元素? - How can I select the last element of this particular php array? 如何从PHP中的源数组搜索特定的索引值数组 - How to search particular index value array from source array in php 如何使用php从结果列表中选择特定选择 - How to select a particular choice from a list of results using php 如何从数据库中为php中输入的特定字段选择条目 - how to select entries from database for particular fields entered in php 如何通过从php下拉列表中选择特定月份来显示数据库中特定月份的数据 - How can display the data of particular month from database by select the particular month through drop down in php 如何从XML获取数据并将其转换为php中特定节点的数组? - How to get data from XML and convert in to array for particular node in php? 如何从php中的对象数组中获取特定的键值 - how to get particular key value from an object array in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM