doubi8383 2015-11-07 12:00
浏览 50
已采纳

PHP - 关联数组中的回声值

I feel like I am losing my mind.

<?php
print_r($roc_option);
?>

prints out this:

Array ( ['class'] => classnew1 ['id'] => idnew1 )

but then, on the next line,

<?php
echo $roc_option['class'];
?>

prints out NOTHING.

Any ideas what is going on?

Context: This is happening inside a foreach loop. A similar construction outside the loop, echo $roc_options[0]['class']; provide similarly nothing.

EDIT

Complete context (lots of debugging crud added to keep me sane)

Code

echo "ln1 -- <br>";
   print_r(array_values( $roc_options ));

   echo "<br><br>ln2 -- <br>";
   print_r(array_values( $roc_options[0] ));

   echo "<br><br>ln3a -- <br>";
   echo $roc_options[0]['class']; 

   echo "<br><br>ln3b -- <br>";
   echo $roc_options[0][0]; 


   foreach ( $roc_options as $roc_option ){ 
    echo "<br>inside foreach <br>";

    echo "<br><br>ln4 -- <br>";
    print_r($roc_option);

    echo "<br><br>ln5 -- <br>";
    echo $roc_option[0];

output

ln1 -- Array ( [0] => Array ( ['class'] => classnew1 ['id'] => idnew1 ) )

ln2 -- Array ( [0] => classnew1 [1] => idnew1 )

ln3a --

ln3b --

inside foreach

ln4 -- Array ( ['class'] => classnew1 ['id'] => idnew1 )

ln5 --

I cannot understand why 3a, 3b, and 5 are all empty.


IT GOES ON AND ON

I switched to var_dumps and forced some new names in, just to make sure the form was saving properly...

CODE:

   var_dump($roc_options);
   echo "ln1 -- <br>";
   print_r(array_values( $roc_options ));

   echo "<br><br>ln2 -- <br>";
   print_r(array_values( $roc_options[0] ));

   echo "<br><br>ln3a -- <br>";
   var_dump( $roc_options[0]['class'] ); 

   echo "<br><br>ln3b -- <br>";
   var_dump( $roc_options[0][0]); 


   foreach ( $roc_options as $roc_option ){ 
    echo "<br>inside foreach <br>";

    echo "<br><br>ln4 -- <br>";
    print_r($roc_option);

    echo "<br><br>ln5 -- <br>";
    var_dump( $roc_option[0] );

    echo "<br><br>ln6 -- <br>";
    var_dump( $roc_option['class'] );

    echo "<br><br>ln7 -- <br>";
    var_dump( $roc_option["id"] );

output

array(1) { [0]=> array(2) { ["'class'"]=> string(9) "new class" ["'id'"]=> string(6) "new id" } } ln1 -- Array ( [0] => Array ( ['class'] => new class ['id'] => new id ) )

ln2 -- Array ( [0] => new class [1] => new id )

ln3a -- NULL

ln3b -- NULL inside foreach

ln4 -- Array ( ['class'] => new class ['id'] => new id )

ln5 -- NULL

ln6 -- NULL

ln6 -- NULL

Notice ln4 --- $roc_option is an array with two items. then look at 5-7 I can't get the value out with a numerical index or with a key name, whether in single or double quotes. (When I try without quotes at all, the page doesn't load.)

  • 写回答

1条回答 默认 最新

  • dongtu1357 2015-11-07 12:31
    关注

    EDIT:
    See difference between $array['class'] and $array["'class'"]

    foreach ( $options as $optionArray ){ 
        var_dump($optionArray['class']); // notice undefined index
        var_dump($optionArray["'class'"]); // string(9) "classnew1"
    }
    

    You need to make the difference between the output of array_values and what print_r gives out.

    Assuming the following structure, based on your output, see the following logic:

    $options = array(
        array(
            'class' => 'classnew1',
            'id'    => 'idnew1'
        )
    );
    
    print_r($options);
    
    print_r(array_values($options));
    
    print_r(array_values( $options[0] ));
    
    foreach ( $options[0] as $option ){ 
        print $option . PHP_EOL;
    }
    

    that would print out your inner values.

    If you want to extend the loop and not rely on the first index, try this

    foreach ( $options as $optionArray ){ 
        print $optionArray['class'] . '-' . $optionArray['id']. PHP_EOL;
    }
    

    Or if you'd like to print out a single value, you could do this

    print $options[0]['id'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题