尝试输出数组时出现Php错误

时间:2017-08-25 15:28:13

标签: php wordpress

我在自定义帖子类型中有一个可重复的字段,该字段链接到另一个自定义帖子类型。我想循环遍历可重复字段,然后为每个字段访问链接的帖子类型中的数据。返回第一个结果,但在第二个结果中出现以下错误:

  

致命错误:字符串不支持[]运算符。

我尝试从我的变量中删除括号,例如$ staff = $ coach ['team_staff'],但这不起作用。

我也试过设置$ staff = array();在循环之前,这不起作用。

不确定我的错误在哪里:

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    foreach ( $coaches as $coach ) :
        $staff[] = $coach['team_staff']; 
        $role[] = $coach['team_role'];

        // Loop through each staff member
        foreach( $staff as $index => $staff ) :
            $args = array (
                'post_type' => 'staff', 
                'title' => $staff
            );

            $posts = get_posts( $args );
            foreach ( $posts as $post ) : setup_postdata ( $post );

                // get post meta here

            endforeach;
        endforeach;

    endforeach;
endif;

2 个答案:

答案 0 :(得分:1)

您需要小心循环变量名称。例如,将$ staff更改为$ staff_member:

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    foreach ( $coaches as $coach ) :
        $staff[] = $coach['team_staff']; 
        $role[] = $coach['team_role'];

        // Loop through each staff member
        foreach( $staff as $index => $staff_member ) :
            $args = array (
                'post_type' => 'staff', 
                'title' => $staff_member
            );

            $posts = get_posts( $args );
            foreach ( $posts as $post ) : setup_postdata ( $post );

                // get post meta here

            endforeach;
        endforeach;

    endforeach;
endif;

理想情况下,您应该在循环外部初始化数组,$ staff和$ role:

$staff = [];
$role = [];

此外,还不清楚为什么你会反复添加$ staff数组并在$ coaches数组的每次迭代时循环遍历它。考虑将两个foreach循环分开并依次运行它们:

global $post;

// Get The Staff Members
$coaches = get_post_meta($post->ID, 'repeatable_fields', true);
if ( $coaches ) :
    $staff = [];
    $role = [];

    foreach ( $coaches as $coach ) :
        $staff[] = $coach['team_staff']; 
        $role[] = $coach['team_role'];
    endforeach;

    // Loop through each staff member
    foreach( $staff as $index => $staff_member ) :
        $args = array (
            'post_type' => 'staff', 
            'title' => $staff_member
        );

        $posts = get_posts( $args );
        foreach ( $posts as $post ) : setup_postdata ( $post );

            // get post meta here

        endforeach;
    endforeach;

endif;

答案 1 :(得分:0)

您的代码设计错误:

  • 你的第二个foreach嵌套在第一个foreach中,并遍历第一个foreach中填充的数组df = pd.read_excel('CurrPrevRate1.xlsx') df.head() dftest = df[:100] # Replace zeros with NaN dftest[['y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006','y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']] = dftest[['y2000','y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006','y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']].replace(0, np.nan) #Change all values in these columns to floats #dftest[['y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006','y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']] = dftest[['y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006','y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']].apply(pd.to_numeric) #Get average of rows dftest['AvgRating'] = dftest[['y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006','y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']].mean(axis=1) def getCurrRate(): for x in dftest['y2017']: if 0 <= x <= 10: return x else: for y in dftest['y2016']: if 0 <= y <= 10: return y else: for z in dftest['y2015']: if 0 <= z <= 10: return z else: return 'N/A' dftest['Curr_Rate'] = dftest[['y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006','y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']].apply(getCurrRate(), axis=1) dftest 。我不知道它是否有意,这意味着在第一个foreach的第一个循环中,第二个foreach将遍历一个条目,一个条目放入其中两行之前,通过第二个循环上的两个条目等......
  • 你的第二个foreach遍历$staff 同时命名它正在使用$staff的值。执行第二个foreach之后,以前称为$staff的数组已消失,并被其第一个条目的值覆盖,该值恰好是$staff

当第一个string正在执行第二个循环时,您的代码因此尝试使用索引运算符 - foreach - 到字符串,如错误所示。

试试这个:

[]

我在数组中添加了一个复数,因此它们包含多个角色/人员更加明显,因为您应用了命名另一个数组global $post; // Get The Staff Members $coaches = get_post_meta($post->ID, 'repeatable_fields', true); if ( $coaches ) : $staffs = $roles = array(); foreach ( $coaches as $coach ) : $staffs[] = $coach['team_staff']; $roles[] = $coach['team_role']; } // Loop through each staff member foreach( $staffs as $index => $staff ) : $args = array ( 'post_type' => 'staff', 'title' => $staff ); $posts = get_posts( $args ); foreach ( $posts as $post ) : setup_postdata ( $post ); // get post meta here } } } 的相同规则并将其值命名为{{1在$coaches