...

View Full Version : Array Questions



ptmuldoon
08-01-2008, 10:41 PM
Getting back into learning more of php, and have two questions regarding arrays. I have the following query and code to pull some data out of a database.


$query = mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
$towns[] = $row;
}

echo "<pre>";
echo print_r($towns);
echo "</pre>";


And the above outputs the below:


Array
(
[0] => Array
(
[id] => 1
[owner] => 1
[name] => Zion
)

[1] => Array
(
[id] => 2
[owner] => 2
[name] => my town
)

[2] => Array
(
[id] => 3
[owner] => 2
[name] => testing town
)

)
1

My current questions are:

a. How can I have array start at 1 and not 0.
b. I'm unsure why I keep getting a value of 1 at the end of the array.

Can anyone help?
Thanks,
PT

oesxyl
08-01-2008, 11:02 PM
Getting back into learning more of php, and have two questions regarding arrays. I have the following query and code to pull some data out of a database.


$query = mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
$towns[] = $row;
}

echo "<pre>";
echo print_r($towns);
echo "</pre>";


And the above outputs the below:


Array
(
[0] => Array
(
[id] => 1
[owner] => 1
[name] => Zion
)

[1] => Array
(
[id] => 2
[owner] => 2
[name] => my town
)

[2] => Array
(
[id] => 3
[owner] => 2
[name] => testing town
)

)
1

My current questions are:

a. How can I have array start at 1 and not 0.
assign your own key:


$query = mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
$i = 1;
while($row = mysql_fetch_assoc($query)){
$towns[$i] = $row;
}

echo "<pre>";
echo print_r($towns);
echo "</pre>";




b. I'm unsure why I keep getting a value of 1 at the end of the array.

because you print the result returned by print_r.


$presult = print_r($towns);
echo $presult;

print_r don't need echo, it is a kind of 'echo'

regards

ptmuldoon
08-02-2008, 01:35 AM
assign your own key:


$query = mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
$i = 1;
while($row = mysql_fetch_assoc($query)){
$towns[$i] = $row;
}

echo "<pre>";
print_r($towns);
echo "</pre>";



Thanks for the help. But assigning a key like mentioned above will only give me the second set of values in the multi-array, and not all 3 items. I'm not sure, but perhaps I can't do this with a while loop, but may need to use a for or foreach loop?

oesxyl
08-02-2008, 01:53 AM
Thanks for the help. But assigning a key like mentioned above will only give me the second set of values in the multi-array, and not all 3 items. I'm not sure, but perhaps I can't do this with a while loop, but may need to use a for or foreach loop?


$query = mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
$i = 1;
while($row = mysql_fetch_assoc($query)){
$towns[$i] = $row;
$i++; // my fault, this was missing
}

echo "<pre>";
print_r($towns);
echo "</pre>";


regards

ptmuldoon
08-02-2008, 03:04 PM
Thanks for the help, and along the same path as my earlier question. After I combine 2 array's using array_merge, how would have the array start at 1 again, instead of 0? So I have:

$towns = array_merge($default, $subchilds);

And it outputs starting with [0], when I need to start at [1] for the a menu function I'm working with.


Array
(
[0] => Array
(
[text] => Articles
[class] => articles
[link] => #
[show_condition] => 1
[parent] => 0
)

[1] => Array
(
[text] => Users
[class] => users
[link] => #
[show_condition] => 1
[parent] => 0
)

[2] => Array
(
[text] => Groups
[class] => groups
[link] => #
[show_condition] => 1
[parent] => 0
)

....... Down to array 10 plus
)

infinivert
08-02-2008, 03:15 PM
Just out of curiosity, why do you need to start your array at 1? I usually find it easier to just remember to call $array[$i-1] (or something similar) if my count starts at 1, rather than trying to mess with array keys.

--Josh

ptmuldoon
08-02-2008, 03:37 PM
Well, I'm working with a dynamic drop down menu from here:

http://www.roscripts.com/Building_a_dynamic_drop_down_menu-216.html

And the code used in the menu creation has arrays starting at 1. I took a try at modifying the code to accept an array starting at 0, but can't seem to get it to work.

And I'm basically working with the menu, pulling data from a database to create the array and menus.

So if I could get the array to start with 1 after the array_merge, (merging the main and submenus) I think I'll pretty close to accomplishing the menu to work the way I would like with the proper submenu's pulled from the db.

ptmuldoon
08-02-2008, 04:50 PM
I usually find it easier to just remember to call $array[$i-1] (or something similar) if my count starts at 1, rather than trying to mess with array keys.

--Josh

Josh. That got me thinking, and perhaps this is not the best way, but after merging the arrays, I simply made a new array based on the merged one shifting key + 1. Not sure if the below is the best method, but this is what I did (FYI. Its still a work in progress as I get the correct information pulled into the array):


//Build Submenu Query for User Towns
$query = mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
$i = 1;
while($row = mysql_fetch_assoc($query)){
$row['text'] = $row['name'];
$row['class'] = 'articles';
$row['link'] = 'towns.php&town='.$row['id'];
$row['show_condition'] = True;
$row['parent'] = 3;
$submenu[] = $row;
$i++;
}

$towns = array_merge($default, $submenu);
$count = count($towns);
$x = 1;
while($x <= $count){
$menu[$x] = $towns[$x-1];
$x++;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum