php -q filename.php
<?php
$c = json_decode(file_get_contents('/config/some.json'), true);
$endpoints = $c['uploads']['endpoints'];
//print var_dump($endpoints);
foreach ($endpoints as $key => $value) {
print $key . " : " . $value . "\n";
}
An array is a way of storing a group of data in the same variable.
It is an accepted convention that array indexes start with the number
zero, so the "zero" address is actually the first item in the array.
ASSOCIATIVE ARRAYS
$testarray[] = array( "name" => "apple" , "size" => '0' );
$testarray[] = array( "name" => "orange" , "size" => '17' );
$testarray['random_key'] = "random value";
if( !empty( $testarray ) )
{
print "testing:";
print "<br /> \n" ;
$tempname = $testarray[ "name" ]; //somehow it assumes we use $testarray[0]["name"], but we should be explicit!
print "$tempname " + $testarray[ "size" ] ;
print "<br /> \n" ;
print "$testarray[1]['name'] " + $testarray[1][ "size" ] ;
print "<br /> \n" ;
<?php
// create our arrays
$my_array_sub1 = array ( 'a', 'b', 'c' );
$my_array_sub2 = array ( 'x', 'y', '42' );
// append one array to the other, numeric keys renumbered!
$my_array = array_merge( $my_array_sub1 , $my_array_sub2 );
$my_array[] = 'm'; //pushes an item to the end of the array
// * array_shift() - Shift an element off the beginning of array
// array_unshift($queue, "new items", "to the front");
// * array_push() - Push one or more elements onto the end of array
// * array_pop() - Pop the element off the end of array
echo sizeof( $my_array ); //how many elements
echo "\n<br />\n";
print_r( $my_array ); //display the full contents of the array
echo "\n<br />\n";
unset( $my_array[ 3 ] ); // This removes the element from the array
print_r( $my_array ); // note that it does not update the indices
echo "\n<br />\n";
$my_array= array_values($my_array); //re-indexes the array items down
print_r( $my_array ); // note that it does not update the indices
echo "\n<br />\n";
unset( $my_array ); // This deletes the whole array
print_r( $my_array );
?>
As you can see, the array indices are printed when the array data is dumped:
7
Array ( [0] => a [1] => b [2] => c [3] => x [4] => y [5] => 42 [6] => m )
Array ( [0] => a [1] => b [2] => c [4] => y [5] => 42 [6] => m )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
In C a "string" is an array of characters with a termination character '\0' .
In PHP we get to ignore how the array is implemented, and there are lots
of built in functions to manipulate arrays that are strings of characters.
array split ( string $pattern , string $string [, int $limit ] )
Splits a string into array by regular expression (a powerful way of
comparing if a set of data matches a set of rules).
<?php
list($user, $pass, $uid, $gid, $extra) =
split(":", $passwd_line, 5);
?>
If you don't require the power of regular expressions, it is faster to use explode(),
which doesn't incur the overhead of the regular expression engine.
$subarray = explode("</cite>", $array[10]);
###########################################################################
ARRAY FUNCTIONS http://uk2.php.net/manual/en/ref.array.php
SEARCHING IN STRINGS
$pos = strpos( $result_array[$i], $target_url);
if( $pos === false ) //requires a funny test!
{ echo "not found"; }
else
{ echo "found"; }
strrpos() - Find position of last occurrence of a char in a string
stripos() - Find position of first occurrence of a case-insensitive string
strrchr() - Find the last occurrence of a character in a string
stristr() - Case-insensitive strstr
strstr() - Find first occurrence of a string
pregsplit is a faster reg exp search
explode just splits by a string (fastest)
strtok?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$arr = $arr1 + $arr2 //union of two arrays
TWO DIMENSIONAL ARRAY (associative)
Two or many more multi dimension arrays allow you to store lots of data in a tree
e.g. wideDataTree[4][3][2]
[0] [1] [2] [3]
[0] [1] [2] [0] [1] [2] [0] [1] [2] [0] [1] [2]
[0][1] [0][1] [0][1] [0][1] [0][1] [0][1] [0][1] [0][1] [0][1] [0][1] [0][1] [0][1]
a b c d x y j k i n t h 77 i 12 e etc.
An associative array is a way of using non numeric indices through some hash/map
to organize data. A KEY is the term given to a non numeric index of an array,
in this case we have keys "somearray" and "somearray2"...
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6]; // 5
echo "\n<br />\n";
echo $arr["somearray"][13]; // 9
echo "\n<br />\n";
echo $arr["somearray"]["a"]; // 42
echo "\n<br />\n";
print_r( $arr );
echo "\n<br />\n";
$somearray2 = array( "somearray2" ) ;
array_push( $arr , $somearray2 ); //assigned a default numeric index
print_r( $arr );
echo "\n<br />\n";
if( isset( $arr["somearray"][6] ) )
{ echo "OK"; } //if element exists
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE! SYNTAX!
$foo['bar'] is correct, while $foo[bar] is wrong!
BUT do not quote keys which are constants or variables
$arr[$i] is correct, $arr['$i'] is wrong!
$my_array = array ( 'a', 'b', 'c' );
$my_array[] = 'm';
$keys = array ( 'aqua', 'blue', 'cherry' );
$i = 0;
foreach($my_array as $value )
{
$newarray[ $value ] = $keys[ $i ];
$i++;
}
print_r( $newarray );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FOR EACH CAN BE MORE TROUBLE THAN IT'S WORTH...
just like every while statement can be a for loop and vice versa
$my_array = array ( 'a', 'b', 'c' );
$my_array[] = 'm';
$keys = array ( 'aqua', 'blue', 'cherry' );
$i = 0;
$k = 0;
for( $i =0 ; $i < count( $my_array ); $i++ )
{
$newarray[ $i ][ 0 ] = $my_array[ $i ];
$newarray[ $i ][ 1 ] = $keys[ $i ];
}
print "<pre>";
print_r( $newarray );
print "</pre>";
print "\n<br />";
print sizeof( $newarray );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
switch($n)
{
case "test1":
break;
default:
break;
}
for HTML forms
test if a field has data (you need both!)
if (!isset($_POST['firstname']) || empty($_POST['firstname']))
{ $errmsg .= "<p>Please enter your first name"; }