1

I think this can be a simple problem for some of you. I've discovered similar questions, but they don't exactly solve my problem.

I've two arrays.

$array_numerals

Array
(
[2] => two
[3] => three
[4] => four
[5] => five
[6] => six
[7] => seven
)

$array_two

Array
(
[0] => $100
[1] => $200
[2] => $300
)

If I use

echo $array_two[0];

it shows the output properly, which is $100, as expected.

what I want to do is to make the "two" and the "[0]" dynamic. the "two" can be replced with "three" or "four" while the "[0]" can be replaced with "[1]" or "[2]".

So if I use

$i = 2;
$j = 0;

echo $array_{$array_numerals[$i]}[$j];

it doesn't work and shows empty value.

Edit: Thank you all for the comments and answers. I must add that $array_two, $array_three, etc. are fixed variables given by another source, so it's not like I can control the initial construction of these arrays. Hence, the answers given by some of you won't work out of the box (not your fault of course, perhaps I didn't clarify enough in the beginning). The answers given by Amadan and LuvnJesus work the best.

4
  • can you please add expected out for your question? Commented Jan 28, 2016 at 5:58
  • 1
    What you're trying to do there is impossible the way you are doing it. Instead, you need to use a jagged array, which is an array of arrays. Commented Jan 28, 2016 at 6:01
  • @amit-shah the expected output for the second code block should be the same as the first code block, which is "$100". Commented Jan 28, 2016 at 6:02
  • You can do this using $$var in PHP. Lets you store a variable or array name in a variable. I've used it rarely but it does work. Example shown in answer post. Commented Jan 28, 2016 at 6:15

5 Answers 5

4
echo ${"array_$array_numerals[$i]"}[$j];

But I must warn you that it is a very bad idea to do this. Rather, use another lookup array:

$array = Array(
  0 => Array(
    2 => "$100",
    ...
  ),
  ...
);

echo $array[$i][$j];
Sign up to request clarification or add additional context in comments.

Comments

0

Why not just use:

$array['two'][0]
$array['three'][0]
$array['four'][0]
$array['four'][1]
etc...

Comments

0

Can you just use it like this:

$array_two[0] = $array_two[1];

And do the same thing with the other array.

Comments

0

First, create the "name" of the array and store it in a variable:

$arrayname = "array_".$array_numerals[$i];

Store the array values into a new array

$newarray = $$arrayname;

Now output whichever elements you wish:

echo $newarray[$j];

Altogether you have:

$arrayname = "array_".$array_numerals[$i];
$newarray = $$arrayname;
echo $newarray[$j];

Comments

0

You can use PHP's compact function. I have used your array and created result as you need. please check here

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.