2

我有一个对象数组,如下所示:

[0] => stdClass Object
    (
        [lid] => 2492
        [post_title] => Winter League 2012/2013
        [eid] => 2121
        [etitle] => a 2013
        [rid] => 2358
        [rtitle] => sdcc2013_2M_W
        [rtype] => W
    )

[1] => stdClass Object
    (
        [lid] => 2492
        [post_title] => Winter League 2012/2013
        [eid] => 2121
        [etitle] => a 2013
        [rid] => 2359
        [rtitle] => sdcc2013_4M_M
        [rtype] => M
    )

[2] => stdClass Object
    (
        [lid] => 2492
        [post_title] => Winter League 2012/2013
        [eid] => 2123
        [etitle] => e 2013
        [rid] => 2360
        [rtitle] => eircom_Women_2.5Mile
        [rtype] => W
    )

[3] => stdClass Object
    (
        [lid] => 2492
        [post_title] => Winter League 2012/2013
        [eid] => 2123
        [etitle] => e 2013
        [rid] => 2362
        [rtitle] => eircom2013_men_5mile
        [rtype] => M
    )

我想“得到”“摆脱”值的子集

 (2358,2359,1360,2362)

我应该使用哪些数组操作函数?

4

2 回答 2

4

我会建议array_map()

$original_array; // your original array
$rid_array = array_map(function($val) {
    return $val->rid;
}, $original_array);

或者对于 PHP < 5.3

$original_array; // your original array
$rid_array = array_map('map_function', $original_array);

function map_function($val) {
    return $val->rid;
}
于 2013-03-05T19:18:37.163 回答
1

我将array_map()与设置对象的 rid 字段的回调函数一起使用。

function set_rid($obj, $val)
{
    return $obj->rid = $val;
}

$my_rids = array(
    '1',
    '2',
    '3',
    '4'
);
array_map('set_rid', $my_array, $my_rids);
于 2013-03-05T19:19:55.807 回答