Jump to content

PHP how to assign a variable, based on the element number in an array ?


oriental_express

Recommended Posts

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

 

 

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.
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.