【问题标题】:How to extract this array? [closed]如何提取这个数组? [关闭]
【发布时间】:2015-10-13 09:18:15
【问题描述】:

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

但是如何在下面的 stream 值中获取值?,

'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":""}}',

【问题讨论】:

标签: php arrays ajax


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你可以用这个:

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

    【讨论】:

      【解决方案3】:

      使用给定的代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 2021-03-02
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多