A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearMake array value variable (PHP) - page 1
User InfoPosts
Make array value variable (PHP)#1
Say I want to echo an array but I want to make the value in the array I echo variable, how would I do this?

Below is kind of an explanation of what I won(t to do but it isn(t the correct syntax.

$number = 0;
echo myArray[$number];

posted date: 2009-04-09 15:08:00


Re: Make array value variable (PHP)#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-09 15:08:01


Re: Make array value variable (PHP)#3
You are nearly there:$number = 0;$myArray = array((a(, (b()echo $myArray[$number]; // outputs (a(

posted date: 2009-04-09 15:11:00


Re: Make array value variable (PHP)#4
Oh how foolish of me, thank-you.

posted date: 2009-04-09 15:16:00


Re: Make array value variable (PHP)#5
$myArray = array("one","two","three","four");$arrSize=sizeof($myArray);for ($number = 0; $number < $arrSize; $number++) {echo "$myArray[$number] ";}// Output: one two three four

posted date: 2009-04-09 15:18:00


Re: Make array value variable (PHP)#6
Just to add more. Another type of array is associative array, where the element is determined using some identifier, usually string.$arrayStates = array((NY( => (New York(, (CA( => (California();To display the values, you can use:echo $arrayStates[(NY(]; //prints New Yorkor, you can also use its numeric indexecho $arrayStates[1]; //prints CaliforniaTo iterate all values of an array, use foreach or for.foreach($arrayStates as $state) { echo $state;}Remember, if foreach is used on non-array, it will produce warning. So you may want to do:if(is_array($arrayStates)) { foreach($arrayStates as $state) { echo $state; }}Hope that helps!

posted date: 2009-04-09 15:22:00


Re: Make array value variable (PHP)#7
This is another piece of code that if they tried to actually run it, would throw an error, which might at least have put them onto the right track to fix it.

posted date: 2009-04-09 15:30:00


Re: Make array value variable (PHP)#8
No, you can not get the second value from an array with a numeric number. You have to use the key, though type conversion applies so '1' is equal to 1 as a key.

posted date: 2009-04-09 19:49:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.