0

Im trying to add data to each of the array objects below.

[
       {
          "first_name":"Chris",
          "surname":"Mowbray",
          "id":"28"
       },
       {
          "first_name":"Gary",
          "surname":"Hume",
          "id":"29"
       },
       {
          "first_name":"Ian",
          "surname":"Hume",
          "id":"30"
       }
    ]

this is the php that generates the array shown:

<?php
$selectedMatch = '( select max(event.match_id) from event)';
// need to update this match id based on usered slected on site
$getMatchPlayers = "select first_name,surname, id\n" . "from player, match_player\n" . "where player.id = match_player.player_id\n" . "and match_player.match_id = $selectedMatch";
$result = mysql_query($getMatchPlayers) or die(mysql_error());
$temp = array();
while ($row = mysql_fetch_assoc($result)) {
    $temp[]    = $row;
    $player_id = $row['id'];
    $AppResult = mysql_query(getApps($player_id)) or die(mysql_error());
    $appearence = mysql_fetch_assoc($AppResult);
    // fb($temp);
}



header('Content-Type: application/json');
echo json_encode($temp);
exit;
function getApps($playerid){
    $getAppearances = "SELECT COUNT(player_id) \n" . "AS Appearances \n" . "FROM match_player\n" . "where player_id = $playerid\n";
    return $getAppearances;
}
?>

im trying to add new data after 'id' of every object but everything that I have tried has added a new index to the array.

$appearence is what i want to add

3
  • Are you sure that this is an ArrayObject and not simply an associative array? Commented Mar 3, 2014 at 0:01
  • I think it is an associative array, im quite new to PHP Commented Mar 3, 2014 at 0:04
  • @MarkBaker... forgot to tag Commented Mar 3, 2014 at 0:13

3 Answers 3

1

$row['appearance']=$appearance['Appearance'] and assign $temp[]=$row as the last statement in the while()

Sign up to request clarification or add additional context in comments.

19 Comments

Note that you can write a query which returns all the lines you want, with all the data you need in one step (the DBMS is faster than PHP, and SQL query turnaround is time-consuming).
$temp[] = $row; needs to be at the top as i need the data in order to return $AppResult see.
Yes, you should do what Lewyx said and return all your data in one query.
@clhereistian i dont think that is possible in my instance
@ChrisMowbray: sorry, I think you're wrong on both instances, but feel free to elaborate your point! The query would be something like this: select first_name,surname,id, (SELECT COUNT(player_id) AS Apperances FROM match_player mp WHERE mp.player_id=match_player.player_id) from player INNER JOIN match_player ON player.id = match_player.player_id WHERE match_player.match_id = ? If you DBMS does not allow that kind of parameter access in subselects, then you have a bit harder job to do (INNER JOIN on an aggregate subquery) but it will still be lighting fast compared to your approach.
|
1

Have you tried this?

 $temp['newKey'] = $appearence;  

3 Comments

Try adding your new value to $row before you add $row to $temp.
i cannot do that as the new value is found using data in row... $player_id = $row['id'] and then player_id is used in the sql to return the new data $appearance
$row is a simple array, you can read AND write its elements in any order you wish; just don't assign to the whole array, because that will replace the whole.
0

Try adding a merged array containing both results after you've retrieved all required information.

while ($row = mysql_fetch_assoc($result)) {
    $player_id  = $row['id'];
    $AppResult  = mysql_query(getApps($player_id)) or die(mysql_error());
    $appearence = mysql_fetch_assoc($AppResult);
    $temp[]     = array_merge($row, $appearance);
}

Be careful not to have the same array key in $row and $appearance when doing this, presume you won't have a problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.