简体   繁体   中英

How to remove field by key from JSON multiple array in PHP [laravel]

I`m scratching my head all day and cant figure out. Can someone help me please.

I need to make that if all values is null in same column then do not show date and those fields with NULL.

In below example need to remove 2015-09-17 value becouse log1, log2, log3, log4, log5 has empty values in 5 position of that field.

0 - column - "2015-09-12"
1 - column - "2015-09-13"
2 - column - "2015-09-14"
3 - column - "2015-09-15"
4 - column - "2015-09-16"
5 - column - "2015-09-17"
6 - column - "2015-09-18"



{
  "error": false,
  "stats": {
    "x": [
      "2015-09-12",
      "2015-09-13",
      "2015-09-14",
      "2015-09-15",
      "2015-09-16",
      "2015-09-17",
      "2015-09-18"
    ],
    "log1": [
      null,
      null,
      null,
      null,
      null,
      null,
      null
    ],
    "log2": [
      "3.0000",
      null,
      null,
      "0.0000",
      "0.7500",
      null,
      "3.0000"
    ],
    "log3": [
      null,
      null,
      null,
      "200",
      null,
      null,
      null
    ],
    "log4": [
      1,
      null,
      null,
      5,
      4,
      null,
      2
    ],
    "log5": [
      null,
      null,
      null,
      null,
      null,
      null,
      null
    ]
  }
}

Fixed problem by using this function:

private function array_intersect_by_key_position($model)
{

    $result_array = array_intersect_assoc($model['log1'], $model['log2'], $model['log3'], $model['log4'], $model['log5']);

    //dd($result_array);
    if(count($result_array) > 0)
    {
        foreach($result_array as $res => $val)
        {
            unset($model['x'][$res]);
            unset($model['log1'][$res]);
            unset($model['log2'][$res]);
            unset($model['log3'][$res]);
            unset($model['log4'][$res]);
            unset($model['log5'][$res]);
        }

        $model['x'] = array_values($model['x']);
        $model['log1'] = array_values($model['log1']);
        $model['log2'] = array_values($model['log2']);
        $model['log3'] = array_values($model['log3']);
        $model['log4'] = array_values($model['log4']);
        $model['log5'] = array_values($model['log5']);

        return $model;

    }
    else
    {
        return $model;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM