Create new arrays from a parent based on key=>val?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darthead
Forum Newbie
Posts: 6
Joined: Tue Apr 07, 2009 5:15 pm

Create new arrays from a parent based on key=>val?

Post by darthead »

I have an array of records with a key named 'news_type_id', which can be an intval from 1-4.

Code: Select all

 
$records = array(
 array('news_type_id'=>2,'content'=>'Some content'),
 array('news_type_id'=>1,'content'=>'Some content'),
 array('news_type_id'=>3,'content'=>'Some content'),
 array('news_type_id'=>3,'content'=>'Some content'),
 array('news_type_id'=>1,'content'=>'Some content'),
 array('news_type_id'=>4,'content'=>'Some content'),
 array('news_type_id'=>2,'content'=>'Some content')
);
 
Each int is an id for a lookup table that contains the news_type_id and the news type:

Code: Select all

 
[u]"news_type_id"   "type"[/u]
"1"             "Press Releases"
"2"             "Company News"
"3"             "Partner News"
"4"             "Published Articles"
 
What I'd like to do is create a new array for each news_type_id found in the original record array so that I can display the records in the view segregated by news_type:

Code: Select all

 
$records['Press Releases'] = array(
  array('news_type_id'=>1,'content'=>'Some content'),
  array('news_type_id'=>1,'content'=>'Some content')
);
$records['Company News'] = array(
  array('news_type_id'=>2,'content'=>'Some content'),
  array('news_type_id'=>2,'content'=>'Some content')
);
$records['Partner News'] = array(
  array('news_type_id'=>3,'content'=>'Some content'),
  array('news_type_id'=>3,'content'=>'Some content')
);
$records['Published Articles'] = array(
  array('news_type_id'=>4,'content'=>'Some content')
);
 
Is there an efficient way to do this? Right now, I'm querying the db, which produces the first array above:

Code: Select all

 
SELECT *
FROM (`news`)
 
Then I'm doing a switch on the result array:

Code: Select all

 
foreach ($records as $r){
    switch($r['news_type_id']){
        case 1:
            $types['Press Releases'] []= $r;
            break;
        case 2:
            $types['Focus News'] []= $r;
            break;
        case 3:
            $types['Partner News'] []= $r;
            break;
        case 4:
            $types['Published Articles'] []= $r;
            break;
     }
}
 
Seems rather clunky, although it works. But what I'd like to do is rewrite the code so I can use any number of news_type_ids and have it automatically create the separate arrays, ideally by querying the lookup table (right now, I'm manually coding the switch statement based on the news_types, which I know in advance). If the client wants to add different types I'd like to not have to go back into the code to reflect them.

Thanks for your input.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Create new arrays from a parent based on key=>val?

Post by AbraCadaver »

Not tested, and I don't know your proper table/column names, but something like this should work:

Code: Select all

$result = mysql_query('SELECT * from `news`, `news_types`
                        WHERE `news`.`news_type_id` =  `news_types`.`news_type_id`');
 
while($row = mysql_fetch_assoc($result)) {
    $records[$row['type']][] = array('news_type_id'=>$row['news_type_id'],'content'=>$row['content']);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
darthead
Forum Newbie
Posts: 6
Joined: Tue Apr 07, 2009 5:15 pm

Re: Create new arrays from a parent based on key=>val?

Post by darthead »

Thanks very much! That was exactly what I looking for... I went one better after I remembered my JOIN clauses. This is pulling from all my relevant tables and does exactly what I was hoping. Much prettier on the eyes and the CPU.

Code: Select all

 
SELECT 
 n.id, n.title, n.news, 
 n.date, n.hidden, nt.type,
 m.media_type, m.filename, m.caption,
 m.ORDER, p.pub_name, p.pub_url
FROM ci_news AS n
LEFT JOIN ci_news_types AS nt
USING (news_type_id)
LEFT JOIN ci_media AS m
USING (id)
LEFT JOIN ci_publications AS p
USING (id)
ORDER BY n.date ASC
 
Post Reply