我为每个循环设置了一个插入值,以便将值插入到我创建的自定义数组中。出于某种原因,当我使用我设置的一个自定义键在阵列上运行for循环后,我没有得到任何结果。
每个循环中的我的数组
$posting_ids[] .= "[posting_id] => ".$value['posting_id'].", [posting_likes] => ".$value['total_likes']."";
在for循环中回显数组
for($a=0; $a < count($posting_ids); $a++) {
echo $posting_ids['posting_id'];
}
我希望能够像下面的示例一样提取所有的posts_ids和posting_likes
Array (
[0] =>
[posting_id] => 1
[posting_likes] => 15
[1] =>
[posting_id] => 2
[posting_likes] => 19
)
填充此数组的结果是从foreach循环中提取的,因此它需要能够在数组中存储多个值。
答案 0 :(得分:1)
选中此example,它可能会对您有所帮助。
<?php
$posting_ids[0]['posting_id' ] = 1;
$posting_ids[0]['total_likes'] = 10;
$posting_ids[1]['posting_id' ] = 2;
$posting_ids[1]['total_likes'] = 20;
for($a=0; $a < count($posting_ids); $a++){
echo $posting_ids[$a]['posting_id'];
}
答案 1 :(得分:0)
相反
$posting_ids[] .= "[posting_id] => ".$value['posting_id'].", [posting_likes] => ".$value['total_likes']."";
这样做 -
$posting_ids['posting_id'] = $value['posting_id'];
$posting_ids['posting_likes'] = $value['total_likes'];
编辑:根据您更新的帖子,您可以这样做
while() { //your loop
$posting_ids = array('posting_id' => $value['posting_id'], 'posting_likes'=>$value['total_likes']);
}
这样你打印$ post_id就会打印出
Array (
[0] =>
[posting_id] => 1
[posting_likes] => 15
[1] =>
[posting_id] => 2
[posting_likes] => 19
)