May 4, 2012

access associative array

Question by user1017268

I am using Code Igniter and I get following data structure after executing a query at DB

array
'application' => 
    array
    0 => 
        object(stdClass)[19]
        public 'app_id' => string '16' (length=2)
        public 'app_name' => string 'dddddddd' (length=8)
        public 'app_title' => string 'sdfsdf' (length=6)
        public 'app_comments' => string 'sdfsdf' (length=6)
        public 'active_flg' => string 'N' (length=1)

I know one way to access the values is

foreach($application as $key => $value)
    $value->app_id

But I know that I will get only one record each time so I want to access the elements without using foreach.
I have tried to $application->app_id and $application[‘app_id’] but I keep getting error.

Can anybody please help me to understand how to access the data directly??

Answer by Starx

You are using multidimensional mixed type of array, with numeric indexing on the second level. SO, while accessing the values, you have to use them too. Like

echo $array['application'][0]->app_id;

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!