2015-10-13 54 views
-2

我有代碼,我假設一些不是數組。 我嘗試使用循環foreach,但它無效。 我提取序列化數據的代碼,如何提取這個數組?

array (
'last_submit' => '1', 
'feeds_changed' => '1', 
'streams' => '{"id1":{"id":"1","name":"Test Stream","feeds":"[{\\"content\\":\\"testing\\",\\"id\\":\\"tm02801\\",\\"type\\":\\"facebook\\",\\"filter-by-words\\":\\"testing2\\"},{\\"content\\":\\"testing3\\",\\"id\\":\\"oe29415\\",\\"type\\":\\"instagram\\",\\"filter-by-words\\":\\"9gag\\"},{\\"content\\":\\"testing\\",\\"retweets\\":\\"nope\\",\\"replies\\":\\"nope\\",\\"id\\":\\"nq62491\\",\\"type\\":\\"twitter\\",\\"filter-by-words\\":\\"testing\\"},{\\"content\\":\\"testing\\",\\"id\\":\\"nt14171\\",\\"type\\":\\"pinterest\\"}]","posts":"40","cache":"yep","cache-lifetime":"10","private":"nope","hide-on-desktop":"nope","hide-on-mobile":"nope","heading":"Stream Demo","headingcolor":"rgb(154, 78, 141)","subheading":"","subheadingcolor":"rgb(114, 112, 114)","hhalign":"center","bgcolor":"rgb(229, 229, 229)","mobileslider":"nope","viewportin":"yep","layout":"grid","theme":"classic","gc-style":"style-4","width":"260","margin":"20","cardcolor":"rgb(255, 255, 255)","namecolor":"rgb(154, 78, 141)","textcolor":"rgb(85, 85, 85)","linkscolor":"rgb(94, 159, 202)","restcolor":"rgb(132, 118, 129)","shadow":"rgba(0, 0, 0, 0.22)","bcolor":"rgba(240, 237, 231, 0.4)","talign":"left","css":""}}', 
'streams_count' => '1', 
'consumer_key' => '', 
'consumer_secret' => '', 
'oauth_access_token' => '', 
'oauth_access_token_secret' => '', 
'instagram_access_token' => '', 
) 

,但如何在流獲得價值值波紋管?

'streams' => '{"id1":{"id":"1","name":"Test Stream","feeds":"[{\\"content\\":\\"testing\\",\\"id\\":\\"tm02801\\",\\"type\\":\\"facebook\\",\\"filter-by-words\\":\\"testing2\\"},{\\"content\\":\\"testing3\\",\\"id\\":\\"oe29415\\",\\"type\\":\\"instagram\\",\\"filter-by-words\\":\\"9gag\\"},{\\"content\\":\\"testing\\",\\"retweets\\":\\"nope\\",\\"replies\\":\\"nope\\",\\"id\\":\\"nq62491\\",\\"type\\":\\"twitter\\",\\"filter-by-words\\":\\"testing\\"},{\\"content\\":\\"testing\\",\\"id\\":\\"nt14171\\",\\"type\\":\\"pinterest\\"}]","posts":"40","cache":"yep","cache-lifetime":"10","private":"nope","hide-on-desktop":"nope","hide-on-mobile":"nope","heading":"Stream Demo","headingcolor":"rgb(154, 78, 141)","subheading":"","subheadingcolor":"rgb(114, 112, 114)","hhalign":"center","bgcolor":"rgb(229, 229, 229)","mobileslider":"nope","viewportin":"yep","layout":"grid","theme":"classic","gc-style":"style-4","width":"260","margin":"20","cardcolor":"rgb(255, 255, 255)","namecolor":"rgb(154, 78, 141)","textcolor":"rgb(85, 85, 85)","linkscolor":"rgb(94, 159, 202)","restcolor":"rgb(132, 118, 129)","shadow":"rgba(0, 0, 0, 0.22)","bcolor":"rgba(240, 237, 231, 0.4)","talign":"left","css":""}}', 
+2

看看PHPS'json_decode()'函數:HTTP ://php.net/manual/en/function.json-decode.php注意,儘管你會返回一個_object_,而不是_array_。 – arkascha

+0

完美,謝謝 –

回答

0

json_decode()可以幫助你。使用此功能可訪問陣列的密鑰。

$array = array (
    'last_submit' => '1', 
    'feeds_changed' => '1', 
    'streams' => '{"id1":{"id":"1","name":"Test Stream","feeds":"[{\\"content\\":\\"testing\\",\\"id\\":\\"tm02801\\",\\"type\\":\\"facebook\\",\\"filter-by-words\\":\\"testing2\\"},{\\"content\\":\\"testing3\\",\\"id\\":\\"oe29415\\",\\"type\\":\\"instagram\\",\\"filter-by-words\\":\\"9gag\\"},{\\"content\\":\\"testing\\",\\"retweets\\":\\"nope\\",\\"replies\\":\\"nope\\",\\"id\\":\\"nq62491\\",\\"type\\":\\"twitter\\",\\"filter-by-words\\":\\"testing\\"},{\\"content\\":\\"testing\\",\\"id\\":\\"nt14171\\",\\"type\\":\\"pinterest\\"}]","posts":"40","cache":"yep","cache-lifetime":"10","private":"nope","hide-on-desktop":"nope","hide-on-mobile":"nope","heading":"Stream Demo","headingcolor":"rgb(154, 78, 141)","subheading":"","subheadingcolor":"rgb(114, 112, 114)","hhalign":"center","bgcolor":"rgb(229, 229, 229)","mobileslider":"nope","viewportin":"yep","layout":"grid","theme":"classic","gc-style":"style-4","width":"260","margin":"20","cardcolor":"rgb(255, 255, 255)","namecolor":"rgb(154, 78, 141)","textcolor":"rgb(85, 85, 85)","linkscolor":"rgb(94, 159, 202)","restcolor":"rgb(132, 118, 129)","shadow":"rgba(0, 0, 0, 0.22)","bcolor":"rgba(240, 237, 231, 0.4)","talign":"left","css":""}}', 
    'streams_count' => '1', 
    'consumer_key' => '', 
    'consumer_secret' => '', 
    'oauth_access_token' => '', 
    'oauth_access_token_secret' => '', 
    'instagram_access_token' => '', 
); 

$json = json_decode($array['streams']); 

echo $json->id1->name; // Output : Test Stream 

希望它能幫助你解決你的問題。

編號:http://php.net/manual/en/function.json-decode.php

0

您可以使用此:

$phpArray= json_decode($array['streams'],true); 
print_r($phpArray); 
foreach ($phpArray as $key => $value) { 
    echo "<p>$key | $value</p>"; 
} 
0

使用給定的代碼:

 if(!empty($array['streams'])){ 
     $phpArray= json_decode($array['streams'],true); 
     print_r($phpArray); 
    }