2015-09-01 75 views
1

最近幾天我一直在砸我的腦袋,因爲我一直無法爲我的問題創建解決方案。分裂/子串數組 - PHP

我有一個數組,看起來像這樣:

[0] => {"count": 1038, "previous": null, "results": [{"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "acbb6a8790604ac9916772f51729e69a", "reference": "[email protected]", "value": "0.0000", "member": "a7cab5fa045d44beb72da3ea26e6a49c", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"} 
[1] => {"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "5ba77c689aba48f8ab845c9578b96d1d", "reference": "[email protected]", "value": "0.0000", "member": "477039a270d841aaa87a5578ee034d60", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"} 

我要的是刪除陣列

[0] => {"count": 1038, "previous": null, "results": 

的第一部分,以及品牌和數組,看起來像這樣:

Array 
(
[0] => Array 
(
[data_source] => f31904c94a72490e8fa5750399a14e44 
[uuid] => acbb6a8790604ac9916772f51729e69a 
[reference] => [email protected] 
[value] => 0.0000 
[member] => a7cab5fa045d44beb72da3ea26e6a49c 
[HPSalesBudget] => 1.00 
[team] => null 
[date] => 2015-08-28T00:00:00 
) 

[1] => Array 
(
[data_source] => f31904c94a72490e8fa5750399a14e44 
[uuid] => acbb6a8790604ac9916772f51729e69a 
[reference] => [email protected] 
[value] => 0.0000 
[member] => a7cab5fa045d44beb72da3ea26e6a49c 
[HPSalesBudget] => 1.00 
[team] => null 
[date] => 2015-08-28T00:00:00 
) 

爲什麼我的數組看起來像這樣的唯一原因是因爲這是我獲取數據的格式。

我試過用google搜索這個問題,但沒有任何運氣。

有沒有人有我的問題的解決方案?

+0

該數組是「數組」類型還是「字符串」? – kezi

+0

我認爲你的數組字符串有什麼不對,因爲在第一行結尾的括號之後應該有一個逗號,或者至少有另一個括號關閉它...... – kezi

回答

0

分割數據與類似explode("\n", $data);

線然後處理由線的數據線,條帶的每一行的前7個字符,從而使你的數據是隻有這樣的:

{"count": 1038, "previous": null, "results": [{"data_source": "f31904c94a72490e8fa5750399a14e44", "uuid": "acbb6a8790604ac9916772f51729e69a", "reference": "[email protected]", "value": "0.0000", "member": "a7cab5fa045d44beb72da3ea26e6a49c", "HPSalesBudget": "1.00", "team": null, "date": "2015-08-28T00:00:00"} 

然後每行使用json_decode

從那裏,這很容易得到的格式的數據,您需要:

$result = array(); 
$result[] = $json['results']; 

我猜的代碼應該是這樣的:

$result = array(); 
$lines = explode("\n", $data); 
foreach ($lines as $line) { 
    $line = substr($line, 7); 
    $json = json_decode($line, true); 
    $result[] = $json['results']; 
} 
0

試試這個:

 $result = json_decode((your_json), true); //decode your json to array 

    $newArr = []; 
    foreach($result['results'] as $key => $val) { 
     $newArr[] = ['data_source'=> $val['data_source'] 
        'uuid' => $val['uuid'], 
        'reference' => $val['reference'], 
        'value' => $val['value'], 
        'member' => $val['member'], 
        'HPSalesBudget' => $val['HPSalesBudget'], 
        'team' => $val['team'], 
        'date'=> $val['date']] ; 
    } 
    print_r($newArr); 
+0

感謝您的回答。 我不是那麼入Json,我的json字符串應該怎麼樣? –

+0

這是關於你的問題。這是一個json類型的數組。所以如果你只想實現數組。解碼。 – aldrin27

0

應該如此簡單:

$myarray[0] = $myarray[0]["results"];