在数组中查找特定值

时间:2016-05-21 08:28:00

标签: php arrays multidimensional-array cakephp-3.0

我有一个数组,我想在其中搜索并找出答案数组中的特定 id ,以及是否将其所有值传递给另一个数组并从主数组中删除它。我尝试了一堆循环,但它们只是让我的程序运行得更慢。 我也尝试过:

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

但后来没有任何回复。我也尝试过cakephp 3中的收藏课,但这对我没有帮助。 所以我把我的阵列放在这里希望有人会帮助我抛出这个。我应该提到我使用 cake 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' => 'He 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 in advanced

1 个答案:

答案 0 :(得分:0)

你可以使用 Cakephp 的集合,你必须使用集合的匹配方法

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

Collection::match