Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何按键和值过滤php关联数组?_Php_Arrays - Fatal编程技术网

如何按键和值过滤php关联数组?

如何按键和值过滤php关联数组?,php,arrays,Php,Arrays,我有以下数据: Array ( [0] => Array ( [name] => Hair Transplantation [price] => € 1000 - 4000 ) [1] => Array ( [name] => Rhinoplasty [price] => € 2500

我有以下数据:

Array
(
    [0] => Array
        (
            [name] => Hair Transplantation
            [price] => € 1000 - 4000
        )

    [1] => Array
        (
            [name] => Rhinoplasty
            [price] => € 2500
        )

    [2] => Array
        (
            [name] => Otoplasty
            [price] => € 1000
        )

)
我想通过
price
键过滤这个数组,因此,我只想返回一个项目,即价格最低的项目


谢谢大家!

我不喜欢这个数据结构,正如我在对这个问题的评论中指出的那样。这会导致字符串解析,这并不理想

然而,我通过使用

以下代码将在变量
$lower\u item
中生成所需的结果。如果其中任何一个具有相同的价格(或相同的起始价格),则定义的第一个价格将是返回的价格(但可以编辑以调整获得优先权的价格)

试试这个:

$items
是您的基本数组

$items = [
    0 => [
        "name" => "Hair Transplantation",
        "price" => "€ 1000 - 4000"
    ],

    1 => [
        "name" => "Rhinoplasty",
        "price" => "€ 2500"
    ],
    2 => [
        "name" => "Otoplasty",
        "price" => "€ 1000"
    ]
];

$lowestPrice = PHP_INT_MAX;
$rightKey = -1;

foreach ( $items as $key => $item )
{
    $clean = str_replace('€ ', '', $item['price']);
    $chunks = explode('-', $clean);

    if ( count($chunks) == 2 )
        $price = floatval(trim($chunks[0]));
    else
        $price = floatval(trim($clean));

    if ( $price < $lowestPrice )
    {
        $lowestPrice = $price;
        $rightKey = $key;
    }
}

$lowestItem = $items[$rightKey];

print_r($lowestItem);
$items=[
0 => [
“名称”=>“毛发移植”,
“价格”=>“€1000-4000”
],
1 => [
“名称”=>“鼻整形术”,
“价格”=>“2500欧元”
],
2 => [
“名称”=>“耳成形术”,
“价格”=>“€1000”
]
];
$lowestPrice=PHP\u INT\u MAX;
$rightKey=-1;
foreach($key=>$item的项目)
{
$clean=str_替换(“€”和“$item['price]”);
$chunks=分解('-',$clean);
if(计数($chunks)==2)
$price=floatval(trim($chunks[0]);
其他的
$price=floatval(修剪($clean));
如果($价格<$最低价格)
{
$lowestPrice=$price;
$rightKey=$key;
}
}
$Lowstitem=$items[$rightKey];
印刷费($Lowstitem);
编辑:正则表达式版本

$items = [
    0 => [
        "name" => "Hair Transplantation",
        "price" => "€ 1000 - 4000"
    ],

    1 => [
        "name" => "Rhinoplasty",
        "price" => "€ 2500"
    ],
    2 => [
        "name" => "Otoplasty",
        "price" => "€ 1000"
    ]
];

$lowestPrice = PHP_INT_MAX;
$rightKey = -1;

foreach ( $items as $key => $item )
{
    $matches = [];

    if ( !preg_match('#([0-9\.]+) #', str_replace(',', '.', $item['price']), $matches) )
        continue;

    $price = floatval($matches[0]);

    if ( $price < $lowestPrice )
    {
        $lowestPrice = $price;
        $rightKey = $key;
    }
}

$lowestItem = $items[$rightKey];

print_r($lowestItem);
$items=[
0 => [
“名称”=>“毛发移植”,
“价格”=>“€1000-4000”
],
1 => [
“名称”=>“鼻整形术”,
“价格”=>“2500欧元”
],
2 => [
“名称”=>“耳成形术”,
“价格”=>“€1000”
]
];
$lowestPrice=PHP\u INT\u MAX;
$rightKey=-1;
foreach($key=>$item的项目)
{
$matches=[];
如果(!preg_match(“#([0-9\.]+)#”,str_replace(“,”,“,”,$item['price']),$matches))
继续;
$price=floatval($matches[0]);
如果($价格<$最低价格)
{
$lowestPrice=$price;
$rightKey=$key;
}
}
$Lowstitem=$items[$rightKey];
印刷费($Lowstitem);

我想第一件商品的价格范围有多大?与单一价格的商品相比,该如何排序?这里的价格是字符串而不是整数。“你为什么要这样做?”Don'tPanic,嗨,谢谢你的回复。对第一个项目(在本例中)具有范围,从该范围中获取最低的项目,并用其他单一价格筛选该数字。@vivek_23,是的,这是一个带“€”符号的字符串,我想删除该符号并返回将被筛选的唯一数字。可以这样做,但它需要一些不可靠的字符串解析——特别是在范围内。您应该认真考虑如何巧妙而简短地修复此数据结构。它确实假设如果给定了一个范围,则首先写入最低值(从左到右)。不过,我认为这是一个安全的假设。干得好!(加上我的1):)可以将
uasort
中的所有行替换为
return(int)trim($a['price'],“€”)(int)trim($b['price'],“€”)如果你只需要最小值,我不支持排序。排序太贵了。@vivek_23,我敢打赌,与输入/输出成本相比,排序毫无价值。你的担心就像得到两个巨无霸和零可乐:)这就完成了,但它有点分散,而且对
价格
字符串的格式“知道得太多”。给定unideal字符串解析的方式,代码对格式的了解越少越好。(无可否认,如果数字中出现小数或逗号,我的答案中的正则表达式将爆炸,但我们可以预先计划)。不过干得好+1我做了一些修正,一次写下。。。但是如果你不确定,很明显,用一个regex替换这个研究,这个regex得到第一个完整的数字出现在项目['line']中,这里是用一个regexI删除了9999999的PHP_INT_MAX,更明确地说明了这个意图。。。。并改进了处理逗号或点值的方式。
    $result = array_reduce($price, 'getMinimumPrice', null);

    function getMinimumPrice(?array $itemA, array $itemB): array
    {
        if (is_null($itemA)) {
            return $itemB;
        }
        return getPrice($itemA) < getPrice($itemB)
            ? $itemA
            : $itemB;
    }
    
    function getPrice(array $item): int
    {
        preg_match("/\d+/", $item['price'], $matches);
        return $matches[0];
    }
$primary = [
    [
        'name' => 'Hair Transplantation',
        'price' => '€ 1000 - 4000',
    ],
    [
        'name' => 'Rhinoplasty',
        'price' => '€ 2500',
    ],
    [
        'name' => 'Otoplasty',
        'price' => '€ 1000',
    ],
    /* ... 155 items */
];

function getMinimumPrice(?array $itemA, array $itemB): array
{
    if (is_null($itemA)) {
        return $itemB;
    }
    return getPrice($itemA) < getPrice($itemB)
        ? $itemA
        : $itemB;
}

function getPrice(array $item): int
{
    preg_match("/\d+/", $item['price'], $matches);
    return $matches[0];
}

$timeRepeat = 1000;
$reduce = 0;
for ($i = 0; $i < $timeRepeat; $i++) {
    $start = microtime(true);
    $price = $primary;
    array_reduce($price, 'getMinimumPrice', null);
    $reduce += microtime(true) - $start;
}

$uasort = 0;
for ($i = 0; $i < $timeRepeat; $i++) {
    $start = microtime(true);
    $price = $primary;
    uasort($price, function($a, $b) {
        preg_match("/\d+/", $a['price'], $matchesA);
        preg_match("/\d+/", $b['price'], $matchesB);
        return (int)$matchesB[0] <=> (int)$matchesA[0];
    });
    array_pop($price);
    $uasort += microtime(true) - $start;
}

print_r([
    'uasort' => $uasort,
    'reduce' => $reduce,
    'difference' => round($uasort / $reduce, 12),
]);
Array (
       [uasort] => 8.0096476078033
       [reduce] => 2.1610336303711
       [difference] => 3.706396557294
)
$items = [
    0 => [
        "name" => "Hair Transplantation",
        "price" => "€ 1000 - 4000"
    ],

    1 => [
        "name" => "Rhinoplasty",
        "price" => "€ 2500"
    ],
    2 => [
        "name" => "Otoplasty",
        "price" => "€ 1000"
    ]
];

$lowestPrice = PHP_INT_MAX;
$rightKey = -1;

foreach ( $items as $key => $item )
{
    $clean = str_replace('€ ', '', $item['price']);
    $chunks = explode('-', $clean);

    if ( count($chunks) == 2 )
        $price = floatval(trim($chunks[0]));
    else
        $price = floatval(trim($clean));

    if ( $price < $lowestPrice )
    {
        $lowestPrice = $price;
        $rightKey = $key;
    }
}

$lowestItem = $items[$rightKey];

print_r($lowestItem);
$items = [
    0 => [
        "name" => "Hair Transplantation",
        "price" => "€ 1000 - 4000"
    ],

    1 => [
        "name" => "Rhinoplasty",
        "price" => "€ 2500"
    ],
    2 => [
        "name" => "Otoplasty",
        "price" => "€ 1000"
    ]
];

$lowestPrice = PHP_INT_MAX;
$rightKey = -1;

foreach ( $items as $key => $item )
{
    $matches = [];

    if ( !preg_match('#([0-9\.]+) #', str_replace(',', '.', $item['price']), $matches) )
        continue;

    $price = floatval($matches[0]);

    if ( $price < $lowestPrice )
    {
        $lowestPrice = $price;
        $rightKey = $key;
    }
}

$lowestItem = $items[$rightKey];

print_r($lowestItem);
$people = array(
    0 => array(
      'name' => 'Hair Transplantation',
      'price' => '€ 1000 - 4000'
    ),
    1=> array(
      'name' => 'Rhinoplasty',
      'price' => '€ 2500'
    ),
    2=> array(
        'name' => 'Otoplasty',
        'price' => '€ 1000'
    )
);

$price = array_map(function($value) {
    if (strpos($value, '-') !== false) {
        $value = explode("-", $value)[0];
    }
    list($symbol, $price) = sscanf($value,'%[^0-9]%s');
    return intval($price);
}, array_column($people, 'price'));

$found_key = array_search(min($price),$price);

print_r($people[$found_key]) ;