March 27, 2012

Codeiginiter array $data view

Question by Uffo

So I need an array from my data that I can foreach on the view, an array like this: album_name,cover_image

Here is my code:

function myFunction(){

    $alb = array();

    foreach($albums as $album)
    {
        //if($album[0]['count'] > 0){
        $alb[]['album_name'] = $album['name'];

        foreach($this->get_fbimages($facebook,$album['id']) as $img)
        {
            $alb[]['cover'] = $img[0]['picture'];
        }   
    }

    return $alb;
}

and I do array_merge($data,myFunction());

Answer by Starx

The structure of your array is invalid, to the result you want.

Try it this way.

function myFunction(){

    $alb = array();

    foreach($albums as $album)
    {
        $tempArray['album_name'] = $album['name'];
        foreach($this->get_fbimages($facebook,$album['id']) as $img)
        {
            $tempArray['cover'][] = $img[0]['picture'];
        }

        $alb[] = $tempArray;

    }

    return $alb;
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!