0

I have an array called $reads when I do an var_dump($reads), I get the below array result. I am trying to get first item of first array with var_dump($reads[0][0]). I get a Message: Error rendering view: [home.uploaded] Undefined offset: 0

array(161) {
  [0]=>
  array(4) {
    ["517a5745e8505"]=>
    string(29) "Ngee Ann Poly_Keywords report"
    ["517a5745e86fe"]=>
    string(0) ""
    ["517a5745e882e"]=>
    string(0) ""
    ["517a5745e89b5"]=>
    string(0) ""
  }
  [1]=>
  array(4) {
    ["517a5745e8505"]=>
    string(7) "Keyword"
    ["517a5745e86fe"]=>
    string(6) "Clicks"
    ["517a5745e882e"]=>
    string(11) "Impressions"
    ["517a5745e89b5"]=>
    string(3) "CTR"
  }
  [2]=>
  array(4) {
    ["517a5745e8505"]=>
    string(18) "accounting diploma"
    ["517a5745e86fe"]=>
    string(1) "2"
    ["517a5745e882e"]=>
    string(3) "364"
    ["517a5745e89b5"]=>
    string(5) "0.55%"
  }
  [3]=>
  array(4) {
    ["517a5745e8505"]=>
    string(11) "polytechnic"
    ["517a5745e86fe"]=>
    string(4) "1940"
    ["517a5745e882e"]=>
    string(5) "42995"
    ["517a5745e89b5"]=>
    string(5) "4.51%"
  }
  [4]=>
  array(4) {
    ["517a5745e8505"]=>
    string(15) "tourism diploma"
    ["517a5745e86fe"]=>
    string(1) "1"
    ["517a5745e882e"]=>
    string(3) "156"
    ["517a5745e89b5"]=>
    string(5) "0.64%"
  }
2
  • what does var_dump($reads[0]["517a5745e8505"]); give? Commented Apr 26, 2013 at 10:40
  • var_dump($reads[0]["517a5745e8505"] Commented Apr 26, 2013 at 10:42

5 Answers 5

1

Try this

var_dump($reads[0]["517a5745e8505"]);

for what you want as per comments do this, put you array in a $arr variable and follow what I am doing.

$firstelementvalues = array();
    $i = 0;
    foreach ($arr as $key=>$val) {

        $x = 0;
        foreach ($val as $value) {
            if ($x == 0) {
                $firstelementvalues[] = $value;
                $x = 1;
            }

        }

        $i++;
    }

print_r($firstelementvalues);

Output is

    Array
(
    [0] => Ngee Ann Poly_Keywords report
    [1] => Keyword
    [2] => accounting diploma
)
Sign up to request clarification or add additional context in comments.

3 Comments

Otherwise foreach is the option with key, in his case he just wanted to know the value of first element of array, that is what I understood hence replied accordingly
Actually I am trying get all the first item of each array
Check my updated answer you do what I have done and you will get final result of first element of each array into a final array. Just put your array into a variable $arr and then copy paste my code it should work.
0

Because there is no value in array with 0 offset so please try like

var_dump($reads[161][0]);

2 Comments

neither is 0 in second dimension ;)
@nicogawenda I missed that in this answer i was focused on 161! :-)
0

I think that you need to use loop (for,foreach) if you would like to display AND use the data. var_dump is: Arrays and objects are explored recursively with values indented to show structure.

Comments

0

It's because your array doesn't have element [0][0].

If you want to select first element in second dimension array you can use current:

$lev1 = current($yourArray);
$lev2 = current($lev1);

Comments

0

Your reads array has no numeric keys in the second dimension. You could do something like this, if you have no clue about the keys:

$read = $reads[0];
// I am getting all keys now, because I guess you also want to process the rest of that data
$readKeys = array_keys($read);
var_dump($read[ $readKeys[0] ] );

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.