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;
February 23, 2012

Codeigniter Route to accept dynamic values

Question by mrN

I am trying to create a router which will take a dynamic value and forward it to the actual route. In normal case it would be like

$route['login'] = 'auth/login';

It is possible to catch a parameter before the login in the above parameter and pass it to as the first parameter to the actual route ? like

$route['^(.+)/login$'] = "$1/user/login";

Answer by Starx

Check out the documentation[docs]. There is a very easy way to do this.

$route['(:any)/login'] = '$1/auth/login';
...

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