Jump to content

$variable = $variable


echo64

Recommended Posts

Hello

 

Sorry if this is a really simple question but I have run out of ideas. Is there a really simple way of doing the following:

 

$animal_dog    = $something_something[$i]['animal_dog'];

$animal_cat  = $something_something[$i]['animal_cat'];

$animal_cow    = $something_something[$i]['animal_cow'];

etc, etc, etc

 

Is there a way to simplify the process above so that I could just do:

 

$animal_*    = $something_something[$i]['animal_*'];

 

I hope this makes sense and thanks in advance... this would be so handy to know how to do!

 

Thanks  :)

Link to comment
Share on other sites

Close. You're just a bit backwards.

<?php

$dog='arf';

$cat = 'meow';

$cow = 'moo';

$animal_sounds['dog']=$dog;

$animal_sounds['cat']=$cat;

$animal_sounds['cow']=$cow;

 

echo $animal_sounds['dog']."<br>";//arf

echo $animal_sounds['cat']."<br>";//meow

echo $animal_sounds['cow']."<p>";//moo

 

foreach($animal_sounds as $key => $value)

{

echo "A $key says $value<br>";

}

?>

 

HTH

Tteamatomic

 

oops....edited for a typo... $animal_sound = $animal_sounds

Link to comment
Share on other sites

Hey

 

Thanks for the replies but that section of code (although nothing to do with animals I might add!) is from a much larger script and it needs to stay in the current format. My main issue is having to manually replicate the variables, which is why I was wondering if it's possible to use some kind of wild-card feature within PHP.

 

If something like: "$animal_*    = $something_something[$i]['animal_*'];" is possible it would save me hours of work. Perhaps this is not even possible?  :confused:

 

So just to reiterate - there could be hundreds of animals and I would have to write hundreds of lines to do what I want, but if there was something like my suggestion that actually worked I could do it all in a few lines. Thoughts?  :shy:

Link to comment
Share on other sites

A short answer to your Q about wild cards. No. You will still have to build the array. As I showed you how to build the array extract would give you:

echo "$dog //arf";

echo "$cat"; //meow

echo "$cow";//moo

 

The easiest way would be:

$array = (

'dog'  => 'arf',

'cat' => 'meow',

'cow' => 'moo'

);

 

put this in a file and include it, then you can:

extract($array);

or

foreach($array as $key => $value)

or

$dog = $array['dog'];

or

anything else you can do with arrays

 

 

HTH

Teamatomic

 

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.