Connecting Tech Pros Worldwide Forums | Help | Site Map

combining arrays into common subarrays

rodeored
Guest
 
Posts: n/a
#1: Oct 15 '08
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 ? .


Kristof Benes
Guest
 
Posts: n/a
#2: Oct 15 '08

re: combining arrays into common subarrays


rodeored wrote:
Quote:
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);
?>
Quote:
php test.php
Array
(
[some] =Array
(
[long] =Array
(
[path] =Array
(
[name] =Array
(
[to] =Array
(
[0] =test.asmx
)

)

)

)

)

)
rodeored
Guest
 
Posts: n/a
#3: Oct 15 '08

re: combining arrays into common subarrays


On Oct 15, 12:13*am, Kristof Benes <ch...@nospammaricopacomputer.com>
wrote:
Quote:
rodeored wrote:
Quote:
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:
>
Quote:
$patharr Array
(
* * [0] =>
* * [1] =members
* * [2] =file_downloads
* * [3] =thisfile.asp
)
>
Quote:
$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
* * * * * * * * * * )
* * * * * * * * )
* *)
>
Quote:
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:
>
Quote:
$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;
}
>
Quote:
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
Kristof Benes
Guest
 
Posts: n/a
#4: Oct 15 '08

re: combining arrays into common subarrays


rodeored wrote:
Quote:
On Oct 15, 12:13 am, Kristof Benes <ch...@nospammaricopacomputer.com>
wrote:
Quote:
>rodeored wrote:
Quote:
>>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);
>?>
>>
Quote:
> 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
Jerry Stuckle
Guest
 
Posts: n/a
#5: Oct 15 '08

re: combining arrays into common subarrays


rodeored wrote:
Quote:
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.
jstucklex@attglobal.net
==================

rodeored
Guest
 
Posts: n/a
#6: Oct 15 '08

re: combining arrays into common subarrays


On Oct 15, 7:23*am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
rodeored wrote:
Quote:
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:
>
Quote:
$patharr Array
(
* * [0] =>
* * [1] =members
* * [2] =file_downloads
* * [3] =thisfile.asp
)
>
Quote:
$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
* * * * * * * * * * )
* * * * * * * * )
* *)
>
Quote:
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:
>
Quote:
$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;
}
>
Quote:
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"
Jerry Stuckle
Guest
 
Posts: n/a
#7: Oct 15 '08

re: combining arrays into common subarrays


rodeored wrote:
Quote:
On Oct 15, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>rodeored wrote:
Quote:
>>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.
jstucklex@attglobal.net
==================

Curtis
Guest
 
Posts: n/a
#8: Oct 20 '08

re: combining arrays into common subarrays


Jerry Stuckle wrote:
Quote:
rodeored wrote:
Quote:
>On Oct 15, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Quote:
>>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.
Quote:
?>
>
Output:
>
Array
(
[dir] =Array
(
[subdir] =Array
(
[0] =file.asp
)
)
)
>
--
Curtis
Jerry Stuckle
Guest
 
Posts: n/a
#9: Oct 20 '08

re: combining arrays into common subarrays


Curtis wrote:
Quote:
Jerry Stuckle wrote:
Quote:
>rodeored wrote:
Quote:
>>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.
>
Quote:
>?>
>>
>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.
jstucklex@attglobal.net
==================

Closed Thread