我对数据库进行查询,然后打印出来。如您所见,p : False
可以组成一个数组。
page_id => 2
我希望数组看起来像这样。
Array
(
[page_id] => 3
[name] => Add Me
[article_category] => Celebrities
[count_article_category] => 1
[average_ctr] => 5.555560111999512
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Celebrities
[count_article_category] => 7
[average_ctr] => 2.525434238570077
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Desi
[count_article_category] => 1
[average_ctr] => 2.892319917678833
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Lifestyle
[count_article_category] => 1
[average_ctr] => 2.3632400035858154
)
Array
(
[page_id] => 2
[name] => I Too Had a Love Story
[article_category] => Sports
[count_article_category] => 1
[average_ctr] => 2.150439977645874
)
Array
(
[page_id] => 1
[name] => Indian Celebrities
[article_category] => Celebrities
[count_article_category] => 2
[average_ctr] => 2.7356200218200684
)
Array
(
[page_id] => 1
[name] => Indian Celebrities
[article_category] => Hilarious
[count_article_category] => 1
[average_ctr] => 0.7684919834136963
)
我尝试了以下在foreach循环中运行的代码。我明白我的逻辑或编码是错误的,但我无法理解我的错误 请帮忙。
[2]
(
[Celebrities] => 7
[Lifestyle] => 1
[Desi] => 1
)
答案 0 :(得分:0)
好的,我们走了:
$pages_data = array();
foreach ($datas as $data) {
if (isset($data['page_id'])) {
// create new element for $pages_data[$data['page_id']] if it does not exists
if (!isset($pages_data[$data['page_id']])) {
$pages_data[$data['page_id']] = array();
}
// if it exists - just add certain values:
$pages_data[$data['page_id']][$data['article_category']] = $data['count_article_category'];
}
}