Jump to content

[SOLVED] "Cropping" two dimensional array...


cgm225

Recommended Posts

I have a two dimensional array with multiple entries, something created like:

 

$this->Data[$id] = array(
                'dir'       => $dir,
                'filename'  => $filename
            );

 

And then I find a particular key and value in that array like so:

 

return $this->Data[$this->id];

 

However, how can I reset the $this->Data array to hold just the $this->Data[$this->id] value, and none of the other values that were also in the array, and also still maintaining the two dimensional structure.

Well, here is my overall problem, I have a class with two methods, findAlbums and findAlbum, and I need both to return the data in the same "format" so they can be outputted the same way.

 

Any ideas?  I have commented where my problem is:

 

    public function findAlbums() {
        /* Queries database and creates a two dimensional array of all albums
         * and each album's associated data.
         */ 
        $query = 'SELECT id, full_title, short_title, dir, timestamp
                    FROM albums ORDER BY timestamp DESC';
        $statement = $this->connection->prepare($query);
        $statement->bind_result($id, $fullTitle, $shortTitle, $dir, $timestamp);
        $statement->execute();
        while($statement->fetch()) {
            $this->albumData[$dir] = array(
                'id'         => $id,
                'fullTitle'  => $fullTitle,
                'shortTitle' => $shortTitle,
                'dir'        => $dir,
                'timestamp'  => $timestamp
            );
        }

        /* If a dirctory name has been provided via a GET variable, then only
         * the album corresponding to that directory name will be returned in the
         * array.
         */
        if(isset($this->dir)) {
            $this->findAlbum();
        } else {
            return $this->albumData;
        }
    }

    public function findAlbum() {
        //Returns a specific album and its associated data
        return $this->albumData[$this->dir];  //PROBLEM AREA; THIS NEEDS TO BE SOMETHING ELSE??
    }

Using:

 

    public function findAlbum() {
        //Returns a specific album and its associated data
        $this->albumData = array($this->dir => $this->albumData[$this->dir]);
        return $this->albumData;
    }

 

Results in a "Warning: Invalid argument supplied for foreach() in C:\public_html\development\Gallery.php5 on line 181" when I try to output the data in a foreach loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.