将Wordpress post meta转换为PHP数组

时间:2015-11-15 06:16:00

标签: php mysql arrays wordpress

我有一个WordPress帖子元到MySql,如

public class FilterChoice extends AppCompatActivity {
    RadioButton rb;
    String filter;
    String selection, domain, temp;
    RadioGroup rg;
    public static ArrayList<KeyValuePair> cate_old;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = new LinearLayout(FilterChoice.this);
        LinearLayout.LayoutParams lrp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(lrp);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        // rb = new RadioButton(FilterChoice.this);
        // rb.setText("All");
        // rg.addView(rb);

        Intent intent = getIntent();
        filter = intent.getExtras().getString("filter");

        if (filter.equals("Price Range")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.prices.size() == 1) {

                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.price_s_sel + "-"
                        + SearchResults.price_e_sel + " ("
                        + SearchResults.price_val + ")");
                rg.addView(rb);

            } else {
                int it;
                for (it = 0; it < SearchResults.prices.size() - 1; it++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.prices.get(it).key + "-"
                            + SearchResults.prices.get(it + 1).key + " ("
                            + SearchResults.prices.get(it).value + ")");
                    rg.addView(rb);
                }
                rb = new RadioButton(FilterChoice.this);
                rb.setText("Above" + SearchResults.prices.get(it).key + " ("
                        + SearchResults.prices.get(it).value + ")");
                rg.addView(rb);
            }

        } else if (filter.equals("Sites")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.site.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.site_sel + " ("
                        + SearchResults.site_val + ")");
                rg.addView(rb);
                // }
            } else {
                StringTokenizer st;
                for (int i = 0; i < SearchResults.site.size(); i++) {
                    st = new StringTokenizer(SearchResults.site.get(i).key, ".");
                    if (st.hasMoreTokens()) {
                        domain = st.nextToken();
                    }
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(domain + " (" + SearchResults.site.get(i).value
                            + ")");
                    rg.addView(rb);
                }
            }
        } else if (filter.equals("Categories")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.cate.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.cate_sel + " ("
                        + SearchResults.cate_val + ")");
                rg.addView(rb);
                // }
            } else {
                for (int i = 0; i < SearchResults.cate.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.cate.get(i).key + " ("
                            + SearchResults.cate.get(i).value + ")");
                    rg.addView(rb);
                }
            }

        } else if (filter.equals("Colors")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.cols.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.cols_sel + " ("
                        + SearchResults.cols_val + ")");
                rg.addView(rb);
            } else {
                for (int i = 0; i < SearchResults.cols.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.cols.get(i).key + " ("
                            + SearchResults.cols.get(i).value + ")");
                    rg.addView(rb);
                }
            }

        } else if (filter.equals("Brands")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.brands.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.brand_sel + " ("
                        + SearchResults.brand_val + ")");
                rg.addView(rb);
            } else {
                for (int i = 0; i < SearchResults.brands.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.brands.get(i).key + " ("
                            + SearchResults.brands.get(i).value + ")");
                    rg.addView(rb);
                }
            }
        } else if (filter.equals("Sub Categories")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.subcate.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.subcate_sel + " ("
                        + SearchResults.subcate_val + ")");
                rg.addView(rb);
            } else {
                for (int i = 0; i < SearchResults.subcate.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.subcate.get(i).key + " ("
                            + SearchResults.subcate.get(i).value + ")");
                    rg.addView(rb);
                }
            }
        }

        linearLayout.addView(rg);
        setContentView(linearLayout);

        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (filter.equals("Price Range")) {

                } else if (filter.equals("Sites")) {
                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.site.size() > 1) {
                        selection = SearchResults.site.get(checkedId - 1).key;
                        temp = SearchResults.site.get(0).key;

                        SearchResults.site_sel = selection;
                        SearchResults.site_val = SearchResults.site
                                .get(checkedId - 1).value;
                        SearchResults.site_id = checkedId;
                    }

                } else if (filter.equals("Categories")) {
                    Log.e("checkedId", "" + checkedId);

                    if (SearchResults.cate.size() > 1) {

                        selection = SearchResults.cate.get(checkedId - 1).key;
                        temp = SearchResults.cate.get(0).key;
                        SearchResults.cate_sel = selection;
                        SearchResults.cate_val = SearchResults.cate
                                .get(checkedId - 1).value;
                        SearchResults.cate_id = checkedId;
                    }

                } else if (filter.equals("Colors")) {

                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.cols.size() > 1) {

                        selection = SearchResults.cols.get(checkedId - 1).key;
                        temp = SearchResults.cols.get(0).key;
                        SearchResults.cols_sel = selection;
                        SearchResults.cols_val = SearchResults.cols
                                .get(checkedId - 1).value;
                        SearchResults.cols_id = checkedId;
                    }
                } else if (filter.equals("Brands")) {
                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.brands.size() > 1) {

                        selection = SearchResults.brands.get(checkedId - 1).key;
                        temp = SearchResults.brands.get(0).key;
                        SearchResults.brand_sel = selection;
                        SearchResults.brand_val = SearchResults.brands
                                .get(checkedId - 1).value;
                        SearchResults.brand_id = checkedId;
                    }
                } else if (filter.equals("Sub Categories")) {
                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.subcate.size() > 1) {
                        selection = SearchResults.subcate.get(checkedId - 1).key;
                        temp = SearchResults.subcate.get(0).key;
                        SearchResults.subcate_sel = selection;
                        SearchResults.subcate_val = SearchResults.subcate
                                .get(checkedId - 1).value;
                        SearchResults.subcate_id = checkedId;
                    }

                }

                Intent intent = new Intent(FilterChoice.this, Filters.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

    }
}

我需要将其转换为[{"field":"Email:1","title":"email","explanation_text":"","explanation_text_location":"","html_styling":"","text_to_display":"","show_title_field":"","pdf_file":"","pdf_file_button_styling":"","pdf_file_button_text":""}] 数组。我使用以下代码将其作为数组。

PHP

但它让我回头

$wpaf_field_title = maybe_unserialize(get_post_meta(52, '__wpaf_field_title', true));
print_r(json_encode($wpaf_field_title));

1 个答案:

答案 0 :(得分:2)

正如评论中所说,你必须使用json_decode()函数:

$json = '[{"field":"Email:1","title":"email","explanation_text":"","explanation_text_location":"","html_styling":"","text_to_display":"","show_title_field":"","pdf_file":"","pdf_file_button_styling":"","pdf_file_button_text":""}]';
$data = json_decode( $json );
var_dump( $data );

然后你得到:

array(1) {
  [0]=>
  object(stdClass)#1 (10) {
    ["field"]=>
    string(7) "Email:1"
    ["title"]=>
    string(5) "email"
    ["explanation_text"]=>
    string(0) ""
    ["explanation_text_location"]=>
    string(0) ""
    ["html_styling"]=>
    string(0) ""
    ["text_to_display"]=>
    string(0) ""
    ["show_title_field"]=>
    string(0) ""
    ["pdf_file"]=>
    string(0) ""
    ["pdf_file_button_styling"]=>
    string(0) ""
    ["pdf_file_button_text"]=>
    string(0) ""
  }
}