1

I've tried some solutions from:

In PHP, how do you change the key of an array element?

php array from multidimensional array keys values

But its is not exacly what i need, i tried to mix some solutions but notting helped.

I have an array from my database:

Array ( 
    [0] => Array (
        [ID] => 1 
        [USER_ID] => 1
        [DATA] => UNIQUE 
        [VALUE] => buuu ) 
    [1] => Array (
        [ID] => 2 
        [USER_ID] => 1 
        [DATA] => NICKNAME 
        [VALUE] => NoAd ) ) 

And i want to transform that database to:

Array ( 
    [UNIQUE] => buuu
    [NICKNAME] => NoAd
    [any new [2]...[3]... from previous array

after that code:

foreach($playerdata as $segment){
                    foreach($segment as $key => $value ){
                    $newArray[$value] = $value;
                }
            }

my array looks like:

Array ( [UNIQUE] => UNIQUE 
        [buuu] => buuu 
        [NICKNAME] => NICKNAME 
        [NoAd] => NoAd ) 

i tried use 3x foreach but it ends in error all time i think i need to change some variables in my foreach but no idea how.

NoAd
  • 141
  • 1
  • 14
  • What? I can't see any logic here. Also you need to expand your example with more than "any new [2]...[3]... from previous array". And preferably a second item that needs to be merged so that we see a more complete picture – Andreas Sep 05 '18 at 07:48
  • example 3: [3] => Array ( [ID] => any number [USER_ID] => any number [DATA] => anything [VALUE] => something ) ) – NoAd Sep 05 '18 at 11:06

3 Answers3

1

Now that I see the other answers it seems it's array_column you are looking for.

It returns an array column and the third parameter is what the key name should be.

$player_data = array(array(
"ID" => 1,
"USER_ID" => 1,
"DATA" => "UNIQUE",
"VALUE" => "buuu"
),
array(
"ID" => 1,
"USER_ID" => 1,
"DATA" => "NICKNAME",
"VALUE" => "NoAd"
));
$new = array_column($player_data, "VALUE", "DATA");
var_dump($new);

Output:

array(2) {
  ["UNIQUE"]=>
  string(4) "buuu"
  ["NICKNAME"]=>
  string(4) "NoAd"
}

https://3v4l.org/ZAkgZ

There is no need for loops to solve this.

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

You could try something like the following:

$newArray = array();    
foreach($playerdata as $segment){
    $newArray[$segment['DATA']] = $segment['VALUE'];
}

This code gets the DATA as key and VALUE as value from each part of the array and stores it in $newArray.

Ruben Pauwels
  • 350
  • 3
  • 13
0

lets assume $playerdata has the below values

Array ( 
    [0] => Array (
        [ID] => 1 
        [USER_ID] => 1
        [DATA] => UNIQUE 
        [VALUE] => buuu ) 
    [1] => Array (
        [ID] => 2 
        [USER_ID] => 1 
        [DATA] => NICKNAME 
        [VALUE] => NoAd ) ) 
    [2]----
    [3]----        

$newArray = [];
foreach($playerdata as $record) {
    $newArray[$record['DATA']] = $record['DATA'];
    $newArray[$record['VALUE']] = $record['VALUE'];
}

print_r($newArray);
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193