Welcome to WebmasterWorld Guest from 107.20.75.63

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Convert associative big array to small array in php

Convert associative big array to small array in php

     
11:05 pm on Dec 22, 2013 (gmt 0)

New User

joined:Dec 22, 2013
posts: 1
votes: 0


Hello,

-> I have an array like this,

array(
name => 'acc',
attr => Array(
id => 32
),
children => Array(
Array(
name => 'name',
attr => Array(),
children => Array(
'test'
),
),
Array(
name => 'cats',
attr => Array(),
children=>Array(
Array(
name => 'cat',
attr => Array(
id => 43
),
children => Array(
Array(
name => 'url',
attr => Array(),
children => Array(
'http://www.google.com'
)
)
)
),
Array(
name => 'cat',
attr => Array(
id => 87
),
children => Array(
Array(
name => 'url',
attr => Array(),
children => Array(
'http://www.yahoo.com'
)
)
)
)
)
)
)
);

I want this array to be like below array,

array
(
'acc' => Array
(
'id' => 32,
'name' => 'test',
'cats' => Array
(
'cat' => Array
(
'0' => Array
(
'id' => 43,
'url' => 'http://www.google.com/'
),
'1' => Array
(
'id' => 87,
'url' => 'http://www.yahoo.com'
)
)
)
)
);

Any help would be appreciate.
11:13 pm on Dec 30, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Have you managed to get anywhere with this? What code/approach have you tried?

How have you output your array data? It would be useful, for anyone attempting to tackle your problem, to output valid PHP (var_export), which your target array appears to be pretty close to. But your source data seems to be some hybrid var dump?