0

How can I retrieve all of the values from variable arrays within a variable array?

EDIT: I most likely don't know how to explain it well, but I want to know how I can display the values in $oranges and $melon within $variables. I thought it would be as simple as setting variables and placing them into an array but it returns "Array".

Example:

<?php 
$oranges = array("0","1");
$melon = array("2","3");


  $variables = array($oranges,$melon);
  $i = 1;


 foreach ($variables as $var) {
 $name = "txt".$i;
 echo "<input type='text' name='".$name."' value='".$var."' />";
 $i++;
 echo "<input type='file' name='image'>";

 } 


 ?>
8
  • what ?? sometimes i am asking myself : am i tired or questions are "non qualified questions" ?? ;) Please specify more what you are trying to do and the result expected Commented Dec 3, 2013 at 1:41
  • What are you expecting as result? It will help us to understand... Commented Dec 3, 2013 at 1:44
  • 1
    Please, explain, what output do you want to get. What is $name? Commented Dec 3, 2013 at 1:44
  • 1
    I like that you edited your comment to a more courteous one lol Commented Dec 3, 2013 at 1:44
  • Please see my EDIT. That should clear things up. Commented Dec 3, 2013 at 1:45

2 Answers 2

1

Retrieving values of an array within another equals to retrieve a value from one single array. You just have to know what you want to do.

Suppose we have array $A and array $A1 and $A2 inside $A. foreach ($A as $innerArray) ... $innerArray the first inner array for the first loop, then the second etc ... If you want to get $innerArray values, make another loop, or get them directly if you already know the keys.

Exemple : here are your vars:

$oranges = array("0","1");
$melon = array("2","3");

$variables = array($oranges,$melon);

Retrieve value of single array

echo $oranges[0]; // prints 0
echo $melon[1]; // prints 3

Retrieve values from array within another

foreach ($variables as $var) {
 $name = "txt".$i;
 echo "<input type='text' name='".$name."' value='".$var["INDEX"]."' />"; // INDEX must be an existing key in $oranges or $melon
 $i++;
 echo "<input type='file' name='image'>";

 }
Sign up to request clarification or add additional context in comments.

1 Comment

Your first sentence made it click for me. I added two input elements and gave them the following values: $var[0] & $var[1]. You rock bro.
1

According to your code

$oranges = array("zero","one");
$melon = array("two","three");
$variables = array($oranges,$melon);
$i = 1;


 foreach ($variables as $var) {

     // $var[0] will contain "zero"
     // $var[1] will contain "one"

     //When $i increments to 2 on the next loop
     // $var[0] will contain "two"
     // $var[1] will contain "three"
 }

If you want to access these values without a loop, it is as simple as

$variables[0][0] --->  "zero"
$variables[0][1] --->  "one"
$variables[1][0] --->  "two"
$variables[1][1] --->  "three"

1 Comment

This is very good stuff. Kinda confusing for me at first, but it all makes sense now. Thank you :)

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.