-4

我设置了一个 for each 循环,以将值插入到我创建的自定义数组中。出于某种原因,当我之后使用我设置的自定义键之一在数组上运行 for 循环时,我没有得到任何结果。

我在 for each 循环中的数组

$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'];
}

我希望能够像下面的示例一样提取所有的posting_ids 和posting_likes

Array (
  [0] => 
       [posting_id] => 1 
       [posting_likes] => 15 
  [1] => 
       [posting_id] => 2 
       [posting_likes] => 19 
)

填充此数组的结果是从 foreach 循环中提取的,因此它需要能够在数组中存储多个值。

4

2 回答 2

1

检查这个例子,它可能会对你有所帮助。

<?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'];
  }
于 2013-03-30T08:50:12.757 回答
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']);
}

这样您就可以打印 $posting_ids 它会像这样打印

Array (
  [0] => 
       [posting_id] => 1 
       [posting_likes] => 15 
  [1] => 
       [posting_id] => 2 
       [posting_likes] => 19 
)
于 2013-03-30T08:37:26.867 回答