2016-10-26 10 views
-1

Iはアレイ現在、この出力を印刷する機能を有する。phpの配列から列/ノードを取り除くにはどうしたらいいですか?

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

これはバックコードが継続specificCommunity.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で非オブジェクトのプロパティを取得しようとすると[ [null]] Notice:/var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php 35行目の非オブジェクトのプロパティを取得しようとしています [[null、null]] 通知: /var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php 35行目の非オブジェクトのプロパティを取得 [[null、null、null]] 注意:非オブジェクトのプロパティを取得しようとしています/var/sites/q/quasisquest.uk/public_html/KeepScore/specificCommunity.php on line 35 [[NULL、NULL、NULL、NULL]]

+0

'も'エコーjson_encode(配列($のin_statement))を使用し '$のplayer_id [ 'player_id']と試みる;'外ループ、または最良のオプションは 'print_r($ communityPlayersIds);' chk thisと結果を共有します – devpro

+1

それはそれでした!乾杯、私は答えに変換しているいくつかのより多くの提案があることを知っておいて良いです。 – RDowns

+0

。 – devpro

答えて

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

その後、あなたはあなたのようにあなたのコードを向上させることができ、この機能

+0

素晴らしい提案、私はまた 'json_encode'メソッド内で使用することができます。 – devpro

+1

@devpro Yupですが、この関数を呼び出した後にそれを残すと、必要に応じて配列を配列として使用する選択肢があります。 – RiggsFolly

+0

よろしくお願いします。 – devpro

0

の結果をいじりを忘れることができます。

まずあなたがいないオブジェクトからの配列から結果を取得する必要があります:

$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攻撃のコードを防ぐのに役立つ文です。

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 
あなたのコードには注意/警告が表示されますか? Ans。
$in_statement = array(); 
foreach($communityPlayersIds as $player_id) 
{ 
    $in_statement[] = $player_id->player_id; 
    echo json_encode(array($in_statement)); 
} 

$のplayer_idは、配列の要素は、オブジェクトのinstedあるので...

関連する問題