duanbi2760 2016-05-21 08:28
浏览 39

在数组中查找特定值

i have an array that i would like to do a search in it and find out if an specific id in answers array and if it is pass all its values to another array and remove it from the primary array.i tried bunch of loops but they just make my program to run slower. i also tried :

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

but then nothing was returned. i also tried collection class in cakephp 3 but that did not helped me. so i put my array here hopefully someone will help me get throw this. i should mention that im using 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条回答 默认 最新

  • douao1579 2016-05-21 14:59
    关注

    you can use the Collection of Cakephp, you have to use the match method of collection

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

    Collection::match

    评论

报告相同问题?