Jump to content

Dynamic variable array ?!


chrisran

Recommended Posts

Hi !

 

I'd like to count all occurences in "$thiscodestring" into a "dynamic?" array and print it out after the end of the loop, how do I assign the variablenames dynamically ?

 

$query = "SELECT codes, status FROM table";

 

while ($row = mysql_fetch_assoc($result)) {

  $thiscodestring = $row[codes];  // looks like "a1 c2 d3 10 15 1 a1 a1";

  $singlecodes = explode(" ", $thiscodestring);

    foreach($singlecodes as $thiscode) {

      $$thiscode[count] = $$thiscode[count] + 1;

    }

}

 

After all Mysql-Stuff is done I'd like to print out the counted codes, for example the code "a1" was found 100 times in all querys then $$a1[count] should be 100, c2 was found 15 times, then $$c2[count] should be 15 and so on.

 

Question:

How do I tell php to assign a "dynamic" variable with the name of the found string which was exploded (in this example a1, c2, d3... ) before ?!

 

$$ does not seem to work.

 

And how can I then print_r() the value if I dont know the name ? :)

 

Thanks !

Link to comment
Share on other sites

Hi chrisran,

you should just use an array, using $thiscode as the key, and incrementin the value:

$query = "SELECT codes, status FROM table";

$codes = Array();
while ($row = mysql_fetch_assoc($result)) {
  $thiscodestring = $row[codes];  // looks like "a1 c2 d3 10 15 1 a1 a1";
  $singlecodes = explode(" ", $thiscodestring);
    foreach($singlecodes as $thiscode) {
      $codes[$thiscode]++;
    }
  print_r($codes);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.