June 26, 2011

how do i access these array keys as a variable in CI?

Question by ktm

Array
(
    [abc] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [title] => hello 12
                    [meta_keyword] => 
                    [meta_description] => 
                    [tags] => sdfgdfg
                    [status] => draft
                    [body] => dsfdsf dfdsafsdfsdfsdf
                    [photo] => images/blog/nari.jpg
                    [raw] => nari
                    [ext] => .jpg
                    [views] => 0
                     => 
                    [categoryid] => 5
                    [subcatid] => 7
                    [featured] => 
                    [pubdate] => 2011-06-17 03:39:55
                    [user_id] => 0
                )

            [1] => Array
                (
                    [id] => 2
                    [title] => hello xyz
                    [meta_keyword] => 
                    [meta_description] => 
                    [tags] => xcfasdfcasd
                    [status] => draft
                    [body] => dfdsafsdf dsfdsf dfdsafsdfsdfsdf
                    [photo] => images/blog/nari.jpg
                    [raw] => nari
                    [ext] => .jpg
                    [views] => 0
                     => 
                    [categoryid] => 1
                    [subcatid] => 2
                    [featured] => 
                    [pubdate] => 2011-06-17 03:43:12
                    [user_id] => 0
                )

for example if i want to echo out title I would do echo $abc['title'] but it’s not working pls help,

the above output is a result of print_r($count['abc]);
it shows nothing when i do print_r($count['abc']['title'])

Answer by Jared Farrish

You would need to use the numeric key as well: $abc[0]['title'].

In other words, you’ve got an array with array members of an array type which use numeric keys, in which each of those members are arrays which use associative keys to access values. So you need to access each array in $abc to get to the array which contains your title values.

EDIT

If you’re trying to loop through these values, you would need to loop through each array. Such as:

$c_abc = count($abc);

for ($i = 0; $i < $c_abc; $i++) {
    echo "{$abc[$i]['title']}<br/>";
}

Answer by Starx

To access you array variables, the right way is like this

$count['abc'][0]['title']

However, in your title, you are asking about Array keys as variables?

Actually this does not need to be related with CI.

A simple example

$array = array ( "hi" => "bye");
extract( $array);
//it will make "hi" a variable :: $hi = "bye"

echo $hi; // will output bye

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!