473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multidimensional array - search for relationships

Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
[0] =Array
(
[0] =30
[1] =31
)

This states that value 30 is related to 31. I would like to search
through the entire multidimensional array (it could be empty or
contain hundreds of entries), to see if multiple relationships exist
and hence group these relationships into one entry. so for the array
below:

Array
(
[0] =Array
(
[0] =30
[1] =31
)

[1] =Array
(
[0] =29
[1] =26
)

[2] =Array
(
[0] =29
[1] =27
)

[3] =Array
(
[0] =29
[1] =28
)

[4] =Array
(
[0] =26
[1] =27
)

[5] =Array
(
[0] =27
[1] =28
)
)

This should be represented as:
Array
(
[0] =Array
(
[0] =30
[1] =31
)

[1] =Array
(
[0] =29
[1] =26
[2] =27
[3] =28
)
)

I am new enough to PHP so I would appreciate your help.
Thanks

Dec 2 '07 #1
5 2285
MW
LittleCake wrote:
Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
[0] =Array
(
[0] =30
[1] =31
)

This states that value 30 is related to 31. I would like to search
through the entire multidimensional array (it could be empty or
contain hundreds of entries), to see if multiple relationships exist
and hence group these relationships into one entry. so for the array
below:

Array
(
[0] =Array
(
[0] =30
[1] =31
)

[1] =Array
(
[0] =29
[1] =26
)

[2] =Array
(
[0] =29
[1] =27
)

[3] =Array
(
[0] =29
[1] =28
)

[4] =Array
(
[0] =26
[1] =27
)

[5] =Array
(
[0] =27
[1] =28
)
)

This should be represented as:
Array
(
[0] =Array
(
[0] =30
[1] =31
)

[1] =Array
(
[0] =29
[1] =26
[2] =27
[3] =28
)
)

I am new enough to PHP so I would appreciate your help.
Thanks
while not exactly your solution, the following code will parse the first
array to another array that looks like:

Array
(
[29] =Array
(
[0] =26
[1] =27
[2] =28
)

[30] =Array
(
[0] =31
)
)

i.e. the array index would be the index term, and the sub-array would
contain all the terms that are related to the index term.

$result_array=array();
foreach ($master_array as $sub_array)
$result_array[{$sub_array[0]}][]=$sub_array[1];
MW
Dec 2 '07 #2
Shane is given an edge array ($aEdges) and wants to
know the distinct connected components. This can be done
by my function connectedComponents($aEdges). The first
part walks each edge once to build an adjacency array.
The second part, which does the real work, picks an
unvisited edge and visits each vertex it can get to from
that selected vertex, processing each edge once, so that
the total running time is O(E). It'd be O(V+E) for the
second part, if singletons (isolated vertices) were
to have been allowed.

Csaba Gabor from Vienna
function connectedComponents ($aEdges) {
// given an edge array, $aEdges, this will return an array
// of connected components. Each element of the returned
// array, $aTrees, will correspond to one component and
// have an array of the vertices in that component.
$aAdj = array(); $aTree = array(); $ctr=-1;
foreach ($aEdges as $br) // Construct V/E adjacancy array
foreach ($br as $i=>$v) {
if (!array_key_exists($v,$aAdj)) $aAdj[$v]=array($br[1-$i]);
else array_push ($aAdj[$v], $br[1-$i]); }

foreach ($aAdj as $v =&$aTrees[++$ctr]) // Now build distinct
for ($i=0;$i<sizeof($aTrees[$ctr]);++$i) { // components
$aV = array_keys(array_flip(array_merge(
$aV = &$aTrees[$ctr], $aAdj[$aV[$i]])));
unset ($aAdj[$aV[$i]]); }

return $aTrees; }
//Example usage:
$aEdges = array(array (30, 31), array (29, 26),
array (29, 27), array (29, 28),
array (26, 27), array (27, 28));
$aComponents = connectedComponents ($aEdges);
//Example 2:
$aEdges = array(
array (a, b), array (b, d), array (b, e),
array (d, g), array (c, i), array (e, h),
array (h, j), array (c, f), array (f, i),
array (g, j), array (j, k), array(j, b));
$aComponents = connectedComponents ($aEdges);

On Dec 2, 6:33 pm, LittleCake <shaneda...@gmail.comwrote:
Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
[0] =Array
(
[0] =30
[1] =31
)

This states that value 30 is related to 31. I would like to search
through the entire multidimensional array (it could be empty or
contain hundreds of entries), to see if multiple relationships exist
and hence group these relationships into one entry. so for the array
below:

Array
(
[0] =Array
(
[0] =30
[1] =31
)

[1] =Array
(
[0] =29
[1] =26
)

[2] =Array
(
[0] =29
[1] =27
)

[3] =Array
(
[0] =29
[1] =28
)

[4] =Array
(
[0] =26
[1] =27
)

[5] =Array
(
[0] =27
[1] =28
)
)

This should be represented as:
Array
(
[0] =Array
(
[0] =30
[1] =31
)

[1] =Array
(
[0] =29
[1] =26
[2] =27
[3] =28
)
)

I am new enough to PHP so I would appreciate your help.
Thanks
Dec 2 '07 #3
Csaba Gabor wrote:
Shane is given an edge array ($aEdges) and wants to
know the distinct connected components. This can be done
by my function connectedComponents($aEdges). The first
part walks each edge once to build an adjacency array.
The second part, which does the real work, picks an
unvisited edge and visits each vertex it can get to from
that selected vertex, processing each edge once, so that
the total running time is O(E). It'd be O(V+E) for the
second part, if singletons (isolated vertices) were
to have been allowed.

Csaba Gabor from Vienna
function connectedComponents ($aEdges) {
// given an edge array, $aEdges, this will return an array
// of connected components. Each element of the returned
// array, $aTrees, will correspond to one component and
// have an array of the vertices in that component.
$aAdj = array(); $aTree = array(); $ctr=-1;
foreach ($aEdges as $br) // Construct V/E adjacancy array
foreach ($br as $i=>$v) {
if (!array_key_exists($v,$aAdj)) $aAdj[$v]=array($br[1-$i]);
else array_push ($aAdj[$v], $br[1-$i]); }

foreach ($aAdj as $v =&$aTrees[++$ctr]) // Now build distinct
for ($i=0;$i<sizeof($aTrees[$ctr]);++$i) { // components
$aV = array_keys(array_flip(array_merge(
$aV = &$aTrees[$ctr], $aAdj[$aV[$i]])));
unset ($aAdj[$aV[$i]]); }

return $aTrees; }
Whoops, small typo, though code works as is.
In the initialization it should be $aTrees = array();

Note to self: the determine distinct connected components
section also works with:

// determine connected components from adjacency matrix:
// $aAdj: [$v =array of vertices connected to $v, ...]
foreach ($aAdj as $v =&$aTrees[++$ctr]) // Now build distinct
for ($i=0;$i<sizeof($aV=&$aTrees[$ctr]);++$i) // components
unset ($aAdj[current (array_slice(
$aV = array_keys (array_flip (array_merge(
$aV, $aAdj[$aV[$i]]))), $i, 1))]);
Dec 3 '07 #4
On Dec 2, 9:24 pm, MW <m...@w.invalidwrote:
LittleCake wrote:
Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
[0] =Array
(
[0] =30
[1] =31
)
This states that value 30 is related to 31. I would like to search
through the entire multidimensional array (it could be empty or
contain hundreds of entries), to see if multiple relationships exist
and hence group these relationships into one entry. so for the array
below:
Array
(
[0] =Array
(
[0] =30
[1] =31
)
[1] =Array
(
[0] =29
[1] =26
)
[2] =Array
(
[0] =29
[1] =27
)
[3] =Array
(
[0] =29
[1] =28
)
[4] =Array
(
[0] =26
[1] =27
)
[5] =Array
(
[0] =27
[1] =28
)
)
This should be represented as:
Array
(
[0] =Array
(
[0] =30
[1] =31
)
[1] =Array
(
[0] =29
[1] =26
[2] =27
[3] =28
)
)
I am new enough to PHP so I would appreciate your help.
Thanks

while not exactly your solution, the following code will parse the first
array to another array that looks like:

Array
(
[29] =Array
(
[0] =26
[1] =27
[2] =28
)

[30] =Array
(
[0] =31
)
)

i.e. the array index would be the index term, and the sub-array would
contain all the terms that are related to the index term.

$result_array=array();
foreach ($master_array as $sub_array)
$result_array[{$sub_array[0]}][]=$sub_array[1];

MW
Thanks for your reply, it was much appreciated.
Keep the flag flying.
Shane
Dec 3 '07 #5
On Dec 3, 1:33 pm, Csaba Gabor <cs...@z6.comwrote:
Csaba Gabor wrote:
Shane is given an edge array ($aEdges) and wants to
know the distinct connected components. This can be done
by my function connectedComponents($aEdges). The first
part walks each edge once to build an adjacency array.
The second part, which does the real work, picks an
unvisited edge and visits each vertex it can get to from
that selected vertex, processing each edge once, so that
the total running time is O(E). It'd be O(V+E) for the
second part, if singletons (isolated vertices) were
to have been allowed.
Csaba Gabor from Vienna
function connectedComponents ($aEdges) {
// given an edge array, $aEdges, this will return an array
// of connected components. Each element of the returned
// array, $aTrees, will correspond to one component and
// have an array of the vertices in that component.
$aAdj = array(); $aTree = array(); $ctr=-1;
foreach ($aEdges as $br) // Construct V/E adjacancy array
foreach ($br as $i=>$v) {
if (!array_key_exists($v,$aAdj)) $aAdj[$v]=array($br[1-$i]);
else array_push ($aAdj[$v], $br[1-$i]); }
foreach ($aAdj as $v =&$aTrees[++$ctr]) // Now build distinct
for ($i=0;$i<sizeof($aTrees[$ctr]);++$i) { // components
$aV = array_keys(array_flip(array_merge(
$aV = &$aTrees[$ctr], $aAdj[$aV[$i]])));
unset ($aAdj[$aV[$i]]); }
return $aTrees; }

Whoops, small typo, though code works as is.
In the initialization it should be $aTrees = array();

Note to self: the determine distinct connected components
section also works with:

// determine connected components from adjacency matrix:
// $aAdj: [$v =array of vertices connected to $v, ...]
foreach ($aAdj as $v =&$aTrees[++$ctr]) // Now build distinct
for ($i=0;$i<sizeof($aV=&$aTrees[$ctr]);++$i) // components
unset ($aAdj[current (array_slice(
$aV = array_keys (array_flip (array_merge(
$aV, $aAdj[$aV[$i]]))), $i, 1))]);
Thanks for that. I was getting warning "The argument should be an
array", is it to do with the reference being unset?
All in all I have a better grasp now. Thanks again.
Shane
Dec 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
3
by: matthewburton | last post by:
Hi everyone, I'm trying to write a program that will search a body of text and replace certain words (say, A, B, and C) with different words that I think are more appropriate (A1, B1 and C1). I...
3
by: BobbyS | last post by:
I am trying to develop a multidimensional array for use of searching a very large database. I understand the concept of one and two dimensional arrays but this project would include up to 12 or 13...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
1
by: tarscher | last post by:
Hi all, 2 array questions; I have a multidimensional matrix. C1 C2 C3 C4 R1 R2 R3 R4 R5
1
by: kardon33 | last post by:
Good day all, First, I am rather new to C programming or at least don't use it enough to get used to it. My question, What part of my script needs to do is monitor a port and record the...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.