2016-05-21 29 views
-1

我有我願做一個搜索中,並瞭解是否有具體的ID回答數組,如果它是其所有值傳遞到另一個陣列和陣列從主數組中移除它。我嘗試了一堆循環,但它們只是讓我的程序運行得更慢。 我也嘗試:在一個數組尋找一個特定值

$key = array_search('2', array_column($myarray, 'id')); 

但隨後被退回無關。我也在cakephp 3試過集合班,但那對我沒有幫助。 所以我把我的陣列在這裏希望有人會幫我把這個扔掉。我應該使用蛋糕PHP 3

\src\Template\Comlibs\get_Result.ctp (line 10) 
[ 
(int) 0 => object(App\Model\Entity\Comlib) { 

    'id' => (int) 1, 
    'question' => 'how to kill someone?', 
    'answer' => (int) 2, 
    'asked' => (int) 90, 
    'tags' => 'kill,professional killer', 
    'created' => null, 
    'modified' => null, 
    'answers' => [ 
     (int) 0 => object(App\Model\Entity\Answer) { 

      'id' => (int) 2, 
      'question_id' => (int) 1, 
      'answer' => 'worst behaviour', 
      'rate' => (float) 5, 
      'view' => (int) 29, 
      'helpful' => '33', 
      '[new]' => false, 
      '[accessible]' => [ 
       '*' => true 
      ], 
      '[dirty]' => [], 
      '[original]' => [], 
      '[virtual]' => [], 
      '[errors]' => [], 
      '[invalid]' => [], 
      '[repository]' => 'Answers' 

     }, 
     (int) 1 => object(App\Model\Entity\Answer) { 

      'id' => (int) 3, 
      'question_id' => (int) 1, 
      'answer' => 'mothafucka never luv us', 
      'rate' => (float) 8, 
      'view' => (int) 60, 
      'helpful' => '22', 
      '[new]' => false, 
      '[accessible]' => [ 
       '*' => true 
      ], 
      '[dirty]' => [], 
      '[original]' => [], 
      '[virtual]' => [], 
      '[errors]' => [], 
      '[invalid]' => [], 
      '[repository]' => 'Answers' 

     }, 
     (int) 2 => object(App\Model\Entity\Answer) { 

      'id' => (int) 4, 
      'question_id' => (int) 1, 
      'answer' => 'that bitch aint a part of me', 
      'rate' => (float) 4, 
      'view' => (int) 76, 
      'helpful' => '75', 
      '[new]' => false, 
      '[accessible]' => [ 
       '*' => true 
      ], 
      '[dirty]' => [], 
      '[original]' => [], 
      '[virtual]' => [], 
      '[errors]' => [], 
      '[invalid]' => [], 
      '[repository]' => 'Answers' 

     }, 
     (int) 3 => object(App\Model\Entity\Answer) { 

      'id' => (int) 5, 
      'question_id' => (int) 1, 
      'answer' => 'she was so friendly', 
      'rate' => (float) 2.5, 
      'view' => (int) 21, 
      'helpful' => '10', 
      '[new]' => false, 
      '[accessible]' => [ 
       '*' => true 
      ], 
      '[dirty]' => [], 
      '[original]' => [], 
      '[virtual]' => [], 
      '[errors]' => [], 
      '[invalid]' => [], 
      '[repository]' => 'Answers' 

     }, 
     (int) 4 => object(App\Model\Entity\Answer) { 

      'id' => (int) 6, 
      'question_id' => (int) 1, 
      'answer' => 'the old movie', 
      'rate' => (float) 3.2, 
      'view' => (int) 11, 
      'helpful' => '11', 
      '[new]' => false, 
      '[accessible]' => [ 
       '*' => true 
      ], 
      '[dirty]' => [], 
      '[original]' => [], 
      '[virtual]' => [], 
      '[errors]' => [], 
      '[invalid]' => [], 
      '[repository]' => 'Answers' 

     } 
    ], 
    '[new]' => false, 
    '[accessible]' => [ 
     '*' => true 
    ], 
    '[dirty]' => [], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[invalid]' => [], 
    '[repository]' => 'Comlibs' 

} 
] 

TNX先進

回答

0

提到IM您可以使用集合CakePHP的,你必須使用收集的匹配方法

use Cake\Collection\Collection; 
$collection = new Collection($data['answers']); 
$answer = $collection->match(['answers.id' => 2]); 

Collection::match

+0

當我使用$ data ['answers']它說:只有一個數組或\ T可以使用可逆的Collection,但是當我只使用$ data時沒有錯誤,我爲$ collection做了每一件事,而我得到的是一個沒有我的第二個數組的ID = 2的數組,現在我該如何使用這個新集合$回答)因爲當我使用它們是空的和toArray()顯示一個空數組 –