它就像一个魅力我重写它,你可以试试看
$user = (object) array(
'name' => 'xxxx',
'phone' => 'xxxxx',
'itemprices' => Array (1.00, 1.00, 1.00),
'iteminfo' => Array ('Chemistry', 'Biology', 'Mathematics')
);
echo "<pre>";
var_dump($user);
echo "</pre>";
$newvalue = 2.0;
$subject = 'Chemistry';
$index = array_search($subject, $user->iteminfo);
if (false !== $index) {
$user->itemprices[$index] = $newvalue;
}
echo "<br><br><pre>";
var_dump($user);
echo "</pre>";
输出
object(stdClass)#21 (4) {
["name"]=>
string(4) "xxxx"
["phone"]=>
string(5) "xxxxx"
["itemprices"]=>
array(3) {
[0]=>
float(1)
[1]=>
float(1)
[2]=>
float(1)
}
["iteminfo"]=>
array(3) {
[0]=>
string(9) "Chemistry"
[1]=>
string(7) "Biology"
[2]=>
string(11) "Mathematics"
}
}
object(stdClass)#21 (4) {
["name"]=>
string(4) "xxxx"
["phone"]=>
string(5) "xxxxx"
["itemprices"]=>
array(3) {
[0]=>
float(2)
[1]=>
float(1)
[2]=>
float(1)
}
["iteminfo"]=>
array(3) {
[0]=>
string(9) "Chemistry"
[1]=>
string(7) "Biology"
[2]=>
string(11) "Mathematics"
}
}