looping through many sequentially named arrays ($v1-$v1574)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dkblb
Forum Newbie
Posts: 8
Joined: Wed Apr 15, 2009 3:30 pm

looping through many sequentially named arrays ($v1-$v1574)

Post by dkblb »

I have a problem with a piece of code which creates sequentially numbered arrays named from $v1 to $v1574 containing one vector each (initially). I can access and print the arrays if I call them by name but I need to be able to loop through the vectors in the next portion of the program and merge arrays based on a previously calculated distance until I have the 1574 arrays conbined into 10 clusters (arrays). I have tried many versions of adding the $.v.sequential number but none have recalled the contents of the array although the version below echo's "array" as if I had left off the [0]. Help.

$result = mysql_query ("Select * From selectedvectors ");
if ($row = mysql_fetch_array($result)) {

do {
$numcount = $numcount + 1;
$vector = $row["vectors"];
$clusterno = "v".$numcount;
$$clusterno = array($vector);
$v = "v".$numcount; //these two lines try to find a way to access the arrays created above
echo $$$v[0]; //which range from $v1 to $v1574 (these number of vectors in the DB
//echo $v1[0]; //I know the arrays exist because they echo correctly when called by name
echo "\n"; //as in these two echo's
//echo $v1574[0]; // I have tested all the way to the last array
echo "\n";
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by requinix »

Why aren't you using arrays?

Code: Select all

$result = mysql_query ("Select * From selectedvectors ");
if ($row = mysql_fetch_array($result)) {
 
    $v = array();
    do {
        $numcount = $numcount + 1;
        $vector = $row["vectors"];
        $v[$numcount] = $vector;
    }
 
    echo $v[1], "\n"; // first
    echo $v[1574], "\n"; // supposedly the last
dkblb
Forum Newbie
Posts: 8
Joined: Wed Apr 15, 2009 3:30 pm

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by dkblb »

I am using arrays; each of the $v1 through $v1574 is an array. The contents of these arrays will be combined until there are only 10 arrays which will contain all 1574 vectors. And I know there exist these 1574 arrays each with one vector as I can print the vectors if I call them by name ie echo $v1400; for example. But I need to be able to increment and loop through them $v1 then $v2 etc by combining $v with the incremented number.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by requinix »

dkblb wrote:But I need to be able to increment and loop through them $v1 then $v2 etc by combining $v with the incremented number.
Yes, but why do you think that's the right way to do it? See how much trouble it's creating for you?

Just use one variable. Make it an array that has all 1574 items inside:

Code: Select all

Array
(
    [1] => whatever $v1 contains,
    [2] => whatever $v2 contains,
    [3] => whatever $v3 contains,
    ...
)
dkblb
Forum Newbie
Posts: 8
Joined: Wed Apr 15, 2009 3:30 pm

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by dkblb »

I don't want a single array with 1574 values there is no way to merge them based on distance if they are already in one array. The arrays must start with a single value in each array the arrays will be combined based on an algorithm until 10 remain with values the others will be empty and be destroyed.
dkblb
Forum Newbie
Posts: 8
Joined: Wed Apr 15, 2009 3:30 pm

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by dkblb »

By the way I know they are arrays that exist because I have done an array_merge of $v1 and $v2 and echoed the results which were as they were supposed to be as I got two 18 digit vectors.

$v1 = array_merge($v1,$v2);
echo $v1[0]; echo $v1[1];
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by Benjamin »

Your approach is wrong. Perhaps if you give a high level overview of what data you have, what you need to calculate, and what format you need the result in, we will be able to provide you with the correct solution.
dkblb
Forum Newbie
Posts: 8
Joined: Wed Apr 15, 2009 3:30 pm

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by dkblb »

I found the answer at http://www.webmasterworld.com/forum88/6949.htm which makes my code :
//so code pertaining to the query etc above
if ($row = mysql_fetch_array($result)) {

do {
$numcount = $numcount + 1;
$vector = $row["vectors"];
$clusterno = "v".$numcount;
$$clusterno = array($vector);
$v = "v".$numcount;
echo ${"$v"}[0];
echo "\n";
echo "\n";
echo $numcount;
echo "\n";
$v1 = array_merge($v1,${"$v"});
echo $v1[0]; echo $v1[1];

notice I can not only print but merger these arrays, here is the end of the output of the above code:

0000000000000000012

0000000000000000011

0000000000000000010

0000000000000000004

0000000000000000003

0000000000000000002

0000000000000000001

0000000000000000000

1574
100001000c0000000000000000000000000000

The code will proceed from here to merge arrays with there closest neighbors as calculated in a previous program with the cosine method for vectors. The array_merge here has no point other than a proof of concept as this is what I really wanted to do as printing the vectors was only to prove I could access them. I hope this is of use to someone.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: looping through many sequentially named arrays ($v1-$v1574)

Post by Benjamin »

Are you calculating distance? You can do that in MySQL queries. Just FYI.
Post Reply