oriental_express Posted December 20, 2008 Share Posted December 20, 2008 Hi there am abit stuck How would is how would I assign a variable based on the element in an array ? as of now i have $a = $apples[$a][3] // but does not work So what ever number is in pocket 3 will be assigned to $a. So the number in pocker 3 might be 7 so it should assign 7 to $a. Without tell me the aswer i would like some hints please Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/137776-php-how-to-assign-a-variable-based-on-the-element-number-in-an-array/ Share on other sites More sharing options...
xangelo Posted December 20, 2008 Share Posted December 20, 2008 Maybe I'm a bit confused, but you're asking how to assign an array to a variable? You've got it right, you can just get rid of the [$a] part so that it reads: $a = $apples[3]; Quote Link to comment https://forums.phpfreaks.com/topic/137776-php-how-to-assign-a-variable-based-on-the-element-number-in-an-array/#findComment-720107 Share on other sites More sharing options...
.josh Posted December 20, 2008 Share Posted December 20, 2008 print_r($apples); post what it prints Quote Link to comment https://forums.phpfreaks.com/topic/137776-php-how-to-assign-a-variable-based-on-the-element-number-in-an-array/#findComment-720108 Share on other sites More sharing options...
redarrow Posted December 20, 2008 Share Posted December 20, 2008 Little help for you... <?php // array $my_array=array("one","two","three","four"); echo"<pre>"; PRINT_R($my_array); echo"</pre>"; /*result <pre>Array ( [0] => one [1] => two [2] => three [3] => four ) </pre> As you can see array start from a 0. unless you tell it ("1"=> "one" ect ..... */ echo" This is array 0 but value is: ".$my_array[0]." <br><br>"; $array_one=$my_array[1]; echo" This is array 1 but value is: $array_one and was assign via a varable of \$array_one"; echo " <br><br>"; //multidesemol array $my_array=array("name"=>"redarrow","programming"=>"php"); echo"<pre>"; PRINT_R($my_array); echo"</pre>"; /* <pre> ( [name] => redarrow [programming] => php ) </pre> */ echo" hi ther i am ".$my_array['name']." and i love ".$my_array['programming'].""; // as you can see now $my_array array name and name is the key of the array //and redarrow is the value echo"<br><br>"; $my_name=$my_array['name']; $like=$my_array['programming']; echo" hi ther i am $my_name and i love $like"; // As you can see, we assign the varable \$my_name then the array \$my_array['name'] then the //key that points to the value of redarrow. ?> Quote Link to comment https://forums.phpfreaks.com/topic/137776-php-how-to-assign-a-variable-based-on-the-element-number-in-an-array/#findComment-720121 Share on other sites More sharing options...
trq Posted December 20, 2008 Share Posted December 20, 2008 Your question (if there is one) makes little to no sense. Exactly what is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/137776-php-how-to-assign-a-variable-based-on-the-element-number-in-an-array/#findComment-720123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.