json 如何在PHP中过滤和循环数组[关闭]

new9mtju  于 2023-08-08  发布在  PHP
关注(0)|答案(2)|浏览(101)

已关闭。此问题需要details or clarity。它目前不接受回答。
**希望改进此问题?**通过editing this post添加详细信息并阐明问题。

18天前关门了。
Improve this question
我希望能够从这个GET API中提取每个网络计划的价格。有人能帮帮我吗?
我想将AIRTEL数据计划[dataPlan、duration、type、status和Price for basic_user]提取为选择选项格式。
我这样做了:

$url = "https://subandgain.com/api/databundles.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response,true);
$suppliers = array();
$customers = array();
//echo "<pre>";
//print_r($obj);
foreach ($obj as $rkey => $resource){
    if ($resource['NETWORK'] == 'AIRTEL'){
        $customers[] = $resource;
    } else if ($resource['NETWORK'] == 'MTN') {
        $suppliers[] = $resource;
    }
}

header('Content-Type: application/json');
$dataplan = json_encode($customers);
$objs = json_decode($dataplan,true);
echo "<pre>";
print_r($objs);

字符串

9rbhqvlz

9rbhqvlz1#

$url = "https://subandgain.com/api/databundles.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response, true);
$suppliers = array();
$customers = array();
$temp = array();
$dataPlan = array();
$kPrice = array();

foreach ($obj as $rkey => $resource) {

    $allPlans = array();
    foreach ($resource['BUNDLE'] as $key) {
        $dataPlan = array('dataPlan'=>$key['dataPlan'], 'duration'=>$key['duration'], 'type'=>$key['type'], 'status'=>$key['status']);
        $kPrice = array();

        foreach ($key['price'] as $keyPrice){
            $kPrice = array('price_basic_user' => $keyPrice['basic_user']);

//            $dataPlan['Price_basic_user'] = $keyPrice['basic_user']; // if you want [Price_basic_user] => 43 below [status] => Active

//            $kPrice[] = array('price_basic_user' => $keyPrice['basic_user']); // if there are multiple basic_user price uncomment
        }

        $dataPlan['Price'] = $kPrice; // comment this if you want [Price_basic_user] => 43 below [status] => Active
        $allPlans[] = $dataPlan;
    }
    $temp[$resource['NETWORK']] =  $allPlans;

}
echo '<pre>';
print_r($temp);

字符串
试试这个!

fcipmucu

fcipmucu2#

$objs变量返回json_decode。替换为json_encode。所以我做了调整,PHP得到了这个格式。

<?php
$url = "https://subandgain.com/api/databundles.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response,true);
$customers = array();
$suppliers = array();

foreach ($obj as $rkey => $resource){
    switch($resource['NETWORK']){
        case "AIRTEL":
            $customers[] = $resource;
            break;
        case "MTN":
            $suppliers[] = $resource;
            break;
    }
}

header('Content-Type: application/json');
$dataplan = json_encode($customers);
$objs = json_encode($dataplan);
echo $objs;

字符串
假设上面代码所在的文件名为api-plans.php,在前面的文件中,我使用了以下代码

<body>
    <div class="content"></div>
</body>
<script type="text/javascript" src="path_your_js/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $.ajax({
            type: "post",
            url: "api-plans.php",
            async: true,
            data: "",
            beforeSend: function() {
            },
            success: function( response ) {
                responseJson = JSON.parse(response);
                var contentHTML = "";
                var responseJsonNetWork = responseJson[0]['BUNDLE'];

                $.each(responseJsonNetWork, function(i, item) {
                    contentHTML = contentHTML + "Data Plan: " + responseJsonNetWork[i]['dataPlan'] + "<br />";
                    contentHTML = contentHTML + "Duration: " + responseJsonNetWork[i]['duration'] + "<br />";
                    contentHTML = contentHTML + "Type: " + responseJsonNetWork[i]['type'] + "<br />";
                    contentHTML = contentHTML + "Status: " + responseJsonNetWork[i]['status'] + "<br />";
                    contentHTML = contentHTML + "Price (basic_user): " + responseJsonNetWork[i]['price'][0]['basic_user'] + "<br /><br />";
                });

                $(".content").html(contentHTML);
            }
        });
    });
</script>


希望这对你有帮助

相关问题