如何在CI中将这些数组键作为变量访问?

时间:2011-06-26 05:56:18

标签: php codeigniter

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
                )

例如,如果我想回显标题,我会echo $abc['title'],但它不起作用请帮忙,

以上输出是print_r($count['abc]);的结果 当我print_r($count['abc']['title'])

时,它什么都没有显示

4 个答案:

答案 0 :(得分:2)

您还需要使用数字键:$abc[0]['title']

换句话说,你有一个数组类型的数组成员使用数字键的数组,其中每个成员都是使用关联键来访问值的数组。因此,您需要访问$abc中的每个数组才能访问包含title值的数组。

修改

如果您尝试循环遍历这些值,则需要遍历每个数组。如:

$c_abc = count($abc);

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

答案 1 :(得分:0)

了解php关联数组....你将实现目标

试试这个:

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

答案 2 :(得分:0)

要访问数组变量,正确的方法就是这样

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

但是,在标题中,您询问Array keys as variables?

实际上这不需要与CI相关。

一个简单的例子

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

echo $hi; // will output bye

答案 3 :(得分:0)

Heres结构化解决方案

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