dton37910
2017-04-07 07:41
浏览 50
已采纳

PHP数组字符串键末尾添加数字

Good day fellas!

I have this block of code where i create a JSON string and i have a dynamic number of data so what i need is also a dynamic name for a key..

if (mysql_num_rows($result) > 0) {
    $response["members"] = array();
    $x = 0;
    $members = array();
    while ($row = mysql_fetch_array($result)) {
        $members = array();
        $members["member" + (string)$x] = array();
        $member["member_id"] = $row["member_id"];
        $member["firstname"] = $row["firstname"];
        $member["mi"] = $row["mi"];
        $member["lastname"] = $row["lastname"];
        $member["email"] = $row["email"];
        $member["username"] = $row["username"];
        $member["password"] = $row["password"];
        $member["guild_id"] = $row["guild_id"];
        $member["guild_name"] = $row["guild_name"];
        $member["guild_code"] = $row["guild_code"];
        array_push($members["member" + (string)$x], $member);
        $x++;
    }
    array_push($response["members"], $members);

    echo json_encode($response);;
}

In JQuery this type of method works.. my question is, is there any way i can achieve it using php? If you know i will be very happy to know...

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutao1939 2017-04-07 08:12
    已采纳

    1.Suggestion:-stop using deprecated+removed(php5 +php7) version of mysql_*.Move towards mysqli_* OR PDO along with prepared statements(Prevent Sql Injection)

    2.Why creating unnecessary array and multiple push.No need to do that.Do like below:-

    $response["members"] = array();//put outside
    if (mysql_num_rows($result) > 0) {
        $x = 0;
        while ($row = mysql_fetch_array($result)) {
            $response["members"][$x]["member_id"] = $row["member_id"]; //assign value directly to the resultant array
            $response["members"][$x]["firstname"] = $row["firstname"];
            $response["members"][$x]["mi"] = $row["mi"];
            $response["members"][$x]["lastname"] = $row["lastname"];
            $response["members"][$x]["email"] = $row["email"];
            $response["members"][$x]["username"] = $row["username"];
            $response["members"][$x]["password"] = $row["password"];
            $response["members"][$x]["guild_id"] = $row["guild_id"];
            $response["members"][$x]["guild_name"] = $row["guild_name"];
            $response["members"][$x]["guild_code"] = $row["guild_code"];
            $x++;
        }
    }
    
    if(count($response["members"])>0){ //check finally that array is not empty
     echo json_encode($response); //echo json encoded array data
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drg17404 2017-04-07 07:45

    if you want to achieve that the members array contains individual records with keys as member0, member1, etc., then you could proceed as:

    if (mysql_num_rows($result) > 0) {
        $x = 0;
        $members = array();
        while ($row = mysql_fetch_array($result)) {
            $member = array();
            $member["member_id"] = $row["member_id"];
            $member["firstname"] = $row["firstname"];
            $member["mi"] = $row["mi"];
            $member["lastname"] = $row["lastname"];
            $member["email"] = $row["email"];
            $member["username"] = $row["username"];
            $member["password"] = $row["password"];
            $member["guild_id"] = $row["guild_id"];
            $member["guild_name"] = $row["guild_name"];
            $member["guild_code"] = $row["guild_code"];
            $members["member$x"] = $member;
            $x++;
        }
        $response["members"] = $members;
    
        echo json_encode($response);
    }
    
    评论
    解决 无用
    打赏 举报