Arsii Rasheed
Arsii Rasheed

Reputation: 351

Can't access the Value in PHP array via index

I am trying to access the element of associative array but its giving the error of undefined index . I have made sure that my index is correct and the code is written fine but it's adding an alphabet 'A' at end of index 'Genre' by itself. i don't know where it is coming from . Below is the code,the error and the dump :

if($Row){
    $Data = unserialize($Row->Data);
    $Last = array();
    unset($Path[0]);
    foreach($Path as $Key=>$name){
        echo $name;
        $Last = $Data[$name];
    }
    return $Last;
}

Error that i got

ErrorException (E_ERROR) Undefined index: GenreA (View: C:\xampp\htdocs......)

dd($Data,$Path);

Gives :

array:7 [▼
    "Regions" => array:10 [▶]
    "Country" => array:241 [▶]
    "Languages" => array:106 [▶]
    "Category" => array:2 [▶]
    "SubCategory" => array:2 [▶]
    "Genre" => array:2 [▶]
    "SubGenre" => array:2 [▶]
] 

array:1 [▼
    1 => "Genre"
]

enter image description here

Debug image enter image description here

Upvotes: 0

Views: 229

Answers (1)

krishna ganisetti
krishna ganisetti

Reputation: 54

I think don't need for each loop just write like this I hope it will work

end($Data);  
$last = key($Data);  
$last_element = $Data[$last];

Upvotes: 2

Related Questions