Forum Moderators: coopster

Message Too Old, No Replies

How to call a dynamically-named array?

So I gave an array a dynamic name... how do I track it down now?

         

MinnaVonBarnhelm

6:14 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



Hi guys, I'm a n00b, this is my first post.

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]

RonPK

6:28 pm on Jan 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Minna, welcome to WebmasterWorld!

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.

MinnaVonBarnhelm

8:42 pm on Jan 7, 2008 (gmt 0)

10+ Year Member



Ah, so there really is no way to get information back out of an array if you give it a dynamic name? (I mean, without specifying one instance of it by its static name, in this case, by the modified guid?)

PHP_Chimp

8:50 pm on Jan 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is why variable variables are generally considered evil ;)
There are some very cool uses for them...but generally you dont see them around, as they screw with people minds to much (or maybe I just have a very small mind...). As it hard to keep track of the name of your variable when every time you try to call the variable it has a different name.

However the one line template engine [webmasterworld.com] from the library is one good use of variable variables.

cacycleworks

12:51 am on Jan 8, 2008 (gmt 0)

10+ Year Member



Hi,

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

cacycleworks

1:00 am on Jan 8, 2008 (gmt 0)

10+ Year Member



BTW, sorry ... meant to say "Hi" too. There's an ancient thread on here that totally made my MySQL day. I'm just getting going with it and got the quick answer to my simple question and thought I'd post and say hi and thanks. Then I got all wrapped up with the variable named array.

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

MinnaVonBarnhelm

1:07 am on Jan 8, 2008 (gmt 0)

10+ Year Member



Hey guys thanks for your help! Due in part to your tips, I've got my RSS thing up and running now! I'm going to polish it up and release it as a Drupal module when I get time (I'll never understand why there are so many complicated Drupal aggregators, but no simple ones!) but here's a quick peek at what I ended up doing, in case any other n00bs stumble upon this:

$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'].;
}

cacycleworks

3:10 am on Jan 8, 2008 (gmt 0)

10+ Year Member



hehehe, oh cool, you'll like this trick, too. Whenever iterating through a set of names or keys, I put them in an array...

$keys=(
'title',
'channelTitle',
'chronology',
'pubdate',
'creator',
'url',
'body',
);

I stack them like this because it's easy to do block-type cut-n-pasting, or adding, deleting members.


// // 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.="&lt;br&gt;"; // < to &lt; for browser
}elseif($i==4 ){
$string.=" :: ";
}
$i++;
}
echo $string;


which outputs:
content_0 content_4 content_4 content_3<br>content_4 :: content_5 content_6

After so many years of C / embedded C, learning about associative array and variable variables, I can stop abusing the cut and paste.

:) :)

jatar_k

12:57 pm on Jan 8, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld both of you

>> 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 ;)