开发者

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

开发者 https://www.devze.com 2023-03-15 05:47 出处:网络
Array ( [abc] => Array ( [0] => Array ( [id] => 1 [title] => hello 12 [meta_keyword] => [meta_description] =>
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
                    [video] => 
                    [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
                    [video] => 
                    [categoryid] => 1
                    [subcatid] => 2
                    [featured] => 
                    [pubdate] => 2011-06-1开发者_StackOverflow中文版7 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'])


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/>";
}


Read about php associative arrays....you will have you goal achieved

try this:

foreach ($array as $i => $values) {
    print "$i {\n";
    foreach ($values as $key => $value) {
        print "    $key => $value\n";
    }
    print "}\n";
}


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


Heres structured solution

$data = 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
                    [video] => 
                    [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
                    [video] => 
                    [categoryid] => 1
                    [subcatid] => 2
                    [featured] => 
                    [pubdate] => 2011-06-17 03:43:12
                    [user_id] => 0
                )
       )
);

extract($data);

foreach($abc as $value){
    echo $value['title']."<br>";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消