0

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.)

15
  • Can you post your foreach loop code? Commented Nov 7, 2015 at 12:01
  • 1
    We need more code to guess where that could come from Commented Nov 7, 2015 at 12:05
  • Also your print_r() outputs are missing things like commas and quotes etc and therefore make no sense. Do not edit the output, put it in your question EXACTLY as it appears without any sensorship Commented Nov 7, 2015 at 12:21
  • That is EXACTLY how it is outputting. (It may matter that this is inside a WordPress admin page, but I don't see how it should matter. --- I am aware that the output makes no sense. That's why I'm so confused. Commented Nov 7, 2015 at 12:22
  • 1
    Could you do a var_dump($roc_options); before ln1 and edit the results into the answer? Should prove enlightening. Commented Nov 7, 2015 at 12:49

1 Answer 1

1

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'];
Sign up to request clarification or add additional context in comments.

4 Comments

I know how it should work. Look at the most recent update, though. I just cannot access the values in $roc_option for some reason.
you have single quotes in your array indexes, I will edit my answer to illustrate. Should have seen it sooner, apologies :)
oh... wow - those should definitely not be there. I need to go look at my form inputs.
yeah, they are clearly shown in the output of print_r but .. missed them somehow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.