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.

2
  • 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 Commented Sep 5, 2018 at 7:48
  • example 3: [3] => Array ( [ID] => any number [USER_ID] => any number [DATA] => anything [VALUE] => something ) ) Commented Sep 5, 2018 at 11:06

3 Answers 3

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.

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

1 Comment

Thank You! that was exactly what I needed
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);

Comments

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.

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.