Harry
Harry

Reputation: 735

PDO Returned array into more accessible array

Information

This should be a fairly simple question. On my website I have a post_categorys in my database that has a structure like below

    ID    |    Name
    1     |   Sports
    2     |   Health
    3     |   Movies
    4     |   Music

Now when I save a blog post I save the category_id such as 1 for movies in the blog_post table.

When printing out the blog post to my users I want to be able to get the blog post category name by something like $categorys[$blog_cat_id']['name']

Here is how I get all categorys for $categorys used above.

function getCategorysArray(){
    $STH = $this->database->prepare('SELECT * FROM project_categorys');
    $STH->execute();
    return $STH->fetchAll();
}

But this returns the array in this format

Array
(
[0] => Array
    (
        [id] => 1
        [0] => 1
        [name] => Sports
        [1] => Sports
    )

[1] => Array
    (
        [id] => 2
        [0] => 2
        [name] => Health
        [1] => Health
    )

[2] => Array
    (
        [id] => 3
        [0] => 3
        [name] => Movies
        [1] => Movies
    )

[3] => Array
    (
        [id] => 4
        [0] => 4
        [name] => Music
        [1] => Music
    )

Therefor I am unable to use my desired method to get the category name ($categorys[$blog_cat_id']['name'])

Question

Is there a way I can remap the array somehow so that $array[1]['name'] = Sports would be the output?

Upvotes: 0

Views: 65

Answers (3)

lonesomeday
lonesomeday

Reputation: 238045

So, basically, you want the array indexed by the value of id?

First, you can simplify the array by calling fetchAll(PDO::FETCH_ASSOC). This means you will no longer have those unnecessary 0 and 1 indexes in your returned array.

Then the simplest solution is simply to loop over the returned array.

$STH = $this->database->prepare('SELECT * FROM project_categorys');
$STH->execute();
$results = $STH->fetchAll(PDO::FETCH_ASSOC);
$ret = array();
foreach ($results as $row) {
    $ret[$row['id']] = $row['name'];
}
return $ret;

Perhaps even better, we can remove the fetchAll call, since PDOStatement is Traversable:

$STH = $this->database->prepare('SELECT * FROM project_categorys');
$STH->execute();

$ret = array();
foreach ($STH as $row) {
    $ret[$row['id']] = $row['name'];
}
return $ret;

Upvotes: 1

AlexP
AlexP

Reputation: 9857

public function getCategories()
{
  if (null == $this->categories) {
    $stmt = $this->database->preapre('SELECT id, name FROM project_categorys')->execute();
    $categories = $stmt->fetchAll();

    foreach($categories as $category) {
      $this->categories[$category['id']] = $category;
    }
  }
  return $this->categories;
}

Upvotes: 1

user399666
user399666

Reputation: 19909

Try this:

function getCategorysArray(){
    $STH = $this->database->prepare('SELECT * FROM project_categorys');
    $STH->execute();
    $res = $STH->fetchAll(PDO::FETCH_ASSOC);
    $categories = array();
    foreach($res as $row){
        $categories[$row['id']] = $row;
    }
    return $categories;
}

Upvotes: 2

Related Questions