Okay, here\'s what I\'m trying to do. I am running a MySQL query for the most recent posts. For each of the returned rows, I need to push the ID of the row to an array, then wit
$master[$id]['post_title'] = $post_title;
$master[$id]['post_text'] = $post_text;
// etc
or, less code. With this one, you can get rid of where you set all those variables:
$master[$row["id"]]['post_title'] = $row["title"];
$master[$row["id"]]['post_text'] = $row["text"];
// etc
Edit in answer to comment:
foreach( $master as $row )
{
echo $row['post_title'];
}
// or if you know the id
echo $row[$id]['post_title'];
here you have the complete reference for arrays, anyway a common way to do this is to use $myarray[] = $aa;
to "push" into it.
<?php
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 10";
$result = mysql_query($query);
$posts = array();
while($row = mysql_fetch_array($result)){
$posts[] = $row;
// second option, index the post by their id
// $posts[$row["id"]] = $row;
// third option
/*
$tmp = array();
$tmp["title"] = $row["title"];
$tmp["desc"] = $row["desc"];
$posts[$row["id"]] = $tmp;
*/
}
?>
I tend to like:
$posts = array();
while ($row = mysql_fetch_array($result)) {
$posts[] = array(
'id' => $row['id'],
'title' => $row['title'],
'text' => $row['text']
);
}