php多維數組重組

在平時的php開發中,我們通常會調用第三方的API來滿足我們的業務需求,但是會遇到返回數據格式不統一的情況,特別是第三方api接口返回數據兼容我們的api接口

1:請求第三方API接口返回的數據格式:

array(1) {
  [0] =>
  array(20) {
    'url' =>
    string(147) "http:/*************************************************/*****"
    'filemtime' =>
    int(1525688855)
    'app' =>
    string(11) "smarket_dev"
    'stream' =>
    string(23) "stream20180507102518_67"
    'start' =>
    int(1525688855)
    'end' =>
    int(1525689358)
    'm3u8' =>
    string(147) "http://*******************************/**************************/"
    'duration' =>
    int(503)
    'thumbnail' =>
    string(100) "https://cdn-************************/********************"
    'size' =>
    int(9259195)
    'width' =>
    int(640)
    'height' =>
    int(360)
    'begin' =>
    int(0)
    'uptime' =>
    int(1525689364)
    'update' =>
    int(1525689364)
    'id' =>
    string(24) "5af02c1415d5239acc6ee28e"
    'title' =>
    string(9) "未定義"
    'desc' =>
    string(9) "未定義"
    'case' =>
    string(1) "0"
    'caseName' =>
    string(3) "無"
  }
}

第三方API接口返回的一般都是數組格式的字符串,我們就可以用數組去處理,處理成我們想要的格式

2:在類中寫一個處理的方法,調用這個方法即可,返回的數據就是我們想要的:

function getRecordInfo($webcastId)
    {
        $app = 'webinar';
        $stream = $webcastId;

        $_access_id = '***********';
        $_access_key = '*************';
        $_openApiUrl = 'http://*************/*******/';

        $service = new \webinar\_services\webCast\Impl\AodianyunApi($_access_id, $_access_key, $_openApiUrl);
        $result = $service->vodGetInfo($app, $stream);
        foreach ($result as $value) {
            $results[] = [
                'createdTime' => $value['filemtime'],
                'id' => $value['stream'],
                'recordStartTime' => $value['start'],
                'recordEndTime' => $value['end'],
                'size' => $value['size'],
                'subject' => $value['title'],
                'url' => $value['url']
            ];
        }
        return $results;
    }

3:getRecordInfo返回的數據:

array(100) {
  [0] =>
  array(7) {
    'createdTime' =>
    int(1527072944)
    'id' =>
    string(6) "stream"
    'recordStartTime' =>
    int(1527072944)
    'recordEndTime' =>
    int(1527073551)
    'size' =>
    int(131098618)
    'subject' =>
    string(9) "未定義"
    'url' =>
    string(105) "https://cdn-************************/********************"
  }
  [1] =>
  array(7) {
    'createdTime' =>
    int(1526029294)
    'id' =>
    string(6) "stream"
    'recordStartTime' =>
    int(1526029294)
    'recordEndTime' =>
    int(1526029826)
    'size' =>
    int(114636073)
    'subject' =>
    string(9) "未定義"
    'url' =>
    string(105) "https://cdn-************************/********************"
  }

4:思路圖:

定義處理第三方接口的getRecordInfo()=》在getRecordInfo()中請求第三方api
=》將第三方的api返回的數據給到result= result數據進行格式處理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章