Forum Moderators: coopster
I'm trying to write a simple RSS parser with Magpie and my dilemma is this:
I need to integrate news items from multiple feeds, sort them by date, filter for duplicates, and then publish. I've gotten without much trouble to the point where it grabs the feeds and outputs them in the order that it grabs them. I then took the step of instead of just echoing a list of strings in the loop, I made an array called $postarray as follows:
$postarray = array($title, $channelTitle, $guid, $chronology, $pubdate, $creator, $url);
I tested that and it worked.
I then took the step of giving it a dynamic name constructed from the guid (unique post identifier) of the post:
$guidStripped = str_replace (' ','', $guid);
${$guidStripped} = array($title, $channelTitle, $guid, $chronology, $pubdate, $creator, $url); //this replaces the $postarray = ... line.
I cant test this because I don't know how to call an array once I've given it a dynamic name! It's probably something really dumb, but I can't figure it out or find documentation anywhere. How do I print out my array as before, now that it's dynamically named?
Thanks in advance!
[edited by: MinnaVonBarnhelm at 6:15 pm (utc) on Jan. 7, 2008]
Have a look at variable variables [php.net], it should tell you all you need to know.
Maybe it's easier to do something like this:
$myArray = array();
$guidStripped = str_replace (' ','', $guid);
$myArray[$guidStripped] = array($title, $channelTitle, $guid, $chronology, $pubdate, $creator, $url);
That way you'll put all your stuff into one array which can easily be addressed, because you yourself have given it its name. With ${$guidStripped} there is no way to call the array once $guidStripped changes.
However the one line template engine [webmasterworld.com] from the library is one good use of variable variables.
I recently used some variable variables, though not as an array, so i whipped up a test...
<?
echo "<pre>\n";
$en_array=Array( "one", "two" );
$es_array=Array( "uno", "dos" );
$types=Array( "en", "es" );
for( $i=0; $i<count($types); $i++ ) {
foreach( $types as $variable ) {
?> echo ${$variable."_array"}[$i]
${<?=$variable?>."_array"}[<?=$i?>]
$<?=$variable."_array"?>[<?=$i?>]
=<? echo ${$variable."_array"}[$i]."\n\n";
}
}
?></pre>
Output:
echo ${$variable."_array"}[$i]
${en."_array"}[0]
$en_array[0]
=one
echo ${$variable."_array"}[$i]
${es."_array"}[0]
$es_array[0]
=uno
echo ${$variable."_array"}[$i]
${en."_array"}[1]
$en_array[1]
=two
echo ${$variable."_array"}[$i]
${es."_array"}[1]
$es_array[1]
=dos
They're real handy when you do forms with like billing and shipping type. I've used a lot of foreach( $bORs as $bs) type arrays, strings, and $_POST variables.
:) Chris
$myArray[$guid] = array('title'=>$title, 'channelTitle'=>$channelTitle, 'chronology'=>$chronology, 'pubdate'=>$pubdate, 'creator'=>$creator, 'url'=>$url, 'body'=>$body);
}
}
// and in here there was some sorting and some styling and so forth
foreach ($myArray as $guid => $tempArray) {
echo $tempArray['chronology'].' '.$tempArray['title'].' '.$tempArray['title'].' '.$tempArray['channelTitle']."<br>".$tempArray['pubdate']." :: ".$tempArray['creator'].' '.$tempArray['body'].;
}
$keys=(
'title',
'channelTitle',
'chronology',
'pubdate',
'creator',
'url',
'body',
);
// // I would do this, but not needed since Array() is used below:
// extract($keys);
// this bit exists only to set up the arrays with sample content:
$i=0;
foreach( $keys as $an_array ){
$key=$an_array;
$$an_array=Array();
$$an_array[$key]="content_".$i++;
}// here's the meat of things like this:
$string="";
$i=0;
foreach( $keys as $variable ){
$string.= $$variable[$variable]." ";
if($i==3 ){
$string=rtrim($string);
$string.="<br>"; // < to < for browser
}elseif($i==4 ){
$string.=" :: ";
}
$i++;
}
echo $string;
After so many years of C / embedded C, learning about associative array and variable variables, I can stop abusing the cut and paste.
:) :)
>> That is why variable variables are generally considered evil
bah, don't believe him, I love them :)
the big trick is that you always need to be able to recreate the names so they need to be created logically
a control array works
or just combining words with numbers so you can iterate through them with a loop
there are other ways too but it's early in the morning so I can't think of them ;)