Jump to content


- - - - -

[PHP] - [Array Functions] - [ltjfansite]


  • Please log in to reply
No replies to this topic

#1 _*ltjfansite_*

_*ltjfansite_*
  • Guests

Posted 05 September 2004 - 11:22 AM

This Tut Was Writen By Freendom Of Zanmato Design and i had permision to post this tut.

You can find the ut on this page here
Click here

-------------------------------------

Explode can be used to separate data in a string or something. Say $var has a value of Hi|Hello|Hey, and you want to separate it into Hi, Hello, and Hey. This is what it will look like:

<?php
$var = 'Hi|Hello|Hey'; // This is what you want to separate

$var = explode("|",$var);
?>

Now it has been assigned to $var as an array. You can access each using $var[0], $var[1], and $var[2].

If you want to echo each out in a certain way, you can use foreach(). You can get the total number of them by using count.

All the other directions are in the script:

<?php
$var = 'Hi|Hello|Hey'; // This is what you want to separate

$var = explode("|",$var);

// You use count($var); to count how many records are in an array.

$total_heys = count($var);

# Print the total number of ways to say hi that are in the array:
echo "There are ".$total_heys." ways to say hello in this array!";

//Now that you have the total amount of ways to say hello, you need to decrease it once, since in the array it starts at "0". It needs to be one less to get the comma's set right.

$total_heys = $total_heys - 1;

echo " They are:<br />";
foreach($var as $key => $value){

// The following will echo the space and the comma
if($key!=$total_heys) { // $key starts at zero. So since $total_heys as already been decreased once, this will tell if it is the last record in the array.

$spacer = ", ";

} else { // If it IS the last in the array, $spacer is given no value.
$spacer = "";
}

// Now print out the value and the spacer.
echo "" . $value . $spacer ."";
}

?>

You can do it much faster using implode also:
<?php
$var = "Hi|Hello|Hey";
$var = explode("|",$var);
$total_heys = count($var); // Count them...
echo "There are " . $total_heys . " ways to say hello in this array! They are:<br />".implode(", ",$var);

?>
The same script is here:
http://virst.net/phptest.php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users