-2

I have an array like this:

Array
(
    [0] => Array
        (
            [Edisi] => Array
                (
                    [0] => 193|Oktober|2001| 
                )

            [Pengantar] => Array
                (
                    [0] =>  Halo!! 
                )

I would like to flatten it to this:

Array
(
  [Edisi]=>193|oktober|2001|,
  [Pengantar=>Halo!!
)
zrvan
  • 7,533
  • 1
  • 22
  • 23
Joe Merra
  • 29
  • 3
  • this was already asked, see http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php – pdu Jan 16 '12 at 07:50
  • 1
    Have you made any attempt to solve it on your own? If so, what did you come up with? Please edit your question with the additional information. – zrvan Jan 16 '12 at 08:01

2 Answers2

0

Have you seen array_values()? If not, try this:

$newArray = Array(
    'edisi' => $oldArray[0]['edisi'][0],
    'Pengantar' => $oldArray[0]['Pengantar'][0]
);

No offense, but this isn't that complex, and has been asked on this site a few times before. We're not here to do your homework or build your programs for you. Please but more time and thought into your problem before you go around asking for help. Then, if you really do need help, explain what you have tried and why you are having problems.

cegfault
  • 6,442
  • 3
  • 27
  • 49
0

If this array does not change in type (meaning there is always first level, then your indices and then the zero index for your values), you could do it like that:

$array = array(
    array(
        'foo' => array([0] => 'hello'),
        'foo2' => array([0] => 'hello2'),
    ),
    array(
        'wecanhandlemore' => array([0] => 'hello3'),
        'wecanhandlemore2' => array([0] => 'hello4'),
    ),
);
$target = array();
foreach ($array as $subarray) {
    foreach ($subarray as $key => $valWithIndex) {
        $target[$key] = $valWithIndex[0];
    }
}

Should result in:

array(
    'foo' => 'hello',
    'foo2' => 'hello2',
    'wecanhandlemore' => 'hello3',
    'wecanhandlemore2' => 'hello4',
);
aufziehvogel
  • 7,167
  • 5
  • 34
  • 56