亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

格式化解析的json數組

格式化解析的json數組

PHP
九州編程 2022-05-27 16:43:21
我有一個項目,我有一個編碼的 json 數組,需要對其進行格式化,以便數組內的元素在 index.php 文件中相互打印。這是我想要的數組內元素的輸出,在彼此下方打印出來,每個新索引都有一個空格到目前為止,這是我的代碼:allNames.php<?php// Get the string from the URL$json = file_get_contents('http://5dd559d3ce4c300014402cb8.mockapi.io/onboard/posts');printAll($json);$array= array();function printAll($json){// Decode the JSON string into an object$obj = json_decode($json,true);$elementCount  = count($obj);// In the case of this input, do key and array lookups to get the valuesfor($i=0; $i<$elementCount; $i++){    #delimit based on added * after each attribute to ensure all elemets are added and not cut short    $array[]=explode("*",$obj[$i]["id"]."*".$obj[$i]["createdAt"]."*".$obj[$i]["name"]            ."*".$obj[$i]["avatar"]."*".$obj[$i]["jobDescription"]."*".$obj[$i]["userEmail"]."*"            .$obj[$i]["userIpAddress"]."*".$obj[$i]["userAgent"]);} echo json_encode($array);}?>索引.php<html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script><script type="text/javascript">function ajax_post(){     $.post("allNames.php", {    }, function(data) {      var returnedData = JSON.parse(data);      $("#data").html(returnedData + "<br>");    });    };</script></head><body><h2>Ajax Post to PHP and Get Return Data</h2><input name="myBtn" type="submit" value="Check" onclick="ajax_post()"> <br><br><center> <p id="data" style="color:black"></p><br><br> </center></body></html>
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

這會格式化 JSON 以進行打印。這是一個工作示例。


該$dataToPrint變量包含您要從數據中打印的所有內容。它檢查空數據,如果沒有輸出數據,也不會打印分隔符。它非常簡單,它循環遍歷所有內容。


如果您不想限制打印的內容,只需刪除$dataToPrint變量和array_search(...條件,然后打印即可。


指數:


<html>

<head>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <script type="text/javascript">


        function ajax_post(){

            $.post("allNames.php", {

            }, function(data) {

                // var returnedData = JSON.parse(data);

                $("#data").html(data);

            });

        };

    </script>

</head>

<body>

<h2>Ajax Post to PHP and Get Return Data</h2>

<input name="myBtn" type="submit" value="Check" onclick="ajax_post()"> <br><br>

<center> <p id="data" style="color:black"></p><br><br> </center>

</body>

</html>

全名:


<?php

// Get the string from the URL

$json = file_get_contents('http://5dd559d3ce4c300014402cb8.mockapi.io/onboard/posts');

$decodedJson = json_decode( $json, TRUE );

$dataToPrint = array(

    'id',

    'createdAt',

    'name',

    'avatar',

    'jobDescription'

);

$returningString = "";

foreach ( $decodedJson as $dataToDisplay ) {

    if ( is_array( $dataToDisplay ) && ! empty( $dataToDisplay ) ) {

        $printSeparator = FALSE;

        foreach ( $dataToDisplay as $key => $value ) {

            if ( array_search( $key, $dataToPrint ) !== FALSE ) {

                $printSeparator = $printSeparator || TRUE;

                $returningString .= "{$key}: {$value} <br/>";

            }

        }

        // Printed all from current, adding a HR or extra line break. Change as desired

        if ( $printSeparator ) {

            $returningString .= "<hr>";

        }

    }

}


echo $returningString;


查看完整回答
反對 回復 2022-05-27
  • 1 回答
  • 0 關注
  • 210 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號