douzhigan1687
2016-03-23 16:08 阅读 42
已采纳

PHP数组| “注意:使用未定义的常量..”或“注意:未定义的偏移量:0 ..”[关闭]

im trying to loop through my array, without success though..

Array

( 

    [Aircraft] => Array  
        (
            [0] => Array
                (
                    [A] => Gulfstream G100
                    [B] => Cessna Citation Excel
                    [C] => Cessna 208
                    [D] => Piper Aztec
                    [E] => Embraer ERJ 145
                    [F] => Airbus A330
                    [G] => Boeing Business Jet
                )


            [1] => Array

                (
                    [A] => Cessna Citation CJ3 
                    [B] => Dassault Falcon 900
                    [C] => Gulfstream G300
                    [D] => Boeing 767
                    [E] => ATR 42
                    [F] => Gulfstream IV
                    [G] => Airbus A320  
                )
        )
)

i have tried almost everything i can think of so far..

even..

print $data[0][A];

.. wount work.

My plan is to loop (foreach?) through this array..

please help me..

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dpsfay2510 dpsfay2510 2016-03-23 16:14
    foreach($array['Aircraft'] as $array){
         echo $array['A'];
    }
    

    Have you tried something like this?

    点赞 评论 复制链接分享
  • dongzi3805 dongzi3805 2016-03-23 16:13

    We need to see some code where you've tried to iterate through the array. You should be trying something like $data['aircraft'][0][A] to access a single item's value.

    Something like this should work to loop through:

    foreach ($array AS $aircraft) {
        echo $aircraft[A] . PHP_EOL;
    }
    
    // Gulfstream G100
    // Cessna Citation CJ3
    

    Hopefully enough to give you an idea.

    点赞 评论 复制链接分享
  • dryift6733 dryift6733 2016-03-23 16:16

    Very easy way : use var_dump() or print_r() functions to dump variable in php
    But, for a specific needs, you have to think with recursive approach

    function loopRecursive($input) {
        foreach ($input as $key => $value ) {
            if (is_array($value)) {
                loopRecursive($value);
            }
            else {
                printf('Key: %s value: %s<br /> ',$key, $value);
            }                                  
        }
    }
    
    点赞 评论 复制链接分享
  • doushe7934 doushe7934 2016-03-23 16:35

    Try this:

    foreach($array['Aircraft'] as $array){
        echo $array['A']." "; // Will print Gulfstream G100 Cessna Citation CJ3 
    }
    

    Hope this helps.

    Peace! xD

    点赞 评论 复制链接分享

相关推荐