【问题标题】:require value and respective key from array in php需要php中数组的值和相应的键
【发布时间】:2018-05-14 15:13:56
【问题描述】:

我有一个如下所示的数组,我想要最小值,它是用于搜索税类 ID 的索引

Array
(
    [tax_class_id] => Array
        (
            [0] => 12
            [1] => 13
            [2] => 13
        )
    [price] => Array
        (
            [0] => 6233
            [1] => 3195
            [2] => 19192
        )

)

我正在 tax_class_id 中搜索最低价格和相应的键。在这个 Senario 中,我需要最低的价格,即 3195 和 tax_id - 13,即密钥 [1]

我的代码是

$prod_total = array();
for($i = 1;$i <= $chunk;$i++){ 
    if($i == 1) {
        $min_product_amt = min($product_amt['price']);
        $index = array_search($min_product_amt, $product_amt);

        $product_total =  $min_product_amt; 
        //ceil Round numbers up to the nearest integer 
        $prod_total['price'] = ceil($product_total * $discount/100);
        $prod_total['tax_id'] = $product_amt['tax_class_id'];
        //Remove the first element from an array
        array_shift($product_amt['price']); 
        array_shift($product_amt['tax_class_id']);          
    } else { 
        $second_min_product_amt = min($product_amt['price']);
        $index = array_search($min_product_amt, $product_amt);

        $product_total = $second_min_product_amt;
        $prod_total['price'] = ceil($product_total * $discount/100);
        $prod_total['tax_id'] = $product_amt['tax_class_id'];
        array_shift($product_amt['price']); 
        array_shift($product_amt['tax_class_id']);              
    }
}
print_r($prod_total);
    die;

【问题讨论】:

  • 你能显示你需要什么输出吗?
  • 从一个实际的问题/问题陈述开始怎么样?
  • 你想解决什么问题?您的问题似乎与最小值和索引有关,但我在您的代码中看不到问题...
  • @rajkumar 没有一个答案适合您的需求?

标签: php arrays min


【解决方案1】:
$array=Array
(
    'tax_class_id' => Array
    (
        0 => 12,
            1 => 13,
            2 => 13
        ),
    'price' => Array
(
    0 => 6233,
            1 => 3195,
            2 => 19192
        )

);

$minValue= min($array['price']);
$minKey=array_keys($array['price'], $minValue);
$tax_id=$array['tax_class_id'][$minKey[0]];
echo $tax_id;

此代码适用于您的问题。首先我得到嵌套数组price 的最小值,然后它关联key。之后我只需访问嵌套数组tax_class_id 并获取我需要的字段的值,就像访问每个数组一样。

【讨论】:

    【解决方案2】:
    $data = [
        "tax_class_id" => [
            12,
            13,
            13
        ],
        "price" => [
            6233,
            3195,
            19192
        ]
    ];
    
    $lowestFound;
    foreach($data["price"] as $i => $price){
        if(!$lowestFound || $lowestFound[1] > $price)
            $lowestFound = [$i,$price];
    }
    echo $data["tax_class_id"][$lowestFound[0]];
    

    此代码在一个周期内获取最低价格键的 tax_class_id。

    【讨论】:

      【解决方案3】:

      我认为 array_column 给你一个很好的输出。

      $array=Array
      (
      'tax_class_id' => Array(
              0 => 12,
              1 => 13,
              2 => 13
          ),
      'price' => Array(
              0 => 6233,
              1 => 3195,
              2 => 19192
          )
      
      );
      // Find minimum value
      $min= min($array['price']);
      // Find key of min value
      $Key=array_search($min, $array['price']);
      // Extract all values with key[min value]
      $new = array_column($array, $Key);
      Var_dump($new);
      

      $new 中的输出现在将是

      array(2) {
           [0]=> int(13)
           [1]=> int(3195)
      }
      

      基本上这两个值都是您要查找的值。
      https://3v4l.org/NsdiS

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-07
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-28
        相关资源
        最近更新 更多