0

So I've got the following output;

Array
(
    [0] => Array
        (
            [PlayerID] => 0
            [Nickname] => Alban_Berisha
            [Score] => 420
            [Ping] => 96
        )

    [1] => Array
        (
            [PlayerID] => 1
            [Nickname] => Flaviano_Villegas
            [Score] => 122
            [Ping] => 95
        )

    [2] => Array
        (
            [PlayerID] => 2
            [Nickname] => Tyrone_DeRozan
            [Score] => 0
            [Ping] => 120
        )

    [3] => Array
        (
            [PlayerID] => 3
            [Nickname] => Tage_Thomas
            [Score] => 3
            [Ping] => 246
        )

    [4] => Array
        (
            [PlayerID] => 4
            [Nickname] => Jamal_Williams
            [Score] => 149
            [Ping] => 149
        ) 
)

I want a list containing every PlayerID coupled with the Nickname, but I keep getting various errors. What is the correct way of doing this? I have worked with arrays before but I cannot seem to extract the data from it without getting an error

One of the ways I've tried to do this;

    for(new $x=0; $x<5; $x++)
{
    echo (var_dump($array[$x]["PlayerID"])." ".var_dump($array[$x]["Nickname"]));
}
1
  • 3
    Show an example of the code you've tried and your expected output. Commented Mar 21, 2014 at 12:00

4 Answers 4

1

Using PHP 5.5's new array_column() function

$newArray = array_column($originalArray, 'Nickname', 'PlayerID');

or with earlier versions of PHP:

$newArray = array_combine(
    array_map(
        function($value) { return $value['PlayerID']; },
        $originalArray
    ),
    array_map(
        function($value) { return $value['Nickname']; },
        $originalArray
    )
);
Sign up to request clarification or add additional context in comments.

Comments

0

What were you trying to do with new x?

<?php
$array = array(
            array('PlayerID' => 0,
                  'Nickname' => 'Alban_Berisha',
                  'Score' => 420,
                  'Ping' => 96
                  ),
            array('PlayerID' => 1,
                  'Nickname' => 'Flaviano_Villegas',
                  'Score' => 122,
                  'Ping' => 95
                  ),
            array('PlayerID' => 2,
                  'Nickname' => 'Tyrone_DeRozan',
                  'Score' => 0,
                  'Ping' => 120
                  ),
            array('PlayerID' => 3,
                  'Nickname' => 'Tage_Thomas',
                  'Score' => 3,
                  'Ping' => 246
                  ),
            array('PlayerID' => 4,
                  'Nickname' => 'Jamal_Williams',
                  'Score' => 149,
                  'Ping' => 149
                  )
            );
print_r($array); 
foreach($array as $arr){
    echo $arr['PlayerID'] .' '.$arr['Nickname'] . "<br />";
}
?>

Data:

Array
(
    [0] => Array
        (
            [PlayerID] => 0
            [Nickname] => Alban_Berisha
            [Score] => 420
            [Ping] => 96
        )

    [1] => Array
        (
            [PlayerID] => 1
            [Nickname] => Flaviano_Villegas
            [Score] => 122
            [Ping] => 95
        )

    [2] => Array
        (
            [PlayerID] => 2
            [Nickname] => Tyrone_DeRozan
            [Score] => 0
            [Ping] => 120
        )

    [3] => Array
        (
            [PlayerID] => 3
            [Nickname] => Tage_Thomas
            [Score] => 3
            [Ping] => 246
        )

    [4] => Array
        (
            [PlayerID] => 4
            [Nickname] => Jamal_Williams
            [Score] => 149
            [Ping] => 149
        )
)

0 Alban_Berisha
1 Flaviano_Villegas
2 Tyrone_DeRozan
3 Tage_Thomas
4 Jamal_Williams

2 Comments

That's it! I tried the for(x) to see if I could get the first 5 results, however it was far from right. I'm still studying on PHP so I consider every mistake a useful one. Thank you for your answer, it helped me loads!
No problem, glad to have helped. You were almost there, just remove 'new' and dont' use var_dump. for($x=0; $x<5; $x++) { echo ($array[$x]["PlayerID"]." ".$array[$x]["Nickname"]) ."<br/>"; } Though in general, it's better to use foreach to loop through data arrays.
0

Do you mean you want a new array with player ID and nickname?

If so:

$ouputArray = array();

foreach ($oldArray as $player){
 $ouputArray[$player['PlayerID']] = $player['Nickname'];
}

print_r($outputArray);

Comments

0
$results = array();
foreach ($players as $player) {
    $results[ $player['PlayerID'] ] = $player['Nickname'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.