将 foreach 分解为新数组
Explode foreach to new array
我有 JSON API 看起来像这样的回复
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
)
我已经用 foreach 成功地迭代了它,所以 return 看起来像这样:
ArticleName1 ArticleHTML1
ArticleName2 ArticleHTML2
所以我像这样分别取了 [name] 和 [html_url]:
$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]\n";
}
}
但我接下来想要的是,该响应应该像这样分解为一个数组:
Array
(
[0] => ArticleName1 ArticleHTML1
[1] => ArticleName2 ArticleHTML2
)
我已经设法将它们分解为单独的数组:
foreach($sections['sections'] as $article) {
foreach($details1 as $detail) {
$name = "$article[$detail] ";
}
foreach($details2 as $detail) {
$url = "$article[$detail]\n";
}
$string = $name . $url;
$array = explode(' ', $string, 1);
print_r($array);
}
但我只需要一个数组。
怎么样?我迷路了或者只是不明白,我什至接近了吗?
编辑
这里的问题是 JSON 转储非常大,我只需要很少的东西(名称和 url)。所以我在想我首先抓住整个东西,然后我只拿名字和 urls(就像第一个 foreach 所做的那样),然后把它们放回大批。因为从这些名称和 url 中我只需要最后 12 个键,并且从单个数组中获取它们很容易。
如果我能首先整理出我不想要的键,那将是完美的。那会解决我的问题。那么我就不需要一个新的数组等
你让这件事变得比它需要的要困难得多。这只是一个循环:
$array = array();
foreach ($sections['sections'] as $article) {
$array[] = $article['name'] . ' ' . $article['html_url'];
}
我有 JSON API 看起来像这样的回复
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
)
我已经用 foreach 成功地迭代了它,所以 return 看起来像这样:
ArticleName1 ArticleHTML1
ArticleName2 ArticleHTML2
所以我像这样分别取了 [name] 和 [html_url]:
$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]\n";
}
}
但我接下来想要的是,该响应应该像这样分解为一个数组:
Array
(
[0] => ArticleName1 ArticleHTML1
[1] => ArticleName2 ArticleHTML2
)
我已经设法将它们分解为单独的数组:
foreach($sections['sections'] as $article) {
foreach($details1 as $detail) {
$name = "$article[$detail] ";
}
foreach($details2 as $detail) {
$url = "$article[$detail]\n";
}
$string = $name . $url;
$array = explode(' ', $string, 1);
print_r($array);
}
但我只需要一个数组。
怎么样?我迷路了或者只是不明白,我什至接近了吗?
编辑
这里的问题是 JSON 转储非常大,我只需要很少的东西(名称和 url)。所以我在想我首先抓住整个东西,然后我只拿名字和 urls(就像第一个 foreach 所做的那样),然后把它们放回大批。因为从这些名称和 url 中我只需要最后 12 个键,并且从单个数组中获取它们很容易。
如果我能首先整理出我不想要的键,那将是完美的。那会解决我的问题。那么我就不需要一个新的数组等
你让这件事变得比它需要的要困难得多。这只是一个循环:
$array = array();
foreach ($sections['sections'] as $article) {
$array[] = $article['name'] . ' ' . $article['html_url'];
}