php - 数组不输出所有值

时间:2017-04-13 15:21:02

标签: php arrays

我有一个小问题,我似乎无法解决。我有一些数据从数据库拉出来,在我显示数据之前,我想在那里操纵一些值。这是我的功能处理这些:

public function getPageContent($id) 
{
    $menutopic_model = new Menu_Topic();
    $content = $menutopic_model->getAllContent($id);

    //var_dump($content);        

    $data = array();

    foreach ($content as $value)
    {
         $title = explode(',', $value->title);
         $filename = explode(',', $value->filename);

         $data['topicname'] = $value->topicname;
         $data['title'] = $title;
         $data['filename'] = $filename;

    }    

   //var_dump($data);

}

现在,当我转储var_dump($content);时,我得到了应有的整个数据数组:

array(2) {
    [0]=>
    object(stdClass)#346 (3) {
      ["topicname"]=>
      string(19) "Signs giving orders"
      ["title"]=>
      string(42) "Stop,No entry,No cycling,No motor vehicles"
      ["filename"]=>
      string(179) "DQP0GVcUA2dG8ZfqeVYLO68YodgYnJMOjJw2o2iC.png,vWUcHGX3VVKPT08JXh9mAqZ40pT0vfDJ78Yoqovz.png,bplX8bbwHzHKX9n6SvvQiYNhkWwKxi2bhsrQ94U2.png,8KAEovxQn3EgzHoZg1euSgNYTFupnLdKusJ4SIEP.png"
    }
    [1]=>
    object(stdClass)#345 (3) {
      ["topicname"]=>
      string(13) "Warning Signs"
      ["title"]=>
      string(111) "Road narrows ahead both sides,Road narrows on right (left if symbol reversed),Crossroads,Junction on bend ahead"
      ["filename"]=>
      string(179) "uZ7fnDjlYfKU3pdSQ7wAq4siFryA6jSEitmRyPhp.png,4pslo6cC0I2E606DscWxiDD3sCBW7ZGaESQES4r0.png,xdVBMpV6PTLtX48M67kfWOqy59rsYMDZMuWbqDwl.png,hLtIGikARgARqmSqit4mqNXRFJthipH6O5vqxlbN.png"
    }
  }

但在通过foreach传递数据后,此var_dump($data);仅返回数组中的最后一项:

array(3) {
  ["topicname"]=>
  string(13) "Warning Signs"
  ["title"]=>
  array(4) {
    [0]=>
    string(29) "Road narrows ahead both sides"
    [1]=>
    string(47) "Road narrows on right (left if symbol reversed)"
    [2]=>
    string(10) "Crossroads"
    [3]=>
    string(22) "Junction on bend ahead"
  }
  ["filename"]=>
  array(4) {
    [0]=>
    string(44) "uZ7fnDjlYfKU3pdSQ7wAq4siFryA6jSEitmRyPhp.png"
    [1]=>
    string(44) "4pslo6cC0I2E606DscWxiDD3sCBW7ZGaESQES4r0.png"
    [2]=>
    string(44) "xdVBMpV6PTLtX48M67kfWOqy59rsYMDZMuWbqDwl.png"
    [3]=>
    string(44) "hLtIGikARgARqmSqit4mqNXRFJthipH6O5vqxlbN.png"
  }
}

我应该在上面改变什么来获得正确的结果?

2 个答案:

答案 0 :(得分:1)

您只获得了最后一个值,因为您使用以下代码覆盖了数组中的值。

$data['topicname'] = $value->topicname;
$data['title'] = $title;
$data['filename'] = $filename;

更改了代码:

public function getPageContent($id) 
{
    $menutopic_model = new Menu_Topic();
    $content = $menutopic_model->getAllContent($id);      

    $data = array();

    foreach ($content as $value)
    {
         $title = explode(',', $value->title);
         $filename = explode(',', $value->filename);

         $data[]=array(
             'topicname'=>$value->topicname,
             'title'=>$title,
             'filename'=>$filename);
    }    

     //var_dump($data);

 }

答案 1 :(得分:0)

每次循环时,您都在重复使用相同的数组。因此,创建您的数组,然后将其添加为真实数组的新发生

public function getPageContent($id) 
{
    $menutopic_model = new Menu_Topic();
    $content = $menutopic_model->getAllContent($id);

    //var_dump($content);        

    foreach ($content as $value)
    {
        $title = explode(',', $value->title);
        $filename = explode(',', $value->filename);

        $d = array();
        $d['topicname'] = $value->topicname;
        $d['title'] = $title;
        $d['filename'] = $filename;

        $data[] = $d;
    }    

   //var_dump($data);

}