【问题标题】:Php group array of object对象的php组数组
【发布时间】:2021-12-12 16:08:00
【问题描述】:

我在下面提到了我的 php 数组对象,

$arr ='[
{
    "id": 4667,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "255",
    "height": "35",
    "rim": "19",
},
{
    "id": 4668,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "275",
    "height": "35",
    "rim": "19",
},
{
    "id": 4669,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "255",
    "height": "35",
    "rim": "19",
},
{
    "id": 4670,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "275",
    "height": "35",
    "rim": "19",
}]';

我想根据宽度将数组分成前后分开,如下所示。

$front = '[
{
    "id": 4667,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "255",
    "height": "35",
    "rim": "19",
},
{
    "id": 4669,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "255",
    "height": "35",
    "rim": "19",
}]';

$rear = '[
{
    "id": 4668,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "275",
    "height": "35",
    "rim": "19",
},
{
    "id": 4670,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "275",
    "height": "35",
    "rim": "19",
}]';

php 有什么方法可以比较每个对象的宽度值并将它们分组以匹配。请指教。感谢您宝贵的时间。

【问题讨论】:

  • 根据什么分类为正面或背面?
  • 请向我们展示您的实际代码。谢谢。
  • 注意:给定的代码不是有效的 PHP 代码。
  • Hello Syscall,请稍等,我将更新我的 php 代码
  • 你好 Geert,我们可以根据轮胎宽度拆分前后

标签: php arrays object


【解决方案1】:

我认为您可以使用以下代码:

$arr='[{
    "id": 4667,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "255",
    "height": "35",
    "rim": "19"
}, {
    "id": 4668,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "275",
    "height": "35",
    "rim": "19"
}, {
    "id": 4669,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "255",
    "height": "35",
    "rim": "19"
}, {
    "id": 4670,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "275",
    "height": "35",
    "rim": "19"
}]'; 
$front=[];
$rear=[];
foreach(json_decode($arr,true) as $key=>$val)
{
  
  if($val['width']==255)
  {
    $front[]=$val;
  }
  else{
    $rear[]=$val;
  }
}  

$front=json_encode($front);
$rear=json_encode($rear);

【讨论】:

  • 感谢您的重播,我需要根据宽度拆分数组
  • 你可以根据array_chunk中的宽度传递参数。
  • 如果元素总数为 10,那么您需要 5 个在前面,5 个在后面,对吧?
  • 有时我们会得到可用的产品 3 个前轮胎和 5 个后轮胎,在数据库中返回单个数组中的所有 8 个元素我需要将产品前后分开显示,我们可以仅使用宽度来识别前后
  • 我已经更新了代码,请检查。您可以根据要求将条件放在前面。
【解决方案2】:

这对我有用,但需要简化

 $all = []; $front = []; $rear = [];

        for($i=0; $i<count($arr); $i++){
            array_push($all, $arr[$i]->width);
        }

        $filter = array_unique($all);
        $max = max($filter);
        $min = min($filter);
        
        for($j=0; $j<count($arr); $j++){
            if($arr[$j]->width == $max){
                array_push($rear, $arr[$j]);
            } else if($arr[$j]->width == $min){
                array_push($front, $arr[$j]);
            } else {
                echo 'Not Match';
            }
        }

        echo "<script>console.log(".json_encode($front).")</script>";
        echo "<script>console.log(".json_encode($rear).")</script>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2012-01-26
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多