Reputation: 1788
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.)
Upvotes: 0
Views: 8002
Reputation: 7283
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'];
Upvotes: 1