2014-01-17 23 views
0

我正在使用foreach循環來獲取數據,然後使用數據從數據庫中獲取信息。 但是,當使用$main[0]->group_number(文字不是整數),則返回錯誤的數據,像015 == 01不能在mysql中使用foreach值

Product::Budget($budget_id)->Active(1)->Parent($main[0]->group_number)->get(); 

但是當我把$主[0] - > GROUP_NUMBER在$字符串,然後它返回正確的數據。

$value = $main[0]->group_number; 
Product::Budget($budget_id)->Active(1)->Parent($value)->get(); 

現在它返回正確的數據,01 == 01和015 == 015

爲什麼第一個返回錯誤的數據,第二個正確的數據?

// This takes data from another table and puts it into $main 
@foreach($groups->insideGroup($budget_id, $group->number, null, 2) as $main) 
    // This takes data from the last table and puts it into $job, but if using $main value in the Parent() scope, it returns wrong values. Except when putting the value into $string 
    @foreach(Job::Budget($budget_id)->Active(1)->Parent($main[0]->group_number)->get() as $job) 
    @endforeach 
@endforeach 
+0

@ ling.s更新我的第一篇 –

+0

不要使用嵌套的foreach循環。我相信PHP正在使用一些內部指針,所以兩個foreach循環影響彼此的位置。而是使用正常的循環。 –

+0

但是,你能解釋爲什麼我不能在第二個foreach中使用第一個foreach $ main [0] - > group number,而是返回錯誤的數據。但是如果我把第一個foreach $ main放在$ string中,那麼第二個foreach會返回正確的數據? –

回答

0

我相信這是PHP的工作類型雜耍。 您可以使用測試了這一點:

var_dump(015); 
var_dump('015'); 

PHP把第一個八進制數 http://www.php.net/manual/en/language.types.integer.php

+0

$ string = $ main [0] - > group_number和$ main [0] - > group_number返回爲'string',但只有$ string在查詢中起作用,所以它返回正確的數據。 –