提问人:Armage 提问时间:6/30/2019 最后编辑:Armage 更新时间:6/30/2019 访问量:368
未定义索引,但索引存在
Undefined index but the index exists
问:
我希望有人可以帮助我解决以下错误。我找不到解决方案。
var_dump($curl->response);
echo '<br><br><br><br>';
$result = json_decode(json_encode($curl->response), true);
var_dump($result);
echo '<br><br><br><br>';
$key = 'email';
foreach ($result as $k => $v) {
echo $k . '->' . $result[$k] . '<br>';
if ($k == $key) {
echo $result[$key] . '<br>';
}
}
//$result[$key];
如果我运行,我会得到以下数据(出于安全原因,我更改了一些值)
object(stdClass)#2085 (6) {
["account_type"]=> string(0) ""
["email"]=> string(17) "[email protected]"
["firstName"]=> string(4) "Mary"
["lastName"]=> string(6) "McLonn"
["organizer_key"]=> string(19) "1234567890123456789"
["version"]=> string(1) "3" }
array(6) {
["account_type"]=> string(0) ""
["email"]=> string(17) "[email protected]"
["firstName"]=> string(4) "Mary"
["lastName"]=> string(6) "McLonn"
["organizer_key"]=> string(19) "1234567890123456789"
["version"]=> string(1) "3" }
account_type->
email->[email protected]
[email protected]
firstName->Mary
lastName->McCann
organizer_key->1234567890123456789
version->3
这里一切都还好。但是,如果取消注释最后一行$result[$key],它会给我带来以下错误:
Slim Application Error
The application could not run because of the following error:
Details
Type: ErrorException
Code: 8
Message: Undefined index: email
File: \App\Controllers\Api\V1\binar\Model.php
Line: 313
Trace
#0 \App\Controllers\Api\V1\binar\Model.php(313): Slim\Slim::handleErrors(8, 'Undefined index...', 'App\Controllers\Api...', 313, Array)
#1 \App\Controllers\Api\V1\binar\Model.php(352): App\Controllers\Api\V1\binar\Model::send('https://api.get...', 'POST', Array, Array, false)
#2 \App\Controllers\Api\V1\binar\binar.php(47): App\Controllers\Api\V1\binar\Model->applyCredentials()
#3 [internal function]: App\Controllers\Api\V1\binar\binar::demo()
#4 \vendor\slim\slim\Slim\Route.php(173): call_user_func_array(Array, Array)
#5 [internal function]: Slim\Route->Slim\{closure}()
#6 \vendor\slim\slim\Slim\Route.php(468): call_user_func_array(Object(Closure), Array)
#7 \vendor\slim\slim\Slim\Slim.php(1355): Slim\Route->dispatch()
#8 \vendor\slim\slim\Slim\Middleware\Flash.php(85): Slim\Slim->call()
#9 \vendor\slim\slim\Slim\Middleware\MethodOverride.php(92): Slim\Middleware\Flash->call()
#10 \App\Resources\Middleware\APMiddleware.php(28): Slim\Middleware\MethodOverride->call()
#11 \App\Resources\Middleware\LPMiddleware.php(31): App\Resources\Middleware\APMiddleware->call()
#12 \vendor\slim\slim\Slim\Middleware\PrettyExceptions.php(67): App\Resources\Middleware\LPMiddleware->call()
#13 \vendor\slim\slim\Slim\Slim.php(1300): Slim\Middleware\PrettyExceptions->call()
#14 \index.php(8): Slim\Slim->run()
#15 {main}
同一行在 foreach 中工作良好,这就是为什么它向我显示电子邮件两次的原因。
我正在使用 php-curl-class/php-curl-class 4.8
我真的不明白我做错了什么,或者在哪里寻找解决方案。
感谢您的帮助。
编辑
如果我这样做
$this->email = $curl->response->email
我收到错误:未定义的属性:stdClass::$email
但是如果我这样做
$this->email = (property_exists($curl->response, 'email')) ? $curl->response->email : null;
我没有错误,并且$this->email加载了正确的值
答: 暂无答案
评论
$result[$key];
echo $curl->response->email;
Message: Undefined property: stdClass::$email