数组未赋值的元素值

未初始化
如果是全局变量,自动赋0;
如果是局部变量,随机值,所以未初始化字符数组,最后要赋个’\0’;
在这里插入图片描述
已经初始化
局部变量和全局变量,未赋值元素都为0;

<div class="post-text" itemprop="text"> <p>I have an array which I return from a class. It contains data for me which I need in the controller of my application. However something strange is happening. Some values are when I get them by key not defined. But when I print the whole array they are defined. But that's not all. Some keys return their value, some don't.</p> <p>To test if the key can be found I used <code>array_key_exists()</code>, which returned true. Let's get to the example. When I print <code>$this->case->data</code>, it will return the following;</p> <pre><code>[id] => 1 [ticket_number] => 2334 [user_id] => 2 [ticket_state] => 1 </code></pre> <p>Which is okay. To test this out I tried returning the ticket_number value. (<code>$this->case->data['ticket_number']</code>). It worked perfectly and returned it's value. However when I try to return ticket_state (<code>$this->case->data['ticket_state']</code>) I get an <code>undefined</code> as value.</p> <p>It might be that I'm missing something here. What am I doing wrong? And if I'm doing it wrong how can it be that <code>ticket_number</code> works, but <code>ticket_state</code> does not?</p> <p><strong>Edit:</strong></p> <p>I do get undefined as value returned. Not an undefined index error. I did a var_dump() on the variable.</p> <pre><code>array(4) { ["id"]=> string(1) "1" ["ticket_number"]=> string(14) "2334" ["user_id"]=> string(1) "2" ["ticket_state"]=> string(1) "1" } </code></pre> <p><strong>Edit II:</strong></p> <p>My code where I try to receive the value is the following simple if statement;</p> <pre><code> if($this->case->data['ticket_state'] == 2){ $this->case->reopen(); } </code></pre> <p>I've tried adding the <code>2</code> in quotes, but since <code>$this->case->data['ticket_state']</code> is undefined it the statement will be executed regardless. (Which I found strange too. Why should it still be executed when undefined is not the same as 2 or '2'?).</p> <p>The current value of my column ticket_state is <code>1</code>.</p> </div>
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页