0

I would like to create a PHP array that will end up looking like this:

idPlayer  namePlayer Points
1             John     20
2             Sam      25
3             Ben      22

But I would like to append the values not all at once:

  • First append the idPlayer and namePlayer
  • Then append the points for that idPlayer (I would have a $idPlayer variable to use each time I loop)

How could I do this in PHP? I was thinking:

$myArray['idPlayer'] = "1";
$myArray['namePlayer'] = "John";
$myArray['Points'] = "20"

And then, how would I tell the array to go to the next row?

user2509541
  • 268
  • 4
  • 14
  • you don't. you make an array of arrays. `$myArray[] = array(idplayer => 1, nameplayer=>john, points=>20)` – Marc B May 30 '14 at 15:00

3 Answers3

0

Use the idPlayer as index:

$players = [
    1 => [
        'namePlayer' => 'John',
        'points' => 20,
    ],
    5 => [
        'namePlayer' => 'Seth',
        'points' => 25,
    ],
    13 => [
        'namePlayer' => 'Ben',
        'points' => 35,
    ],
];

var_dump($players);

http://codepad.viper-7.com/nSXmZF

In php <5.4 use array() instead of [] constructor.

Misiur
  • 5,019
  • 8
  • 38
  • 54
  • but in your example everyone has the same idPlayer (1)? I need to assign an idPlayer per player – user2509541 May 30 '14 at 15:03
  • I assumed you have serial id's. Check the update. Of course - this disallows duplicate playerid's. – Misiur May 30 '14 at 15:08
  • And then, if I order my array by Points desc, will the index also get reordered? – user2509541 May 30 '14 at 15:09
  • Well, there is no builtin function to sort by key in subarray. [Take a look here](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value). So, whatever your final solution will be, you'll probably end up with multidimensional array – Misiur May 30 '14 at 15:21
0
// output array
$myArray = array();

// your loop
while (something) {

  // make an array for this iteration
  $itarr = array();

  // put stuff in it
  $itarr['idPlayer'] = "1";
  $itarr['namePlayer'] = "John";
  $itarr['Points'] = "20"

  // append it to output array using standard array indexing
  $myArray[] = $itarr;
  // OR, your own index
  $myArray[$itarr['idPlayer']] = $itarr;

}
James
  • 20,957
  • 5
  • 26
  • 41
0

I dont know why you want to achieve such thing. But consider this example:

// like player table array initial
$players = array(
    array(
        'idPlayer' => 1,
        'namePlayer' => 'John',
    ),
    array(
        'idPlayer' => 2,
        'namePlayer' => 'Sam',
    ),
    array(
        'idPlayer' => 3,
        'namePlayer' => 'Ben',
    ),
);


// data points to be added later (like points table)
$points = array(
    array(
        'idPlayer' => 1,
        'Points' => 20,
    ),
    array(
        'idPlayer' => 2,
        'Points' => 25,
    ),
    array(
        'idPlayer' => 3,
        'Points' => 22,
    ),
);

$myArray = array();
foreach($players as $key => $value) {
    foreach($points as $index => $element) {
        // if this particular id matches the record inside points table then merge
        if($value['idPlayer'] == $element['idPlayer']) {
            $myArray[] = array('idPlayer' => $value['idPlayer'], 'namePlayer' => $value['namePlayer'], 'Points' => $element['Points']);
        }
    }
}

Should output something like: (can be used in a tabular data)

Array
(
    [0] => Array
        (
            [idPlayer] => 1
            [namePlayer] => John
            [Points] => 20
        )

    [1] => Array
        (
            [idPlayer] => 2
            [namePlayer] => Sam
            [Points] => 25
        )

    [2] => Array
        (
            [idPlayer] => 3
            [namePlayer] => Ben
            [Points] => 22
        )

)
user1978142
  • 7,946
  • 3
  • 17
  • 20