【问题标题】:PHP How to select values that are > or < from a multidimensional arrayPHP如何从多维数组中选择>或<的值
【发布时间】:2018-12-11 16:01:38
【问题描述】:

我不知道如何获得所需的值。我正在尝试根据它的值过滤我的数组。

#current array
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1
            [2] => 10
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2
            [2] => 20       
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3  
            [2] => 30    
        )

    [3] => Array
        (
            [0] => Product4
            [1] => Description product 4  
            [2] => 40    
        )

    [4] => Array
        (
            [0] => Product5
            [1] => Description product 5  
            [2] => 50    
        )
)

#resultant array
Array
(
    [0] => Array
        (
            [0] => Product3 
            [1] => Description product 3
            [2] => 30
        )

    [1] => Array
        (
            [0] => Product4
            [1] => Description product 4
            [2] => 40       
        )
)

正如您在我的代码块中看到的,我正在尝试创建一个由 >= 和 #resultant array 仅包含 [2] 大于 (>=) 大于 30 且小于或等于 (

我确实找到了无维数组的答案,但我不知道如何在我的应用程序中使用它,请参阅:php numeric array select values greater than a number and lower than another and save it to a new array

我只是不知道如何编写/构建这段代码,我也想要两个变量;例如,$min = 30 和 $max = 40。

我希望我已经提供了足够的信息,如果没有,请随时发表评论。感谢您的阅读,我希望我能找到一些可以帮助我的东西。

干杯科迪

【问题讨论】:

  • 请出示您尝试过的代码。
  • 您可以遍历数组元素并使用您想要的条件进行过滤。

标签: php arrays multidimensional-array filter


【解决方案1】:

您可以使用array_filter 根据条件过滤您的数组:

<?php
    $data = array
    (
        0 => array
        (
            0 => 'Product1',
            1 => 'Description product 1',
            2 => '10'
        ),
        1 => array
        (
            0 => 'Product2',
            1 => 'Description product 2',
            2 => '20'
        ),
        2 => array
        (
            0 => 'Product3',
            1 => 'Description product 3',
            2 => '30'
        ),
        3 => array
        (
            0 => 'Product4',
            1 => 'Description product 4',
            2 => '40'
        ),
        4 => array
        (
            0 => 'Product5',
            1 => 'Description product 5',
            2 => '50'
        )
    );

    $data = array_filter($data, function($el)
    {
        return ($el[2] >= 30 && $el[2] <= 40);
    });

    echo '<pre>'. print_r($data, 1) .'</pre>';

在这里,您可以将一个函数传递给第二个回调并以这种方式设置您的条件。

【讨论】:

  • 谢谢!这对我帮助很大!您是否知道也许知道如何跳过包含产品 1 的第一个数组,所以它会留下来?干杯
  • @Cody 你是什么意思?你想保持第一个数组完好无损吗?只需将结果存储在一个新的变量中?
【解决方案2】:

您可以只使用 foreach 和 if 条件检查代码如下:

$array=Array();
$array[]=array('Product1','Description product 1',10);
$array[]=array('Product2','Description product 2',20);
$array[]=array('Product3','Description product 3',30);
$array[]=array('Product4','Description product 4',40);
$array[]=array('Product5','Description product 5',50);

// code
$array2=array();
foreach($array as $row)
{

    if($row[2] >=30 and $row[2] <=40)
        $array2[]=$row;


}

#resultant array
print_r($array2);

die;

【讨论】:

    【解决方案3】:

    例如

    $array = [
      0 => "item1",
    ];
    

    要定位字符串“item1”,您可以使用 with 调用数组中的第一项

    echo $array[0];
    

    例如,要使用多维数组,您可以这样做

    $array = [
        0 => [
            0 => 'item1',
            1 => 'item2',
        ]
        1 => [
            0 => 'item3',
            1 => 'item4',
        ]
    ]
    

    例如,要定位字符串“item3”,您可以使用它

    $array[1][0];
    

    这里你定位$array内第二个数组中的第一项

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2014-11-17
      • 1970-01-01
      相关资源
      最近更新 更多