Arrays
Moderator: General Moderators
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Arrays
Ok, wow i've just started learning arrays. I found myself in a bind at work were i really need to write this script.
What i need to do and i need to know if it is possible and perhaps maby a little insite on how to accomplish these tasks...
1.) Lets say i have 3 words in an array. Seperated by . the words are one two and three. Is it possible to take the word two out and change it to the word six. While leaving it in the same order in the array. The same order would be "one six three" the wrong order would be "one three six"
2.) I need to write a script that displays all the words in an array. Basically a loop. How might i go about this.
BTW i didn't just come running here to ask hour. I've been reading about arrays for the past couple hours trying to figure something out. I even got a notepad and pen trying to figure something up in my head.
I'm sure it is something easy i just over looked.
Thank you for any help you can provide me.
- Anthony
What i need to do and i need to know if it is possible and perhaps maby a little insite on how to accomplish these tasks...
1.) Lets say i have 3 words in an array. Seperated by . the words are one two and three. Is it possible to take the word two out and change it to the word six. While leaving it in the same order in the array. The same order would be "one six three" the wrong order would be "one three six"
2.) I need to write a script that displays all the words in an array. Basically a loop. How might i go about this.
BTW i didn't just come running here to ask hour. I've been reading about arrays for the past couple hours trying to figure something out. I even got a notepad and pen trying to figure something up in my head.
I'm sure it is something easy i just over looked.
Thank you for any help you can provide me.
- Anthony
Please correct me if I'm wrong but from reading your post it looks as though you are thinking about arrays in the wrong way.
For example a basic array could be created like this...
...to change any of the array values you could do this...
...to display of all the array values using a loop you might use this...
You've probably seen this already but having a look at the PHP manual might give you some more ideas. [php_man]array[/php_man]
For example a basic array could be created like this...
Code: Select all
<?php
$fruit[0] = "Apple";
$fruit[1] = "Banana";
$fruit[2] = "Orange";
?>Code: Select all
<?php
// Change Banana to Lime
$fruit[1] = "Lime";
?>Code: Select all
<?php
foreach($fruit as $value)
{
echo $value."<br />";
}
?>-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
That makes a tid bit more since. Maby you could helpe me out here thought
<?php
foreach($fruit as $value)
{
echo $value."<br />";
}
?>
That wa your code. What does $value do? What is it? That is what is kinda getting me. I don't know why that is their.
Also basically what i'm doing. Is i have a photo album for my family. It allows 4 pictures and i'm storing the urls in arrays. in one sql field.
So i just needed to know how to update specific vars in an array but i think you explained that to me.
<?php
foreach($fruit as $value)
{
echo $value."<br />";
}
?>
That wa your code. What does $value do? What is it? That is what is kinda getting me. I don't know why that is their.
Also basically what i'm doing. Is i have a photo album for my family. It allows 4 pictures and i'm storing the urls in arrays. in one sql field.
So i just needed to know how to update specific vars in an array but i think you explained that to me.
Check-out the manual for [php_man]foreach[/php_man] but it's just a fancy while() or for() really.
You could just do the following to get the same result....
You could just do the following to get the same result....
Code: Select all
<?php
for($i=0; $i<count($fruit); $i++)
{
$value = $fruit[$i];
echo $value."<br />";
}
?>-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
OK, so basically i wrote something similar to an example on the manual
$a = array ("default.gif", "default.gif", "default.gif", "default.gif");
foreach ($a as $v) {
print "Current value of \$a: $v.\n";
}
However, what i need to do is i need to make each one of those defaults into a var. A var i can change and stuff.
Is it liek this..
$a[1];
$a[2];
and it will give me my info?
$a = array ("default.gif", "default.gif", "default.gif", "default.gif");
foreach ($a as $v) {
print "Current value of \$a: $v.\n";
}
However, what i need to do is i need to make each one of those defaults into a var. A var i can change and stuff.
Is it liek this..
$a[1];
$a[2];
and it will give me my info?
Yes, but remember that arrays are zero-based. Heres an example:
Code: Select all
<?php
$a = array ("1.gif", "2.gif", "3.gif", "4.gif");
echo $a[0]; // result: 1.gif
echo $a[1]; // result: 2.gif
?>-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Ok, i have in my db field the following contents.
default.gif, default.gif, default.gif.
What i want to do is i wanted to create a loop that will display these pictures. So i did the following code.
$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$arraypic = $row['pics'];
$pics = array($arraypic, ",");
foreach ($pics as $picid) {
print "Print html for $picid</p>";
}
yet it does not print the defaults in the db.. Any ideas?
default.gif, default.gif, default.gif.
What i want to do is i wanted to create a loop that will display these pictures. So i did the following code.
$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$arraypic = $row['pics'];
$pics = array($arraypic, ",");
foreach ($pics as $picid) {
print "Print html for $picid</p>";
}
yet it does not print the defaults in the db.. Any ideas?
This might work...
Code: Select all
<?php
$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());
while($arr = mysql_fetch_array($result))
{
$pics[] = $arr["pics"];
}
foreach($pics as $picid)
{
echo "Print HTML for {$picid}<br />";
}
?>-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Wow, thank you again so much for your help. Just one quick problem.
$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());
while($arr = mysql_fetch_array($result))
{
$pics[] = $arr["add_picts"];
}
foreach($pics as $picid)
{
echo "Print HTML for {$picid}</p>";
echo "</p>";
}
Thats the code.. it does not </p> at the end of default.php instead it just does this
Print HTML for default.gif,default.gif,default.gif,default.gif
I seperated the default.gif's in the db feild with ,'s
THank you
$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());
while($arr = mysql_fetch_array($result))
{
$pics[] = $arr["add_picts"];
}
foreach($pics as $picid)
{
echo "Print HTML for {$picid}</p>";
echo "</p>";
}
Thats the code.. it does not </p> at the end of default.php instead it just does this
Print HTML for default.gif,default.gif,default.gif,default.gif
I seperated the default.gif's in the db feild with ,'s
THank you
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
No you shouldn't need to do that. Run the code again and have a look at the HTML source-code once the page has loaded. You should get some idea of why it's not being displayed correctly (look for missing tags etc).pinehead18 wrote:well i changed the </p> to a <br> yet, it still does not work.
Don't i need to like explode these vars from the array and put them back into the array or something?
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm