0

I'm sorry for this beginner question. My problem is that I would like to get the value of the variable name assigned in the array. For instance like this:

$fruits = array($apple, $mango, $banana);

And then I am assigning the elements in the array same values.

for ($i = 0; $i < count($fruits); $i++) { 
    $fruits[$i] = "fruit";
}

When I want to print the apple value in the array I would do:

echo $fruits[0];

But what I want is that to print the value of apple in the array using:

echo $apple;

How would I do this? Sorry beginner here..

11
  • try this print_r(get_defined_vars()); Commented Jul 19, 2015 at 4:42
  • Thank you for answering! Is it possible just to echo the value in the array? Like echo $apple and then getting the value? Commented Jul 19, 2015 at 4:48
  • 1
    What exactly are you trying to do here? You're overwriting the $apple item in your $fruits array with "fruit" so naturally the value of $apple is gone? Are you trying to add items to the array? Commented Jul 19, 2015 at 4:49
  • I think you're a little confused on the purpose of arrays. You can achieve what you're doing by just assigning values directly to the fruit. Commented Jul 19, 2015 at 4:53
  • Why not do an associative array if you want to use "apple": $fruits = array("apple"=>$apple,"mango"=>$mango); echo $fruits['apple']; I assume it's for readability?? Commented Jul 19, 2015 at 4:54

2 Answers 2

1

This is an alternative.

<?php
$fruits = array(
    "apple" => $apple, 
    "mango" => $mango, 
    "banana" => $banana);
foreach ($fruits as $key => $value) { 
    $fruits[$key] = "fruit";
}

echo $fruits['mango'];
Sign up to request clarification or add additional context in comments.

Comments

0

Ok,so what you are doing is incorrect to begin with.

$apple = "apple";
$mango = "mango";
$banana ="banana";

$fruits = array($apple, $mango, $banana);

for ($i = 0; $i < count($fruits); $i++) { 
    $fruits[$i] = "fruit";
}
echo "<pre>";
print_r($fruits);

What you have done in your for loop is to override the values of the array. So when you do the print_r you will get:

Array
(
    [0] => fruit
    [1] => fruit
    [2] => fruit
)

If you want to store whether it is a fruit or vegetable (and i wonder if it is the case, since your array name is 'fruits'), however if you do want to do that, then you should use an associative array:

 $cabbage= 'cabbage';
 $stuffIEat = array($apple => 'fruit', 
                    $mango => 'fruit', 
                    $banana =>'fruit',
                    $cabbage => 'vegetable');

Now when you want to see what kind of stuff apple is, you do:

echo $stuffIEat[$apple]; //prints fruit
echo $stuffIEat[$cabbage]; //prints vegetable

Now if you want to print all the fruits that you eat, you do:

print_r(array_keys($stuffIEat,'fruit'));

This will print

Array
(
    [0] => apple
    [1] => mango
    [2] => banana
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.