I have this PHP one dimensional array:
Array
(
[Female--N] => 11
[Male--N] => 11
[Humans--N] => 11
[Adult--N] => 8
[Adolescent--N] => 8
[Reaction Time-physiology--N] => 6
[Acoustic Stimulation-methods--N] => 6
[Schizophrenia-genetics--Y] => 5
[Motion Perception--N] => 3
)
And i want a new array from this that looks like (i think this tow-dimensional..?):
Array
(
[Female][N] => 11
[Male][N] => 11
[Humans][N] => 11
[Adult][N] => 8
[Adolescent][N] => 8
[Reaction Time-physiology][N] => 6
[Acoustic Stimulation-methods][N] => 6
[Schizophrenia-genetics][Y] => 5
[Motion Perception][N] => 3
)
Can i use split method on key elements?
Little bit harder… i also need to split on the single ‘_’ underscore, i did this to prevent the columns getting mixed up… But the example below doesn’t do the job right…
$new_array = array();
foreach($MeshtagsArray as $key => $value) {
$parts = explode('__', $key, 2);
$parts2 = explode('_', $key, 2);
$new_array[] = array(
'discriptor' => $parts[0],
'qualifier' => $parts2[1],
'major' => $parts[1],
'#occurence' => $value
);
So the output should be something like:
[0] => Array
(
[discriptor] => Female
[qualifier] =>
[major] => N
[#occurence] => 11
........
[5] => Array
(
[discriptor] => Reaction Time
[qualifier] => physiology
[major] => N
[#occurence] => 6
Best regards,
Thijs
UPDATED
$new_arraywill now be a numerically indexed, multidimensional array. Each top level key will contain an associative array of the elements you require.In the future, feel free to explain the entire problem from the beginning, that way we can all better help you!
Explanation
According to php.net:
The array in your problem is associative; it’s keys are strings. This makes it is a simple matter to iterate over the array with
foreachand explode the keys into parts. I use a limit parameter to ensure that there will be no more than two parts.Also, since one delimiter is a doubled version of the other, we have to first explode on the double
--delimiter, and then explode on the single-delimiter.Technically, we could have used a single explode—with no limit parameter—and the single
-delimiter. Then we could have inferred which element part belonged where. However, sometimes there is noqualifier. To get around this problem, I’ve used two explodes, and a ternary operator that counts the returned number of elements from the second explode.