Saturday, January 1, 2011

php - working with array

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title>Untitled</title>
</head>
<body>


<?php

//
// print_r()

// use it to debug array
//


echo '<i>using print_r() to debug anime list.</i><br />';
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
    'high shool'=>'bakuman','monster'=>'claymore');
print_r($animeGroupList);
echo '<br />';
$animeList=array('naruto','high school of dead','bakuman','claymore');
print_r($animeList);
echo '<br />';
echo '<br />';


//
// array_push()

// add an element to the end
//
// note: does not work with associative arrays
//


$anime='bleach';
echo "<i>using array_push(), added $anime at the end.</i><br />";
array_push($animeList,'bleach');
print_r($animeList);
echo '<br />';
echo '<br />';


//
// array_pop()

// remove an element from the end,
// and returns it
//


$anime=array_pop($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_pop(), $anime was removed from the end of anime list.</i>";
echo '<br />';
echo '<br />';


//
// array_shift()

// take an element off the beginning
//


$anime=array_shift($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_shift(), removing $anime the beginning.</i>";
echo '<br />';
echo '<br />';


//
// array_unshift()

// add an element to the beginning
//
// note: does not work with associative arrays
// note: returns the shifted value - NOT the element!
//


$anime=array_unshift($animeList, 'naruto');
print_r($animeList);
echo '<br />';
echo "<i>using array_unshift(), added $anime to beginning.</i><br />";
echo '<br />';
echo '<br />';


//
// explode()

// using a deliminater, splits a string into array elements
//


echo "using explode()<br />";
$str = 'shinigami, zombie, ninja';
echo "$str <br />";
$charactorTypeList = explode(', ', $str);
print_r($charactorTypeList);
echo '<br />';
echo '<br />';


//
// inplode()

// opposite of explode()
//

echo "using implode(), putting it back to a string<br />";
$str=implode(', ', $charactorTypeList);
echo $str;
echo '<br />';
echo '<br />';


//
// sort() and rsort()

//

echo 'original list';echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';

sort($charactorTypeList);
echo 'after sort()';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';

rsort($charactorTypeList);
echo 'after rsort() ';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';


//
// getting elements out of array
//


$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
for ($x = 0; $x < sizeof($artists); $x++){
    echo "<li> $artists[$x]";
 // echo '<li>'.$artists[$x]; //this works too
}
print "<br />";
print "<br />";


//
// array_key()

// array_values()
// for associative array
//


echo 'print keys and values of array of:<br />';
echo "'ninja'=>'naruto','zonbie'=>'high school of dead',
    'high shool'=>'bakuman','monster'=>'claymore'";
print "<br />";
print "<br />";  
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
    'high shool'=>'bakuman','monster'=>'claymore');

$result = array_keys($animeGroupList);
print_r($result);
print "<br />";

$result = array_values($animeGroupList);
print_r($result);
print "<br />";
print "<br />";


//
// foreach()

//

echo 'the list of artists: ';
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
foreach($artists as $a){
    echo '<li>'.$a;
}


?>
</body>
</html>

No comments:

Post a Comment