suo
suo

Reputation: 609

php - array not outputting all values

I have a small issue i can't seem to work around. i have some data i'm pulling from the database and before i display the data, i want to manipulate some values in there. here's my function handling these:

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);

}

Now, when i dump var_dump($content);, i get the entire array of data as it should be:

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"
    }
  }

but after passing the data through the foreach, this var_dump($data); only returns the last item in the array:

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"
  }
}

what should i change in the above to get the correct result?

Upvotes: 0

Views: 43

Answers (2)

Sahil Gulati
Sahil Gulati

Reputation: 15141

You were getting only last value just because you were over-writing values in array with the below code, which you were using.

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

Changed code:

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);

 }

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94642

You are reusing the same array occurance each time round the loop. So create your array and then add it asa new occurance to the real array

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);

}

Upvotes: 0

Related Questions