基于块中包含的值获取数组块的最快方法(不工作)

https://gist.github.com/jcranny/9465715

I have this array (example)...

Array
(
    [0] => Array
        (
            [series] => stdClass Object
                (
                    [term_id] => 5
                    [name] => Moto2 2013
                    [slug] => moto2-2013
                    [term_group] => 0
                    [term_taxonomy_id] => 3
                )

            [race_number] => 77
            [team] => Technomah carXpert
            [constructor] => Suter
            [machine] => Honda CBR600RR
        )
    [1] => Array
        (
            [series] => stdClass Object
                (
                    [term_id] => 6
                    [name] => Moto2 2014
                    [slug] => moto2-2014
                    [term_group] => 0
                    [term_taxonomy_id] => 3
                )

            [race_number] => 15
            [team] => La Fonte Tascaracing
            [constructor] => Suter
            [machine] => Honda CBR600RR
        )
    [2] => Array
        (
            [series] => stdClass Object
                (
                    [term_id] => 7
                    [name] => Moto2 2015
                    [slug] => moto2-2015
                    [term_group] => 0
                    [term_taxonomy_id] => 3
                )

            [race_number] => 15
            [team] => Mapfre Aspar Team Moto2
            [constructor] => Suter
            [machine] => Honda CBR600RR
        )
)

Now I would like to be able to get information from each block.

For example I would like to echo this data:

  • [race_number]
  • [team]
  • [constructor]
  • [machine]

But I want only to echo this data that is relevant to a specific [series]

I have the [series] term_id so this is my key to get the relevant data, but i'm struggling to get my node function work.


This is the function to do this:

function node_modify_riders_array($rider_id)
{
        $fields = get_field("rider_series_data", $rider_id);
        foreach($fields as $field_key => $field_val)
        {
            $new_fields[$field_val["series"]] = $field_val;
        }
return $new_fields; 
}


Then this is how I am get the series specific data based on the series term id.

$rider_series_data    = node_modify_riders_array($rider_id);    
$series_id            = $series[0]->term_id;

$team            = $rider_series_data[$series_id]["team"];
$contstructor    = $rider_series_data[$series_id]["constructor"];   
$machine         = $rider_series_data[$series_id]["machine"];
$race_number     = $rider_series_data[$series_id]["race_number"];


But some thing is wrong and I can work it out. Can anyone see where I'm going wrong or help me fix it.

Massive thanks in advance, would really appreciate some help.


What the problem is:

My function node_modify_riders_array is returning null, which is causing my $team, $contstructor, $machine, and $race_number too not output anything.

If I echo $series_id on my example, I get 6

Which should pass though my node_modify_riders_array function and display the relevant array values. But it's not outputting anything and I got no errors.

This is my full code so you can see what I am trying to do...

https://gist.github.com/jcranny/9465715

You are using an object as your array key and not the term_id of your series object.

function node_modify_riders_array($rider_id)
{
        $fields = get_field("rider_series_data", $rider_id);
        foreach($fields as $field_key => $field_val)
        {
            $new_fields[$field_val["series"]->term_id] = $field_val;
                                          //^^^^^^^^^  <--- add this
        }
        return $new_fields; 
}