2016-06-15 48 views
2

我有三個序列化的數據結構。將序列化數據添加到數組 - PHP

a:2:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";} 


a:2:{i:0;s:163:"[["10","0","0","0","30","0","0","60","0","0","0","0"],["20","0","0","30","0","0","20","0","0","0","50","0"],["30","0","0","0","20","0","0","30","0","20","0","30"]]";i:1;s:154:"[["20","0","0","0","0","0","0","0","0","0","0","0"],["30","0","0","0","0","0","0","0","0","0","0","0"],["40","0","0","0","0","0","0","0","0","0","0","0"]]";} 


a:4:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";i:2;s:151:"[["1","1","1","0","1","1","1","0","0","0","1","1"],["1","1","1","0","0","1","1","1","1","0","1","1"],["1","1","1","1","0","0","1","1","1","1","1","1"]]";i:3;s:151:"[["1","0","1","0","0","0","1","0","1","0","0","2"],["1","0","0","2","1","0","1","0","1","1","0","1"],["1","0","2","1","1","1","0","1","0","1","1","1"]]";} 

我想將所有三個序列化數據添加到單個序列化的array

我試過這個代碼和它的工作,但我想能夠添加額外的數據。

$data2=unserialize($value['monthly_forecast']); 
$data1=unserialize($temp['monthly_forecast']); 
//print_r($data1); 
$combinedData = array($data1, $data2); 
$monthly_forecast=serialize($combinedData); 
$temp['monthly_forecast']=$monthly_forecast; 
+3

反序列化他們。結合起來;然後再次序列化它/他們 –

+0

「結合它們」是什麼意思?合併它們還是創建一個包含它們的數組? – apokryfos

+0

選中此鏈接[Serialize](http://stackoverflow.com/a/1817545/5139148)。這個問題已經回答 –

回答

0

什麼unserialize然後array_merge(),然後serialize合併陣列回來?需要注意的是array_merge()可用於

合併一個或多個陣列

,所以你應該能夠在你的情況來應用它。

編輯:

你有幾個選項。你可以將unserializemerge所有東西放到一個單一的數組中,然後serialize即可。或者,你還可以:

$array = array($serialized_data_1, $serialized_data_2, $serialized_data_3); 
$all_serialized = serialize($array); 

,然後訪問數據:

$all_unserialized = unserialize($array); 
$unserialized_data_1 = unserialize(all_unserialized[0]); 
$unserialized_data_2 = unserialize(all_unserialized[1]); 
$unserialized_data_3 = unserialize(all_unserialized[2]); 
+0

但我想添加序列化數據也。 –