格式化数组php并将其分离

时间:2018-06-21 07:03:32

标签: php arrays

我收到这样的数组

undefined

所以我想做的是有一个单独的数组[ {"date":"2018-06-16","val":"no","amount":2}, {"date":"2018-06-18","val":"no","amount":30}, {"date":"2018-06-18","val":"yes","amount":2}, {"date":"2018-06-19","val":"no","amount":26}, {"date":"2018-06-19","val":"yes","amount":3}, {"date":"2018-06-20","val":"yes","amount":4} ] 仅包含日期,但不应重复使用$date[]完成的操作,所以我有一个看起来像

array_unique()

但是您可以看到并非所有日期的值都为 $date = [2018-06-16,2018-06-18,2018-06-19,2018-06-20] yes 所以我想要两个数组no$yes[],所以如果日期不具有值yes或no,则它应该为零,所以最终结果将是

$no[]

我希望我的问题很清楚

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

export default class CustomService{
    getData(onSuccess, onError){
        //axios.get(...) or fetch(...)
        if(success) call onSuccess(result)
        if(error) call onError(error)
    }
}

export default class MyComponent extends React.Component{
   constructor(props){
       super(props);

       this.state = {
           showError:false
       }

       this.customService = new CustomService();

       this.onSuccess = this.onSuccess.bind(this);
       this.onError = this.onError.bind(this);
   }

   componentDidMount(){
       this.customService.getData(this.onSuccess, this.onError);
   }

   onSuccess(result){
       // Do something if necessary
       // Call the redux action
   }

   onError(error){
       //Show a toast message to user or do something else
       this.setState({showError:true});
   }
}

这将导致:

$json = '[{"date":"2018-06-16","val":"no","amount":2},{"date":"2018-06-18","val":"no","amount":30},{"date":"2018-06-18","val":"yes","amount":2},{"date":"2018-06-19","val":"no","amount":26},{"date":"2018-06-19","val":"yes","amount":3},{"date":"2018-06-20","val":"yes","amount":4}]';

//Convert the json string into an associative array
$arr = json_decode($json, true);

//Get all unique dates
//Use array_column to convert the associative array into a simple array
//Use array_unique to get the unique values from the simple array
$date = array_unique(array_column($arr , "date"));

//Make a temp array to combine the date and val as the key. Use the amout as the value
$temp = array_reduce($arr, function($c, $v) {
    $c[ $v['date'] . '-' . $v['val'] ] = $v['amount'];
    return $c;
}, array());

//Just do a simple foreach to loop thru the $date
//Check if the key exist from the temp array using isset()
$yes = array();
$no = array();
foreach( $date as $val ) {
    if ( isset( $temp[ $val . '-yes' ] ) ) $yes[] = $temp[ $val . '-yes' ];
    else $yes[] = 0;

    if ( isset( $temp[ $val . '-no' ] ) ) $no[] = $temp[ $val . '-no' ];
    else $no[] = 0;
}

答案 1 :(得分:0)

我的第一次尝试是将array_map(…)函数与array_filter(…)结合使用(假设您的数组中已经有唯一的日期):

/** @var $receivedData list of received dates */

$dates = array_map(function(array $date) {
    return $date['date'];
}, $receivedData);

$yes = array_filter(array_map(function(array $date) {
    return $date['value'] == 'yes' ? $date['amount'] : null;
}, $receivedData);

$no = array_filter(array_map(function(array $date) {
    return $date['value'] == 'no' ? $date['amount'] : null;
}, $receivedData);

请注意以下事实:如果不将回调传递给array_filter(…),也会过滤零值。您可以通过比较null的函数来解决该问题。

此外,这只是第一枪。就性能而言,它不是最佳的。或类似的东西。我想最好只迭代整个数组一次,然后根据需要将所有项目推入先前定义的数组$dates$yes$no