max li
max li

Reputation: 2457

PHP Multidimensional Array return error

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%"
  }

Upvotes: 0

Views: 1296

Answers (5)

chandresh_cool
chandresh_cool

Reputation: 11830

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
)

Upvotes: 1

nico gawenda
nico gawenda

Reputation: 3748

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] ] );

Upvotes: 0

Narek
Narek

Reputation: 3823

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);

Upvotes: 0

SuperManSL
SuperManSL

Reputation: 1306

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.

Upvotes: 0

Praveen kalal
Praveen kalal

Reputation: 2129

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

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

Upvotes: 0

Related Questions