By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

combining arrays into common subarrays

P: n/a
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:

$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)

$patharr Array
(
[0] =>
[1] =books
[3] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:

$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}

Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .

Oct 15 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
rodeored wrote:
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:

$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)

$patharr Array
(
[0] =>
[1] =books
[3] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:

$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}

Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .
<?php
$url = "http://example.com/some/long/path/name/to/test.asmx";
$ar1 = explode('/',$url);
array_shift($ar1); // http:/
array_shift($ar1); // /
array_shift($ar1); // example.com/
$rv = array(array_pop($ar1));
while (count($ar1)) {
$rv = array(array_pop($ar1) =$rv);
}
print_r($rv);
?>
php test.php
Array
(
[some] =Array
(
[long] =Array
(
[path] =Array
(
[name] =Array
(
[to] =Array
(
[0] =test.asmx
)

)

)

)

)

)
Oct 15 '08 #2

P: n/a
On Oct 15, 12:13*am, Kristof Benes <ch...@nospammaricopacomputer.com>
wrote:
rodeored wrote:
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. *Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:
$patharr Array
(
* * [0] =>
* * [1] =members
* * [2] =file_downloads
* * [3] =thisfile.asp
)
$patharr Array
(
* * [0] =>
* * [1] =books
* * [3] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
* * [/] =Array
* * (
* * * * * [books] =Array
* * * * * * * * (
* * * * * * * * * * [0] =thisotherfile.asp
* * * * * * * * )
* * * * * *[members] =Array
* * * * * * * * (
* * * * * * * * * * [filedownloads] =Array
* * * * * * * * * * (
* * * * * * * * * * * * [0] =thisfile.asp
* * * * * * * * * * )
* * * * * * * * )
* *)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:
$size=sizeof($patharr);
switch($size){
* *case 1:
* *break;
* *case 2:
* * * $allurls["/"][]=$thelink];
* *break;
* *case 3:
* * * $allurls["/"][$patharr[1]][]=$thelink;
* *break;
* *case 4:
* * * $allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
* *break;
}
Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .

<?php
$url = "http://example.com/some/long/path/name/to/test.asmx";
$ar1 = explode('/',$url);
array_shift($ar1); // http:/
array_shift($ar1); // /
array_shift($ar1); // example.com/
$rv = array(array_pop($ar1));
while (count($ar1)) {
* * $rv = array(array_pop($ar1) =$rv);}

print_r($rv);
?>

*php test.php
Array
(
* * *[some] =Array
* * * * *(
* * * * * * *[long] =Array
* * * * * * * * *(
* * * * * * * * * * *[path] =Array
* * * * * * * * * * * * *(
* * * * * * * * * * * * * * *[name] =Array
* * * * * * * * * * * * * * * * *(
* * * * * * * * * * * * * * * * * * *[to] =Array
* * * * * * * * * * * * * * * * * * * * *(
* * * * * * * * * * * * * * * * * * * * * * *[0] =test.asmx
* * * * * * * * * * * * * * * * * * * * *)

* * * * * * * * * * * * * * * * *)

* * * * * * * * * * * * *)

* * * * * * * * *)

* * * * *)

)
I don't see how to apply this to my situation.
For example, in my code I have
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;

which adds each link to the large array. If I use your code, it
collapses each url into an array, but it doesn't solve my main problem
which is how to combine the values in that array with the large array
Oct 15 '08 #3

P: n/a
rodeored wrote:
On Oct 15, 12:13 am, Kristof Benes <ch...@nospammaricopacomputer.com>
wrote:
>rodeored wrote:
>>I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:
$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)
$patharr Array
(
[0] =>
[1] =books
[3] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:
$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}
Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .
<?php
$url = "http://example.com/some/long/path/name/to/test.asmx";
$ar1 = explode('/',$url);
array_shift($ar1); // http:/
array_shift($ar1); // /
array_shift($ar1); // example.com/
$rv = array(array_pop($ar1));
while (count($ar1)) {
$rv = array(array_pop($ar1) =$rv);}

print_r($rv);
?>
> php test.php
Array
(
[some] =Array
(
[long] =Array
(
[path] =Array
(
[name] =Array
(
[to] =Array
(
[0] =test.asmx
)

)

)

)

)

)

I don't see how to apply this to my situation.
For example, in my code I have
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;

which adds each link to the large array. If I use your code, it
collapses each url into an array, but it doesn't solve my main problem
which is how to combine the values in that array with the large array
I'm not going to write your whole solution for you. I usually bill
about $150 an hour to do that. I'm just suggesting a way to avoid using
per-depth if statements. Consider using array_shift / pop / etc..

BTW love reenie.org
Oct 15 '08 #4

P: n/a
rodeored wrote:
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:

$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)

$patharr Array
(
[0] =>
[1] =books
[3] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:

$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}

Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .

Not too hard, but first of all a question: Is element [0] always going
to be an empty string? Or will it sometimes contain a value
(directory/file)?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 15 '08 #5

P: n/a
On Oct 15, 7:23*am, Jerry Stuckle <jstuck...@attglobal.netwrote:
rodeored wrote:
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. *Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:
$patharr Array
(
* * [0] =>
* * [1] =members
* * [2] =file_downloads
* * [3] =thisfile.asp
)
$patharr Array
(
* * [0] =>
* * [1] =books
* * [2] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
* * [/] =Array
* * (
* * * * * [books] =Array
* * * * * * * * (
* * * * * * * * * * [0] =thisotherfile.asp
* * * * * * * * )
* * * * * *[members] =Array
* * * * * * * * (
* * * * * * * * * * [filedownloads] =Array
* * * * * * * * * * (
* * * * * * * * * * * * [0] =thisfile.asp
* * * * * * * * * * )
* * * * * * * * )
* *)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:
$size=sizeof($patharr);
switch($size){
* *case 1:
* *break;
* *case 2:
* * * $allurls["/"][]=$thelink];
* *break;
* *case 3:
* * * $allurls["/"][$patharr[1]][]=$thelink;
* *break;
* *case 4:
* * * $allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
* *break;
}
Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .

Not too hard, but first of all a question: Is element [0] always going
to be an empty string? *Or will it sometimes contain a value
(directory/file)?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Its always empty. That array is the result of exploding a path of a
parsed url, such as "/dir/subdir/file.asp"
Oct 15 '08 #6

P: n/a
rodeored wrote:
On Oct 15, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>rodeored wrote:
>>I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:
$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)
$patharr Array
(
[0] =>
[1] =books
[2] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:
$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}
Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .
Not too hard, but first of all a question: Is element [0] always going
to be an empty string? Or will it sometimes contain a value
(directory/file)?

Its always empty. That array is the result of exploding a path of a
parsed url, such as "/dir/subdir/file.asp"
OK, so how about something like this? It uses a reference variable to
keep track of where you are in the array.

<?php

$allurls = array();
$patharr = explode('/', "/dir/subdir/file.asp");

$curdir = &$allurls; // Reference to the current directory
for ($i = 1; $i < count($patharr) - 1; $i++) {
if (!isset($curdir[$patharr[$i]]))
$curdir[$patharr[$i]] = array();
$curdir = &$curdir[$patharr[$i]]; // Update the reference
}
$curdir[] = $patharr[count($patharr) - 1];

print_r($patharr);
?>

Output:

Array
(
[dir] =Array
(
[subdir] =Array
(
[0] =file.asp
)
)
)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 15 '08 #7

P: n/a
Jerry Stuckle wrote:
rodeored wrote:
>On Oct 15, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>rodeored wrote:
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:
$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)
$patharr Array
(
[0] =>
[1] =books
[2] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:
$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}
Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .
Not too hard, but first of all a question: Is element [0] always going
to be an empty string? Or will it sometimes contain a value
(directory/file)?

Its always empty. That array is the result of exploding a path of a
parsed url, such as "/dir/subdir/file.asp"

OK, so how about something like this? It uses a reference variable to
keep track of where you are in the array.

<?php

$allurls = array();
$patharr = explode('/', "/dir/subdir/file.asp");

$curdir = &$allurls; // Reference to the current directory
for ($i = 1; $i < count($patharr) - 1; $i++) {
if (!isset($curdir[$patharr[$i]]))
$curdir[$patharr[$i]] = array();
$curdir = &$curdir[$patharr[$i]]; // Update the reference
}
$curdir[] = $patharr[count($patharr) - 1];

print_r($patharr);
Slight typo here, I believe you meant to use $allurls:

print_r($allurls);

for the below output. BTW, nice approach.
?>

Output:

Array
(
[dir] =Array
(
[subdir] =Array
(
[0] =file.asp
)
)
)
--
Curtis
Oct 20 '08 #8

P: n/a
Curtis wrote:
Jerry Stuckle wrote:
>rodeored wrote:
>>On Oct 15, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
rodeored wrote:
I don't know what the official programming lingo is for this situation
but there probably is one.
I have arrays, which happen to be parsed urls, and I want to make one
big array with each subarray in the big array representing one
directory. Sometimes the path has no sub directories, sometimes 1 or
2 etc . For example here are 2 example arrays:
$patharr Array
(
[0] =>
[1] =members
[2] =file_downloads
[3] =thisfile.asp
)
$patharr Array
(
[0] =>
[1] =books
[2] =thisotherfile.asp
)
What I want from this is an array that looks like this:
allurls Array
(
[/] =Array
(
[books] =Array
(
[0] =thisotherfile.asp
)
[members] =Array
(
[filedownloads] =Array
(
[0] =thisfile.asp
)
)
)
If I run this code for each array, it does what I want, but it is not
entirely dynamic because it only works if all cases/sizes are
accounted for:
$size=sizeof($patharr);
switch($size){
case 1:
break;
case 2:
$allurls["/"][]=$thelink];
break;
case 3:
$allurls["/"][$patharr[1]][]=$thelink;
break;
case 4:
$allurls["/"][$patharr[1]][$patharr[2]][]=$thelink;
break;
}
Is there a better way to do this, one that does not care how large the
arrays I am adding are ? .
Not too hard, but first of all a question: Is element [0] always going
to be an empty string? Or will it sometimes contain a value
(directory/file)?
Its always empty. That array is the result of exploding a path of a
parsed url, such as "/dir/subdir/file.asp"

OK, so how about something like this? It uses a reference variable to
keep track of where you are in the array.

<?php

$allurls = array();
$patharr = explode('/', "/dir/subdir/file.asp");

$curdir = &$allurls; // Reference to the current directory
for ($i = 1; $i < count($patharr) - 1; $i++) {
if (!isset($curdir[$patharr[$i]]))
$curdir[$patharr[$i]] = array();
$curdir = &$curdir[$patharr[$i]]; // Update the reference
}
$curdir[] = $patharr[count($patharr) - 1];

print_r($patharr);

Slight typo here, I believe you meant to use $allurls:

print_r($allurls);

for the below output. BTW, nice approach.
>?>

Output:

Array
(
[dir] =Array
(
[subdir] =Array
(
[0] =file.asp
)
)
)
Yes, you're correct. I had both in the original code (for debugging)
and deleted the wrong one!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 20 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.