如何提取这个数组?

时间:2015-10-13 09:18:15

标签: php arrays ajax

我有代码,我假设其中一些不是数组。 我尝试使用循环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":""}}',

3 个答案:

答案 0 :(得分:0)

json_decode()可以帮到你。使用此函数可以访问数组的 stream 键。

$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

答案 1 :(得分:0)

您可以使用:

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

答案 2 :(得分:0)

使用给定的代码:

$final_array=array();
foreach (array as $arr){
  foreach($arr as $aaa){
    $final_array[]=array($aaa,$arr[0])
  }
}