获取特定值时,foreach循环获取错误

here is my code below,

<? foreach($goodsview as $key=>$value)
echo $value;
?>

above code works perfectly by displaying all the values in an array, now i need to fetch perticular value using foreach loop itself,

echo $value['myvalue'] ;

but iam getting an error `Illegal string offset 'myvalue' how can we fix this?

The variable $value is not an array but a string. That's the reason why the error message is being displayed.

Without seeing the code there's not much to suggest. But in general, this is how foreach loops work:

Say your array structure is as follows.

Array
(
    [name] => Tom
    [gender] => Male
    [age] => 18
)

To display all the elements, i.e. 'Tom', 'Male', '18', you'd do something like this:

foreach ($array as $key => $value) {
     echo $arr[$key].'<br>';
} 

Output:

Tom
Male
18

To retrieve only a particular value:

For example, if you want to retrieve the person's name, you can do:

echo $arr['name']; //Tom
echo $arr['gender']; //Male
echo $arr['age']; //18

Hope this clears up things for you! :)

$value holds the value in your foreach loop. It's the same as $goodsview[$key]. If you just want a single value then don't use the loop. $goodsview['myvalue'] could hold a value if your Array looks like $goodsview = array('myvalue' => 'This would be the value').

This will not work, because $values does not exist

foreach($goodsview as $key => $value)
   echo $values;

This might work if $value is a string or number, etc, but not if it is a array.

foreach($goodsview as $key => $value)
   echo $value;

The string offset error refers to you accessing a string like an array, but the key you are using does not exist.

Let me explain.

$string = "Jake";
echo $sting[0]; //outputs 'J'
echo $sting[3]; //outputs 'e'
echo $sting['doom']; // will fail


$array = array("Jake", "doom"=>3);
echo $sting[0]; //outputs 'Jake'
echo $sting[3]; //will fail
echo $sting['doom']; // outputs 3

So $value is a string, not a array.

You can access your array like this too $goodsview[$key] which will give you the same as $value.

It may be a shot in the dark but it looks like your maybe working on something like a loop to display options for a select element. Normally you wouldn't waste time with a loop, if you were simply wanting a specific array element.

Does this give you any ideas:

<select name="myvalue">
<?php
foreach($goodsview as $key=>$value) {
  $seltxt='';
  // assume $myvalue is the previously posted value from the select
  // if the $key matches the posted $myvalue - mark this option as selected
  if (!strcmp($key,$myvalue)) $seltxt=' selected';
  echo "<option value=\"$key\"$seltxt>$value</option>
";
}
?>
</select>

Just a thought.

Well Let me Answer my own question, Actually my array is a single dimentional so this method does not work

echo $value['myvalue'] ;

so we have to use

echo $arr[$key]

but it displays multiple elements as if it is running in foreachloop , so without using foreach loop we have to display the result something like

echo $goodsview['myvalue']

hope it will help someone in future if they come across with similar issue...Thank you all for responding to this question,