如何在php中从数组中删除列/节点?

时间:2016-10-26 11:08:07

标签: php mysql

我有一个数组和一个当前打印此输出的函数:

  

{ “player_id”: “2”} { “player_id”: “31”} { “player_id”: “31”} { “player_id”: “32”}

我需要删除节点“player_id”,所以我只剩下一组数字。

这是我目前的代码:

specificCommunity.php:

$communityPlayerIds = array();
$communityPlayersIds = $dao->getSpecificCommunity($id)

MySQLDao.php

public function getSpecificCommunity($id)
{

    $returnValue = array();
    $sql = "SELECT community_players.player_id\n"
. "from community_players\n"
. "where community_players.community_id = '".$id."'";

  $result = $this->conn->query($sql);
    if($result != null && (mysqli_num_rows($result) >= 1)){
while($row = $result -> fetch_array(MYSQLI_ASSOC)){
   if(!empty($row)){
      $returnValue[] = $row;
   }
}
}

这会将数组返回到特定的社区.php,代码继续:

$in_statement = array();
foreach($communityPlayersIds as $player_id) 
{
  $in_statement[] = $player_id->player_id;
  echo json_encode(array($in_statement));
}

然而,这不起作用并返回:

  

注意:尝试在第35行的/var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php中获取非对象的属性   [[空值]]   注意:尝试在第35行的/var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php中获取非对象的属性   [[NULL,NULL]]   注意:尝试在第35行的/var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php中获取非对象的属性   [[NULL,NULL,NULL]]   注意:尝试在第35行的/var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php中获取非对象的属性   [[NULL,NULL,NULL,NULL]]

3 个答案:

答案 0 :(得分:2)

如果您真的想这样做,那么在从数据库获取数据的代码中执行此操作,以便在此处理中创建数字索引数组

public function getSpecificCommunity($id)
{

    $returnValue = array();
    $sql = "SELECT community_players.player_id
            from community_players
            where community_players.community_id = '$id'";

    $result = $this->conn->query($sql);
    if($result != null && (mysqli_num_rows($result) >= 1)){
        while($row = $result -> fetch_array(MYSQLI_ASSOC)){
            $returnValue[] = $row['player_id'];
        }
    }
    return $returnValue;
}

然后你可以忘记搞乱这个功能的结果

答案 1 :(得分:0)

您可以将代码改进为:

首先,您需要从数组中获取结果而不是来自object:

$in_statement[] = $player_id['player_id'];

第二次,您还需要从循环中删除json_encode(),无需在循环内使用它,您可以将其移出循环。

echo json_encode($in_statement);

第三次,不知道为什么在json_encode()方法中再次使用数组。

第四,检查您的getSpecificCommunity()方法,您没有返回值,您需要使用:

return $returnValue; // at last line, this will return the output

,如果$id是用户输入或会话值而不是您需要准备的语句,它可以帮助您阻止使用SQL攻击的代码。

答案 2 :(得分:0)

<?php


public function getSpecificCommunity($id)
{

    $returnValue = array();
    $sql = "SELECT community_players.player_id from community_players where community_players.community_id = '$id'";

  $result = $this->conn->query($sql);
    if($result != null && (mysqli_num_rows($result) >= 1)){
while($row = $result -> fetch_array(MYSQLI_ASSOC)){
   if(!empty($row)){
      $returnValue[] = $row['player_id'];
   }
}


//convert array to json data (As you want)

$data_json=json_encode($returnValue);

//return json data

return $data_json;


}

//call your function

$recieve_data= getSpecificCommunity('31');

// check via var_dump() or print_r
你为什么在你的代码中收到通知/警告???答。
$in_statement = array();
foreach($communityPlayersIds as $player_id) 
{
  $in_statement[] = $player_id->player_id;
  echo json_encode(array($in_statement));
}

因为$ player_id是对象数组的元素...