未定义索引,但索引存在

时间:2019-06-29 16:27:48

标签: php arrays json undefined-index

我希望有人可以帮助我解决以下错误。我找不到解决方法。

    private void SkglControl1_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintGLSurfaceEventArgs e)
    {
        var paintStroke = new SKPaint
        {
            IsAntialias = true,
            Style = SKPaintStyle.Stroke,
            Color = SKColors.Purple,
            StrokeWidth = 1
        };

        var rand = new Random();
        var sw = new Stopwatch();

        sw.Start();

        e.Surface.Canvas.Clear();

        var width = (int)skglControl1.CanvasSize.Width;
        var height = (int)skglControl1.CanvasSize.Height;

        for (int i = 0; i < 1000; i++)
        {
            var x1 = rand.Next(width);
            var x2 = rand.Next(width);
            var y1 = rand.Next(height);
            var y2 = rand.Next(height);

            paintStroke.Color = new SKColor((byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255));

            e.Surface.Canvas.DrawLine(new SKPoint(x1, y1), new SKPoint(x2, y2), paintStroke);
        }

        sw.Stop();

        Console.WriteLine($"ms: {sw.ElapsedMilliseconds}, w: {width}, h: {height}, pixels: {width * height}");
    }

如果我跑步,我会得到以下数据(出于安全原因更改了一些值)

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];

这里一切都很好。但是,如果取消最后一行$ result [$ key]的注释,则会显示出followinf错误:

object(stdClass)#2085 (6) { 
    ["account_type"]=> string(0) "" 
    ["email"]=> string(17) "sales@example.com" 
    ["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) "sales@example.com" 
    ["firstName"]=> string(4) "Mary" 
    ["lastName"]=> string(6) "McLonn" 
    ["organizer_key"]=> string(19) "1234567890123456789" 
    ["version"]=> string(1) "3" } 



account_type->
    email->sales@example.com
    sales@example.com
    firstName->Mary
    lastName->McCann
    organizer_key->1234567890123456789
    version->3

foreach内部的同一行工作正常,这就是为什么它两次向我显示电子邮件的原因。

我正在使用php-curl-class / php-curl-class 4.8

我真的不明白我在做什么错,或者在哪里寻找解决方案。

感谢您的帮助。

编辑

如果我愿意

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}

我收到错误:未定义的属性:stdClass :: $ email
但是如果我这样做

$this->email = $curl->response->email

我没有错误,$ this->电子邮件已加载正确的值

0 个答案:

没有答案