RDowns RDowns - 1 month ago 6
MySQL Question

How do I strip a column/node out of an array in php?

I have an array and a function that currently prints this output:


{"player_id":"2"}{"player_id":"31"}{"player_id":"31"}{"player_id":"32"}


I need to strip out the node "player_id" so I am just left with an array of numbers.

This is my current code:

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;
}
}
}


This returns the array back to specificCommunity.php where the code continues:

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


However this doesn't work and returns:


Notice: Trying to get property of non-object in /var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php on line 35
[[null]]
Notice: Trying to get property of non-object in /var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php on line 35
[[null,null]]
Notice: Trying to get property of non-object in /var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php on line 35
[[null,null,null]]
Notice: Trying to get property of non-object in /var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php on line 35
[[null,null,null,null]]

Answer

If you really want to do that then do it in the code that gets the data from the database, so you create a numerically indexed array in this processing

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;
}

Then you can forget about messing with the result of this function