...

View Full Version : Array Convertion Help?



DJCMBear
06-16-2010, 04:58 PM
Hi I was trying to create an array convertion function for my php class but got stuck some of the way through so thought I would ask for some help.

What i'm trying to do is covert an array like this.


array(
'Game::Name::GameName',
'Game::Points::null',
'Game::Lives::10',
'Game::Guesses::null',
'Player::ID::1234567890'
);
To an array like this


array(
[Game] => array(
[Name] => 'GameName',
[Points] => null,
[Lives] => 10,
[Guesses] => null,
),

[Player] => array(
[ID] => 1234567890
)
);
I tried doing a continuous function but it didn't add the sections under the game section.

Any Help Welcome, Thank You

DJCMBear

Fumigator
06-16-2010, 05:08 PM
Something like this perhaps:



foreach ($inAarray as $val)
{
$data = explode("::", $val);
$outArray[$data[0]][$data[1]] = $data[2];
}

DJCMBear
06-16-2010, 05:14 PM
Would this keep on going so if I had something like Game::Errors::Internal::null

Would it display like this?


array(
[Game] => array(
[Errors] => array(
[Internal] => null
)
)
)
What i'm looking for is a function that counts how many sections there are and the makes an array for that section.

so like


array(
'Main',
'NewMain::Inner',
'OldMain::Outer::Inner',
'NewOld::OldMain::Outer::Inner'
)
Would become this.


array(
[0] => 'Main',
[NewMain] => 'Inner',
[OldMain] => array(
[Outer] => 'Inner'
),
[NewOld] => array(
[OldMain] => array(
[Outer] => 'Inner'
)
)
)

DJCMBear
06-17-2010, 12:08 PM
can no one help???

Fumigator
06-17-2010, 05:03 PM
Well you changed the rules between your first and third posts. I'm just waiting for you to give us the final requirements.

DJCMBear
06-17-2010, 05:25 PM
Basicly what I am looking for is a function which will do something like this.

The function will lookat the string of each section by using func_get_args() and then for each of the arguments it can be any length so like this main::section::new or like main::name or even main::section::new::home, but the code in post #2 it only gets the first two parts and outputs the third part so if there are more or less than three sections it wont work.

I need to check how many section there are in each of the function arguments and then make an array for that section.

If this doesn't make sence im sorry but its hard to explain.

Morri
06-17-2010, 05:47 PM
here is a code snippet, unfortunaly its just the other way round. it cleans the dimensions of the array... But i think you should be able to modify it


function nested_Array_to_one_dimensional_Array ($nested_Array) {
//ein paar Variablen statisch definieren, sodass sie die Rekursion überleben
static $one_dimensional_Array_keys;
static $one_dimensional_Array_values;
static $prefix = '';
static $prefix_old;
if($prefix == '') {
$one_dimensional_Array_keys = array();
$one_dimensional_Array_values = array();
}
foreach ($nested_Array as $key => $value) {
if (!is_array($value)) {
//vor dem aktuellen Schlüssel ein Prefix und ein Unterstrich anbringen, falls nötig
if ($prefix != '') {$key = $prefix.'_'.$key;}
//Den Schlüsselnnamen und den Wert sichern
$one_dimensional_Array_keys[] = $key;
$one_dimensional_Array_values[] = $value;
} else {
//Das Prefix der momentanen Iteration sichern
$prefix_old = $prefix;
//Unterstrich am Anfang des Schlüssels vermeiden
if ($prefix == '') {
$prefix = $key;
} else {
$prefix = $prefix."_".$key;
}
//rekursiv das nächste Array einhängen
nested_Array_to_one_dimensional_Array($value);
//Das Prefix auf die Sicherung zurücksetzen
$prefix = $prefix_old;
}
}
//Die beiden Array-Paare (Schlüssel und Werte) verbinden
$one_dimensional_Array = array_combine($one_dimensional_Array_keys, $one_dimensional_Array_values);
return $one_dimensional_Array;
}


Morri

DJCMBear
06-19-2010, 04:34 PM
Thanks for the code but it didn't help me with what im trying to do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum