1

I have an array called $alldata

If I do this

echo $alldata[0][6][0]["COLOUR"];

It successfully returns the colour. I want to access the value without using the name / label "COLOUR"

I tried this, but it fails with undefined offset echo $alldata[0][6][0][0];

1
  • There are 4. I will want to get the first and third Commented Nov 17, 2016 at 20:22

2 Answers 2

1

Re-index so you can use a numeric index:

echo array_values($alldata[0][6][0])[0];

Or for them all:

$result = array_values($alldata[0][6][0]);
echo $result[0];
echo $result[2];
Sign up to request clarification or add additional context in comments.

4 Comments

if $alldata[0][6][0] = array, then it won't echo out the value, it will echo array.
@Hallur: What?!?!?
well, at one point before you edited your post, it said "echo $alldata[0][6][0];" and the output of that would simply be "Array"
@Hallur: No. Look at the edit history: echo current($alldata[0][6][0]); will get the value of the array element that currently has the pointer. echo end($alldata[0][6][0]); will return the value of the last array element.
0

You have to use foreach for this, since the array key is "COLOUR" and not 0.

here's an example on how to solve your problem.

<?php
$alldata = array(
0=>array(
6=>array(
0=>array(
"COLOR"=>"test"
))));

print_r($alldata);


foreach ($alldata[0][6][0] as $key => $value) {
    echo $key . "=>" . $value;
}
?>

if you want to use the third key, then you can add a counter to it, by defining $x outside of the foreach and $x++; in the foreach.

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.