PHP:将Google表格结果对象转换为简单数组

时间:2019-04-24 18:05:30

标签: php arrays multidimensional-array google-sheets google-sheets-api

我在Google工作表中包含以下数据: enter image description here

我正在用简单的代码从Google驱动器中提取它:

   $ranges = [
    'Price_1'
   ];
   $params = array(
    'ranges' => $ranges
   );

   $response = $service->spreadsheets_values->batchGet($spreadsheetId, $params);

响应是一个复杂的对象:

object(Google_Service_Sheets_BatchGetValuesResponse)#58 (8) { ["collection_key":protected]=> string(11) "valueRanges" ["spreadsheetId"]=> string(44) "1BQpKLuvlaVeGqIqoGUOynF9IZ5C3zK6gagZiRJO1UEU" ["valueRangesType":protected]=> string(32) "Google_Service_Sheets_ValueRange" ["valueRangesDataType":protected]=> string(5) "array" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["valueRanges"]=> array(1) { [0]=> object(Google_Service_Sheets_ValueRange)#67 (7) { ["collection_key":protected]=> string(6) "values" ["majorDimension"]=> string(4) "ROWS" ["range"]=> string(14) "Price_1!E2:E74" ["values"]=> array(65) { [0]=> array(1) { [0]=> string(2) "28" } [1]=> array(1) { [0]=> string(2) "14" } [2]=> array(1) { [0]=> string(1) "0" } [3]=> array(0) { } [4]=> array(1) { [0]=> string(2) "55" } [5]=> array(1) { [0]=> string(2) "28" } [6]=> array(1) { [0]=> string(1) "0" } [7]=> array(0) { } [8]=> array(0) { } [9]=> array(0) { } [10]=> array(0) { } [11]=> array(0) { } [12]=> array(0) { } [13]=> array(0) { } [14]=> array(0) { } [15]=> array(0) { } [16]=> array(0) { } [17]=> array(0) { } [18]=> array(0) { } [19]=> array(0) { } [20]=> array(1) { [0]=> string(2) "59" } [21]=> array(1) { [0]=> string(2) "49" } [22]=> array(0) { } [23]=> array(1) { [0]=> string(2) "38" } [24]=> array(1) { [0]=> string(2) "38" } [25]=> array(0) { } [26]=> array(1) { [0]=> string(2) "37" } [27]=> array(1) { [0]=> string(2) "25" } [28]=> array(1) { [0]=> string(1) "0" } [29]=> array(0) { } [30]=> array(1) { [0]=> string(2) "79" } [31]=> array(1) { [0]=> string(2) "99" } [32]=> array(0) { } [33]=> array(1) { [0]=> string(2) "75" } [34]=> array(1) { [0]=> string(2) "99" } [35]=> array(0) { } [36]=> array(1) { [0]=> string(2) "79" } [37]=> array(1) { [0]=> string(2) "99" } [38]=> array(0) { } [39]=> array(1) { [0]=> string(3) "160" } [40]=> array(1) { [0]=> string(3) "190" } [41]=> array(0) { } [42]=> array(0) { } [43]=> array(0) { } [44]=> array(0) { } [45]=> array(0) { } [46]=> array(0) { } [47]=> array(0) { } [48]=> array(0) { } [49]=> array(0) { } [50]=> array(0) { } [51]=> array(0) { } [52]=> array(1) { [0]=> string(2) "33" } [53]=> array(0) { } [54]=> array(1) { [0]=> string(2) "59" } [55]=> array(1) { [0]=> string(2) "34" } [56]=> array(1) { [0]=> string(1) "0" } [57]=> array(0) { } [58]=> array(1) { [0]=> string(2) "29" } [59]=> array(1) { [0]=> string(2) "14" } [60]=> array(1) { [0]=> string(1) "0" } [61]=> array(0) { } [62]=> array(1) { [0]=> string(2) "37" } [63]=> array(1) { [0]=> string(2) "25" } [64]=> array(1) { [0]=> string(1) "0" } } ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } } } 

var_dump($response->getValueRanges()[0]['values']);给出以下结果:

array(74) { [0]=> array(5) { [0]=> string(2) "ID" [1]=> string(10) "Ticket for" [2]=> string(9) "Excursion" [3]=> string(8) "Duration" [4]=> string(14) "Price (Brutto)" } [1]=> array(5) { [0]=> string(3) "01A" [1]=> string(13) "adult ( 12+ )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "28" } [2]=> array(5) { [0]=> string(3) "01B" [1]=> string(18) "child ( age 5-11 )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "14" } [3]=> array(5) { [0]=> string(3) "01C" [1]=> string(18) "toodler ( age 0-4)" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(1) "0" } [4]=> array(0) { } [5]=> array(5) { [0]=> string(3) "02A" [1]=> string(13) "adult, age 8+" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "55" } [6]=> array(5) { [0]=> string(3) "02B" [1]=> string(14) "child, age 3-7" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "28" } [7]=> array(5) { [0]=> string(3) "02C" [1]=> string(16) "toodler, age 0-2" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(1) "0" } [8]=> array(0) { } [9]=> array(3) { [0]=> string(3) "03A" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [10]=> array(3) { [0]=> string(3) "03B" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [11]=> array(3) { [0]=> string(3) "03C" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [12]=> array(0) { } [13]=> array(3) { [0]=> string(3) "04A" [1]=> string(0) "" [2]=> string(9) "Submarine" } [14]=> array(3) { [0]=> string(3) "04B" [1]=> string(0) "" [2]=> string(9) "Submarine" } [15]=> array(3) { [0]=> string(3) "04C" [1]=> string(0) "" [2]=> string(9) "Submarine" } [16]=> array(0) { } [17]=> array(3) { [0]=> string(3) "05A" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [18]=> array(3) { [0]=> string(3) "05B" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [19]=> array(3) { [0]=> string(3) "05C" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [20]=> array(0) { } [21]=> array(5) { [0]=> string(3) "06A" [1]=> string(9) "fisherman" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "59" } [22]=> array(5) { [0]=> string(3) "06B" [1]=> string(9) "spectator" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "49" } [23]=> array(0) { } [24]=> array(5) { [0]=> string(3) "07A" [1]=> string(5) "adult" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [25]=> array(5) { [0]=> string(3) "07B" [1]=> string(13) "child, age 6+" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [26]=> array(0) { } [27]=> array(5) { [0]=> string(3) "08A" [1]=> string(14) "adult, age 12+" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "37" } [28]=> array(5) { [0]=> string(3) "08B" [1]=> string(15) "child, age 3-11" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "25" } [29]=> array(5) { [0]=> string(3) "08C" [1]=> string(16) "toodler, age 0-1" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(1) "0" } [30]=> array(0) { } [31]=> array(5) { [0]=> string(3) "09A" [1]=> string(17) "flight from 800m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "15min" [4]=> string(2) "79" } [32]=> array(5) { [0]=> string(3) "09B" [1]=> string(18) "flight from 1000m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "25min" [4]=> string(2) "99" } [33]=> array(0) { } [34]=> array(5) { [0]=> string(3) "10A" [1]=> string(21) "Single (for 1 person)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "75" } [35]=> array(5) { [0]=> string(3) "10B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "99" } [36]=> array(0) { } [37]=> array(5) { [0]=> string(3) "11A" [1]=> string(21) "Single (for 1 person)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "79" } [38]=> array(5) { [0]=> string(3) "11B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "99" } [39]=> array(0) { } [40]=> array(5) { [0]=> string(3) "12A" [1]=> string(21) "Single (for 1 person)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "160" } [41]=> array(5) { [0]=> string(3) "12B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "190" } [42]=> array(0) { } [43]=> array(3) { [0]=> string(3) "13A" [1]=> string(0) "" [2]=> string(8) "Trekking" } [44]=> array(3) { [0]=> string(3) "13B" [1]=> string(0) "" [2]=> string(8) "Trekking" } [45]=> array(3) { [0]=> string(3) "13C" [1]=> string(0) "" [2]=> string(8) "Trekking" } [46]=> array(0) { } [47]=> array(3) { [0]=> string(3) "14A" [1]=> string(0) "" [2]=> string(7) "Karting" } [48]=> array(3) { [0]=> string(3) "14B" [1]=> string(0) "" [2]=> string(7) "Karting" } [49]=> array(3) { [0]=> string(3) "14C" [1]=> string(0) "" [2]=> string(7) "Karting" } [50]=> array(0) { } [51]=> array(3) { [0]=> string(3) "15A" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [52]=> array(3) { [0]=> string(3) "15B" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [53]=> array(5) { [0]=> string(3) "15C" [1]=> string(0) "" [2]=> string(11) "Jungle park" [3]=> string(0) "" [4]=> string(2) "33" } [54]=> array(0) { } [55]=> array(5) { [0]=> string(3) "16A" [1]=> string(14) "adult, age 12+" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "59" } [56]=> array(5) { [0]=> string(3) "16B" [1]=> string(15) "child, age 2-11" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "34" } [57]=> array(5) { [0]=> string(3) "16C" [1]=> string(16) "toodler, age 0-1" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(1) "0" } [58]=> array(0) { } [59]=> array(5) { [0]=> string(3) "17A" [1]=> string(14) "adult, age 12+" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "29" } [60]=> array(5) { [0]=> string(3) "17B" [1]=> string(15) "child, age 2-11" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "14" } [61]=> array(5) { [0]=> string(3) "17C" [1]=> string(16) "toodler, age 0-1" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(1) "0" } [62]=> array(0) { } [63]=> array(5) { [0]=> string(3) "18A" [1]=> string(14) "adult, age 12+" [2]=> string(11) "Loro parque" [3]=> string(12) "8:30 - 18:45" [4]=> string(2) "37" } [64]=> array(5) { [0]=> string(3) "18B" [1]=> string(15) "child, age 6-11" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(2) "25" } [65]=> array(5) { [0]=> string(3) "18C" [1]=> string(16) "toodler, age 0-1" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(1) "0" } [66]=> array(0) { } [67]=> array(3) { [0]=> string(3) "19A" [1]=> string(0) "" [2]=> string(9) "Antologia" } [68]=> array(3) { [0]=> string(3) "19B" [1]=> string(0) "" [2]=> string(9) "Antologia" } [69]=> array(3) { [0]=> string(3) "19C" [1]=> string(0) "" [2]=> string(9) "Antologia" } [70]=> array(0) { } [71]=> array(3) { [0]=> string(3) "20A" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [72]=> array(3) { [0]=> string(3) "20B" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [73]=> array(3) { [0]=> string(3) "20C" [1]=> string(0) "" [2]=> string(13) "Medieval show" } }   

并且:

for($i = 1; $i < 5; $i++) {
    print_r ($response->getValueRanges()[0]['values'][$i]);
}

赠予:Array ( [0] => 01A [1] => adult ( 12+ ) [2] => Catamaran [3] => 2h [4] => 28 ) Array ( [0] => 01B [1] => child ( age 5-11 ) [2] => Catamaran [3] => 2h [4] => 14 ) Array ( [0] => 01C [1] => toodler ( age 0-4) [2] => Catamaran [3] => 2h [4] => 0 ) Array ( )

结果,我需要一个简单的数组,该数组将保存一个键[行id],来自ID行的ID和来自最后一行的价格,类似这样的图形:$result[$i] = unique_id=1, ID=01A, price=28

如何实现?

1 个答案:

答案 0 :(得分:1)

因此,使用以下方法获取Google表格的数据后:

$arr = $response->getValueRanges()[0]['values'];

您可以使用此简单的for循环来获取所需的数据:

// notice start from 1 to avoid the headers
for($i = 1; $i < count($arr); $i++) {
    $res[$i] = array("unique_id" => $i, "ID" => $arr[$i][0], "price" => $arr[$i][4]);
}

现在$res将包含您的结构。