Jump to content

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


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

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.