drf65218 2017-02-23 22:34
浏览 91
已采纳

将foreach分解为新数组

I have JSON API response which look something like this

Array
(
    [sections] => Array
        (
            [0] => Array
                (
                    [id] => 115000089967
                    [url] => xxxxx
                    [html_url] => ArticleHTML1
                    [category_id] => 204458828
                    [position] => 0
                    [sorting] => manual
                    [created_at] => 2016-12-19T14:56:23Z
                    [updated_at] => 2017-02-03T08:23:04Z
                    [name] => ArticleName1
                    [description] => 
                    [outdated] => 
                )

            [1] => Array
                (
                    [id] => 207077828
                    [url] => xxxxxx
                    [html_url] => ArticleHTML2
                    [category_id] => 204458828
                    [position] => 1
                    [sorting] => manual
                    [created_at] => 2016-11-14T09:28:30Z
                    [updated_at] => 2017-02-02T09:15:42Z
                    [name] => ArticleName2
                    [description] => 
                    [outdated] => 
                )
        )

    [page] => 1
    [per_page] => 30
    [page_count] => 1
    [sort_by] => position
    [sort_order] => asc
)

I have successfully iterated this with foreach, so return looks like this:

ArticleName1 ArticleHTML1

ArticleName2 ArticleHTML2

So I took [name] and [html_url] from each like this:

$details1 = array('name');
$details2 = array('html_url');

foreach($sections['sections'] as $article) {
  foreach($details1 as $detail) {
    echo "$article[$detail] ";
  }
    foreach($details2 as $detail) {
    echo "$article[$detail]
";
  }
} 

But what I want next is that, that response should be exploded to one single array like this:

Array
(
    [0] => ArticleName1 ArticleHTML1
    [1] => ArticleName2 ArticleHTML2
)

I already managed to explode those to individual arrays:

foreach($sections['sections'] as $article) {
  foreach($details1 as $detail) {
    $name = "$article[$detail] ";
  }
    foreach($details2 as $detail) {
    $url = "$article[$detail]
";
  }
  $string = $name . $url;
  $array = explode(' ', $string, 1);
  print_r($array);
} 

but I need just one array.

How? I'm lost or just doesn't understand, am I even close?

EDIT

The thing here is that the JSON dump is pretty large and I only need few things (name and url). So I was thinking that I first grab the whole thing, then I just take the names and urls (like the first foreach is doing), and then put those back to array. Because from those names and urls I need only last 12 keys, and taking those from sinlge array would be easy.

Tho it would be perfect, if I could sort out the keys which I don't want, in the first place. That would solve my problem. Then I wouldn't need a new array etc.

  • 写回答

1条回答 默认 最新

  • duanjianhe1388 2017-02-23 22:41
    关注

    You're making this much more difficult than it needs to be. It's just a single loop:

    $array = array();
    foreach ($sections['sections'] as $article) {
        $array[] = $article['name'] . ' ' . $article['html_url'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?