遍历 get_posts 以创建嵌套的多维数组和 JSON

Loop through get_posts to create a nested multidimensional array and JSON

提问人:Lotte_Char 提问时间:10/24/2023 最后编辑:Lotte_Char 更新时间:10/25/2023 访问量:51

问:

我对这个wp_query功能有疑问。我需要创建一个嵌套的JSON,如下所示:

{
    "country_zone A": [
        {
            "tarif_zone A": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone B": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            " tarif_zoneC": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone D": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            }
        }
    ],
    " country_zone B": [
        {
            "tarif_zone A ": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone B": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone C": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone D": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            }
        }
    ],
    "country_zone C": [
        {
            " tarif_zone A": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone B": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone C": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            },
            "tarif_zone D": {
                "out": "0.0",
                "in": "0.2",
                "data": "1"
            }
        }
    ]
}

我有与这些字段相对应的 ACF 字段,我需要这个 JSON 来检索 JS 前端中的数据。

这是我的php代码:

add_action('wp_ajax_country_search', 'country_search_callback');
add_action('wp_ajax_nopriv_country_search', 'country_search_callback');

function country_search_callback()
{
    header("Content-Type: application/json");

     $args_pays = array(
        "post_type" => 'pays',
        "posts_per_page" => 50,
    );

    $args_tarif = array(
        "post_type" => 'tarif',
        "posts_per_page" => 50,

    );

    $tarifs = get_posts($args_tarif);

    $countries = get_posts($args_pays);
    if ($countries);
    if ($tarifs) {
        foreach ($countries as $country) :
            setup_postdata($country);
            $country_names[] =
                get_the_title($country);
            $country_zones[] = get_field('pays_id_zone', $country);
        endforeach;
        foreach ($tarifs as $tarif) :
            setup_postdata($tarif);
            $tarif_zone[] =
                get_field('tarif_dans_id_zone', $tarif);
            $tarif_type[] = get_field('tarif_type', $tarif);
            $tarif_sens[] = get_field('tarif_sens', $tarif);
            $tarif_vers[] = get_field('tarif_vers_id_zone', $tarif);
            $tarif_in[] = get_field('tarif_de_id_zone', $tarif);

        endforeach;
        $premiere_cle[] = array_fill_keys($tarif_zone, $country_zones);
        $deuxieme_cle[] = array_fill_keys($country_zones, $tarif_type);
        $troisieme_cle[] = array_fill_keys($tarif_type, $tarif_sens);
        $quatrieme_cle[]  = array_fill_keys($tarif_sens, array($tarif_vers, $tarif_in));

    };

    echo json_encode($premiere_cle);
wp_die()
}

使用此代码,我获得了以下 JSON 对象:

[{"A":["D","E","B","C","B","B","A","A","Z"],"C":["D","E","B","C","B","B","A","A","Z"],"E":["D","E","B","C","B","B","A","A","Z"],"B":["D","E","B","C","B","B","A","A","Z"],"":["D","E","B","C","B","B","A","A","Z"]}]

如果您明白我的意思,我如何组合所有不同的数组,现在它们都有一个值,该值应该是另一个数组的键?

最后,我试图重新创建这样的东西:https://www.orange.be/fr/options-et-services/international-roaming/tarifs

所有帮助将不胜感激!谢谢!

php jquery json wordpress 高级自定义字段

评论

0赞 disinfor 10/25/2023
WP_Query可能不是要走这条路。您可以考虑改用。获取所有帖子,将它们放在一个数组中,然后对于每个帖子,然后用于将帖子类型帖子设置为子数组。get_posts()paysget_poststariff
0赞 CBroe 10/25/2023
无法从您的示例中看出,每个国家/地区下面的所有这些条目是否完全相同(看起来帽子方式),或者您是否需要为每个国家/地区获取不同的条目......?但是代码看起来两者之间没有任何特定的关联,您将以任何方式检查这些关联。因此,如果它们都相同 - 那么我将首先创建该子数组,然后在国家/地区的循环中,只需将其添加到每个国家/地区键下的结果结构中即可。tarif_zone X
0赞 Lotte_Char 10/25/2023
你好!感谢你们俩的帮助!我更新了我的问题,因为我仍然无法找到答案!
0赞 CBroe 10/25/2023
“如果你明白我的意思,我怎么能组合所有不同的数组,现在它们都有一个值,应该是另一个数组的键?”-不,我没有。我仍然无法真正说出,你到底想要什么。即使是你从这里开始收集数据的方式——在多个数组中“并行”,而不是在一个二维数组中,对我来说也没有多大意义。
0赞 CBroe 10/25/2023
我认为您应该首先将 tarif 数据放入数组中 - 并使用区域标识符作为键。所以这将是这样的.然后,当您遍历您的国家/地区时,您只需使用为该国家/地区存储的区域标识符从该数组中选择正确的条目即可。['A' => ['out' => ..., 'in' => ..., ...], 'B' => [...], ...]

答: 暂无答案