/    Sign up×
Community /Pin to ProfileBookmark

Create or rearrange the current array based on it’s value in php

Hi Guys,

I am really stuck with something and can’t get a way out.I am having a problem in creating a new array based on existing array.I have the following array and I want to match elements ‘id’ with ‘parent’ element. If the value is same then I want to create another array where the child elements will be under parent element.

[CODE]Array
(
[0] => Array
(
[id] => 66
[parent] => 26

)

[1] => Array
(
[id] => 47
[parent] => 26

)

[2] => Array
(
[id] => 48
[parent] => 66

)[/CODE]

I want it to be

[CODE]Array
(
[0] => Array
(
[id] => 66
[parent] => 26

)
[0] => Array
(

[id] => 48
[parent] => 66

)

[1] => Array
(
[id] => 47
[parent] => 26

)[/CODE]

I am stuck with the following code.I have got the values but I don’t know how to match with another element in a array.

[code=php] foreach($firstlevel_results as $k => $v)
{
echo $v[‘id’].”==”.$v[‘parent’].”==”.$v[‘grandparent’].”<br>”;
}
[/code]

Please suggest if there is any possible way.Thanks in advance. ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmSep 28.2014 — I don't see how your example matches what you say you want.

Anyway - why not reduce the array that you create? For each parent you have only the child elements in the final result.

$parents = array(

'26'=>array('66','47'),

'66'=>array('48')

);
Copy linkTweet thisAlerts:
@raj_2006authorSep 28.2014 — I don't see how your example matches what you say you want.

Anyway - why not reduce the array that you create? For each parent you have only the child elements in the final result.

$parents = array(

'26'=>array('66','47'),

'66'=>array('48')

);[/QUOTE]


Sorry I didn't get you.How can I reduce the array.

I want to re arrange the array values depending on which parent is who.Kind of page tree.

I want the final array to be

[CODE]Array
(
[0] => Array
(
[id] => 66
[parent] => 26

)
[0] => Array
(
[id] => 48
[parent] => 66
)

[1] => Array
(
[id] => 47
[parent] => 26

)
)[/CODE]
Copy linkTweet thisAlerts:
@ginerjmSep 28.2014 — You posted the same silly example again. I don't see why those first two sub arrays belong in the same upper level array element.

How are you using this data? I see no meaningful grouping here. The only thing that makes sense is that you have pairs of 'id' and 'parent' values.

Good luck. Signing off.
Copy linkTweet thisAlerts:
@NogDogSep 29.2014 — Sounds like you're trying to do something like this, which is pretty clunky and not scalable:
[code=php]
<?php

$data = array(
array(
'id' => 66,
'parent' => 26
),
array(
'id' => 47,
'parent' => 26
),
array(
'id' => 48,
'parent' => 66
)
);

$byID = array();
foreach($data as $arr) {
$byID[$arr['id']] = $arr['parent'];
}
$result = array();
$children = array();
foreach($byID as $id => $parent) {
if(!isset($byID[$parent])) {
$result[] = array(
'id' => $id,
'parent' => $parent
);
}
else {
$children[$id] = $parent;
}
}
foreach($children as $id => $parent) {
foreach($result as &$arr) {
if($arr['id'] == $parent) {
$arr['children'][] = array(
'id' => $id,
'parent' => $parent
);
}
}
}
echo "<pre>".print_r($result, 1)."</pre>";
[/code]

Output:
<i>
</i>Array
(
[0] =&gt; Array
(
[id] =&gt; 66
[parent] =&gt; 26
[children] =&gt; Array
(
[0] =&gt; Array
(
[id] =&gt; 48
[parent] =&gt; 66
)

<i> </i> )

<i> </i> )

<i> </i>[1] =&gt; Array
<i> </i> (
<i> </i> [id] =&gt; 47
<i> </i> [parent] =&gt; 26
<i> </i> )

)

But I think you'd be better off in the long run with a structure that used the "id" as the array key (essentially a hash instead of an array), so that the structure was more like the following, more succinct and more accurately reflecting the structure:
[code=php]
$data = array(
26 => array(
66 => array(
48 => array()
),
47 => array()
)
);
[/code]
Copy linkTweet thisAlerts:
@mjdamatoSep 29.2014 — You should probably do this through your query. How many levels can the parent-child associations go?
×

Success!

Help @raj_2006 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...