找一个阵列内的单个元素,而无需通过数组循环 [英] Find a single element within an array without looping through the array

查看:132
本文介绍了找一个阵列内的单个元素,而无需通过数组循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在它里面对象的数组。我想抓住的对象有测试歌曲投票-02的名称数组中为止。我知道我可以循环直通它,做一个有条件的每个对象中签的名字,但我不知道是否有一个数组/对象PHP函数,可以返回的对象,其中name ='测试歌曲民意测验-03

 阵列

    [0] => stdClass的对象
        (
            [名] =>测试歌曲投票-01
            [说明] =>测试歌曲投票-01
            [created_at] => 2014-05-02T23:07:59Z
            [计] => stdClass的对象
                (
                    [批准] => 63787
                    [未决] => 341
                    [驳回] => 78962
                    [总] => 143090
                )            [TPM] => 12
            [approved_tpm] => 3
            [PCT] => 4
        )    [1] => stdClass的对象
        (
            [名] =>测试歌曲投票-02
            [说明] =>测试歌曲投票-02
            [created_at] => 2014-05-02T23:17:20Z
            [计] => stdClass的对象
                (
                    [批准] => 9587
                    [未决] => 0
                    [驳回] => 9780
                    [总] => 19367
                )            [TPM] =>五
            [approved_tpm] => 3
            [PCT] => 1
        )    [2] => stdClass的对象
        (
            [名] =>测试歌曲投票-03
            [说明] =>测试歌曲投票-03
            [created_at] => 2014-05-02T23:19:06Z
            [计] => stdClass的对象
                (
                    [批准] => 26442
                    [未决] => 0
                    [驳回] => 36242
                    [总] => 62684
                )            [TPM] => 25
            [approved_tpm] => 9
            [PCT] => 2
        )

更新了我的code,以显示我多么想传递一个变量:

 函数get_results()
{
    $#标签=测试松轮询-03;
    $这个 - >负载>模型('artist_model');
    $数据['结果'] = $这个 - > artist_model-> get_results();    $ =为MyObject array_filter($数据['结果'] - >流功能($ E,$#标签){
      返回STRCMP($ E-GT&;名,$#标签)== 0;
    });    的print_r($ myObject的);
}


解决方案

您可以使用array_filter。

  $ myobjects = array_filter($对象,函数($ E)使用($#标签){
  返回STRCMP($ E-GT&;名称,测试松轮询-03)== 0;
});

由于匿名函数,这只会工作PHP> = 5.3

如果你有旧的版本,您可以使用自己的功能。

I have an array with objects stored within it. I want to grab the object in the array that has a name of test-song-poll-02. I know I could loop thru it and do a conditional to check the name within each object but I was wondering if there is an array/object php function that can return the object where name = 'test-song-poll-03'

Array
(
    [0] => stdClass Object
        (
            [name] => test-song-poll-01
            [description] => test-song-poll-01
            [created_at] => 2014-05-02T23:07:59Z
            [count] => stdClass Object
                (
                    [approved] => 63787
                    [pending] => 341
                    [rejected] => 78962
                    [total] => 143090
                )

            [tpm] => 12
            [approved_tpm] => 3
            [pct] => 4
        )

    [1] => stdClass Object
        (
            [name] => test-song-poll-02
            [description] => test-song-poll-02
            [created_at] => 2014-05-02T23:17:20Z
            [count] => stdClass Object
                (
                    [approved] => 9587
                    [pending] => 0
                    [rejected] => 9780
                    [total] => 19367
                )

            [tpm] => 5
            [approved_tpm] => 3
            [pct] => 1
        )

    [2] => stdClass Object
        (
            [name] => test-song-poll-03
            [description] => test-song-poll-03
            [created_at] => 2014-05-02T23:19:06Z
            [count] => stdClass Object
                (
                    [approved] => 26442
                    [pending] => 0
                    [rejected] => 36242
                    [total] => 62684
                )

            [tpm] => 25
            [approved_tpm] => 9
            [pct] => 2
        )
)

UPDATED my code to show how I want to pass a variable in:

function get_results()
{
    $hashtag = "test-song-poll-03";
    $this->load->model('artist_model');
    $data['results'] = $this->artist_model->get_results();

    $myobject = array_filter($data['results']->streams, function($e, $hashtag) {
      return strcmp($e->name, $hashtag) == 0;
    });

    print_r($myobject);
}

解决方案

You can use array_filter.

$myobjects = array_filter($objects, function($e) use($hashtag) {
  return strcmp($e->name, "test-song-poll-03") == 0;
});

Because of the anonymous function, this will only work PHP >= 5.3

You can use your own function if you have older version.

这篇关于找一个阵列内的单个元素,而无需通过数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆