Re: fmouse,
the phenomenom you're describing pertains to superglobals, not arrays, and it only applies in a very specific scope. Check out http://de.php.net/manual/en/language.variables.variable.php :
"Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically."
配列
PHP の配列は、実際には順番付けられたマップです。マップは型の一種で、 値をキーに関連付けます。 この型は、いくつかの手法で最適化されます。このため、 実際の配列またはリスト (ベクトル)、(あるマップの実装である) ハッシュテーブル、ディレクトリ、コレクション、スタック、 キュー等として使用することが可能です。 PHP の配列には他の PHP 配列を値として保持することができるため、 非常に簡単にツリー構造を表現することが可能です。
これらのデータ構造に関する説明は本マニュアルの範囲外ですが、 これらの構造に各々に関する例を少なくとも一つ見付けることが可能です。 この分野は広範囲にまたがるので、 より詳細な情報については他の書籍を参照ください。
構文
array() で指定
配列 は、言語に組み込まれた array() で作成することが可能です。この構造は、 特定の数のカンマで区切られた key => value の組を引数とします。
array( key => value , ... ) // key は、文字列または // 非負の整数です。 // value に制約はありません.
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
key は、整数 または 文字列です。 あるキーが、整数の標準的な表現形式である場合、 そのように解釈されます (つまり、"8" は 8 として解釈されます。一方、 "08" は "08" として解釈されます)。 key に浮動小数点数値を指定すると、 その値は integer に切り詰められます。 PHP においては添字配列と連想配列の間に違いはなく、配列型は 1 つだけで、 整数または文字列のインデックスを使用することができます。
値には、PHP の全ての型を使用することができます。
<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
キーを省略した場合、整数添字の最大値が使用され、 新しいキーはその最大値 +1 となります。整数値は負の数とすることができ、 負の添字についても同様となります。例えば、最高時の添字が -6 の場合、次のキーは -5 となります。 整数添字がまだ存在しない場合、キーは 0 (ゼロ) となります。 値が既に代入されているキーを指定した場合、元の値は上書きされます。
<?php
// この配列は以下の配列と同じです ...
array(5 => 43, 32, 56, "b" => 12);
// この配列は上の配列と同じです
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
PHP 4.3.0 以降、上記のような添字生成動作は変更されました。 現在では、配列に追加する際に、その配列の最大添字が負である場合は 次の添字はゼロ (0) となります。 以前は、正の添字の場合と同様に新しい添字は最大添字に +1 したものにセットされていました。
キーとして TRUE を使用した場合、 整数型の 1 がキーとして解釈されます。 キーとして FALSE を使用した場合、 整数型の 0 がキーとして解釈されます。 キーとして NULL を使用した場合、 空の文字列として評価されます。キーとして空の文字列を使用すると、 空の文字列のキーとその値を作成 (または上書き) します。 空の括弧を用いた場合と同じではありません。
配列またはオブジェクトをキーとして使用することはできません。 これを行なうと、warning: Illegal offset type が発生します。
角括弧構文で作成/修正
明示的に値を設定することにより、既存の配列を修正することも可能です。
これは、角括弧の中にキーを指定し、配列に値を代入することにより行います。 キーを省略することも可能です。この場合、空の角括弧 ("[]") の変数名として追加してください。
$arr[key] = value; $arr[] = value; // key は 文字列 または // 非負の整数のどちらかです。 // value は何でもかまいません
$arr がまだ存在しない場合、作成されます。 配列を指定する別の手段でもあります。ある値を変更するには、 新しい値に値を代入します。特定のキー/値の組を削除したい場合には、 unset() を使用する必要があります。
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // このスクリプトのこの位置に記述した場合、
// $arr[13] = 56; と同じです
$arr["x"] = 42; // キー"x"の新しい要素を配列に追加します
unset($arr[5]); // 配列から要素を削除します
unset($arr); // 配列全体を削除します
?>
注意: 上記のように、キーを省略して新規要素を追加する場合、 追加される数値添字は、使用されている添字の最大値 +1 になります。 既に値が割り当てられているキーを指定した場合には、 値は上書きされます。
警告次のキー生成において、オフセットとして使われる整数値 (添字の最大値) に対応するエントリーが、 必ずしも配列内に存在するわけではないことに注意してください。 しかし、その値は、多くの場合、 配列にある整数のキー値の最大値と等しいはずです。以下に例を示します。PHP 4.3.0 以降、上記のような添字生成動作は変更されました。 現在では、配列に追加する際に、 その配列の最大添字が負である場合は次の添え字はゼロ (0) となります。 以前は、正の添字の場合と同様に新しい添字は最大添字に +1 したものがセットされました。
<?php
// 簡単な配列を生成します。
$array = array(1, 2, 3, 4, 5);
print_r($array);
// 全てのアイテムを削除しますが、配列自体は削除しないでおきます。
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
// アイテムを追加します(新しい添え字は0ではなく
// 5となることに注意)
$array[] = 6;
print_r($array);
// 添え字を振りなおします。
$array = array_values($array);
$array[] = 7;
print_r($array);
?>上の例の出力は以下となります。
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 )
有用な関数
配列で使用する便利な関数がたくさんあります。 配列関数 の節を参照ください。
注意: unset()関数は配列のキーを削除することが出来ます。 ただし、これによってインデックスの再構築が行われるわけではないことに 注意してください。 "通常の整数添字" (0 から始まり、1 つずつ増加) のみを使用している場合、 array_values() を用いてインデックスを再構築することができます。
<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* これにより配列は以下の様に定義されます。
$a = array(1 => 'one', 3 => 'three');
以下ではありません:
$a = array(1 => 'one', 2 =>'three');
*/
$b = array_values($a);
// $b は、array(0 => 'one', 1 =>'three')となります
?>
配列専用の制御構造としてforeach があります。 この構造は、配列の要素に簡単に連続的にアクセスする手段を提供します。
配列ですべきこととしてはならないこと
なぜ、$foo[bar] は使用できないのか?
連想配列の添字の前後は常に引用符で括る必要があります。 例えば、$foo[bar] ではなく $foo['bar'] を使用してください。 しかし、$foo[bar] はなぜ誤りなのでしょうか? 古いスクリプトで次のような構文を見たことがあるかもしれません。
<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
これは間違っていますが、動作します。では、なぜ間違っているのでしょう? その理由は、このコードには文字列 ('bar' - 引用符で括られている) ではなく未定義の定数 (bar) が使用されており、PHP が同じ名前の定数を不幸にして同じコードの中に定義する可能性があるためです。 下位互換性の維持のため、未定義の定数は同じ名前の文字列に自動的に変換されます。 そのため、このコードは動作します。 例えば、bar という名前の定義されていない定数があるとすると、 PHP は 'bar' という文字列でそれを置換して使用します。
注意: これは、添字を常にクォートするという意味ではありません。 定数や 変数 を添字として使う際には、クォートしてしまうと PHP はそれを解釈できなくなってしまいます。
上の例の出力は以下となります。<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// 単純な配列
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo "\nChecking $i: \n";
echo "Bad: " . $array['$i'] . "\n";
echo "Good: " . $array[$i] . "\n";
echo "Bad: {$array['$i']}\n";
echo "Good: {$array[$i]}\n";
}
?>
Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 1 Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 2
この具体例を以下に示します。
<?php
// エラーを全て表示するよう設定
error_reporting(E_ALL);
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
// 正しい
print $arr['fruit']; // apple
print $arr['veggie']; // carrot
// 間違い。これは動作しますが、未定義の定数fruitを使用しているため、
// 同時にE_NOTICEレベルのPHPエラーを発生します
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple
// 検証のため、定数を定義してみましょう。
// fruitという名前の定数に値'veggie'を代入します。
define('fruit', 'veggie');
// ここでは、出力が異なることに注意してください。
print $arr['fruit']; // apple
print $arr[fruit]; // carrot
// 以下は文字列の中であるためOKです。定数は、文字列の中では解釈されな
// いため、E_NOTICEエラーはここでは発生しません。
print "Hello $arr[fruit]"; // Hello apple
// 例外が1つあり、文字列の中で波括弧で配列を括った場合には、
// 定数が解釈されます
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// これは動作せず、以下のようなパースエラーを発生します:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// 文字列の中でスーパーグローバルを使用した場合も無論同様です。
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// 文字列結合で同じことをすることもできます。
print "Hello " . $arr['fruit']; // Hello apple
?>
error_reporting() で (E_ALL を指定する等により) E_NOTICE レベルのエラー出力を有効にした場合、 上記のエラーが出力されます。 デフォルトでは、 error_reporting はこれらを表示しない設定になっています。
構文の節に記述したように、角括弧 ('[' および ']') の間には、式がなければなりません。これは、 次のように書くことが可能であることを意味します。
<?php
echo $arr[somefunc($bar)];
?>
これは、関数の戻り値を配列の添字として使用する例です。PHP は定数についても認識します。以下のような E_* の使用例を見たことがあるかもしれません。
<?php
$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";
?>
最初の例の bar と全く同様に E_ERROR も有効な添字であることに注意してください。 しかし、実際には最後の例は次のように書くことと同じです。
<?php
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>
これは、E_ERROR が 1 と等しいこと等によります。
では、なぜ $foo[bar] は動作することが可能なのでしょう? それは、bar が定数式であることを 期待される構文で使用されているためです。しかし、この場合、 bar という名前の定数は存在しません。PHP は、 この場合、あなたが文字列"bar" のようにリテラル bar を指定したが引用符を忘れたと仮定します。
では、なぜ間違っているのでしょう?
将来的に、PHP 開発チームが他の定数またはキーワードを追加したいと思うかもしれず、 問題となる可能性があります。例えば、現在でも、 単語 empty および defaultを使用することはできません。 これは、これらが特別な 予約済みのキーワードであるためです。
注意: 二重引用符で括られた文字列の中では 引用符で配列の添字を括らないことができ、このため、 "$foo[bar]" は有効です。 この理由の詳細については、上記の例や 文字列中での変数のパースを参照してください。
配列への変換
integer, float, string, boolean, resourceのいずれの型においても、 array に変換する場合、 最初のスカラー値が割り当てられている一つの要素 (添字は 0) を持つ配列を得ることになります。
objectを配列にする場合には、配列の要素として オブジェクトの属性 (メンバ変数) を持つ配列を得ることになります。 添字はメンバ変数名となりますが、いくつか注意すべき例外があります。 private 変数の場合、変数名の頭にクラス名がつきます。また、 protected 変数の場合は、変数名の頭に '*' がつきます。 このとき、頭に追加される値の前後に null バイトがついてきます。 その結果、予期せぬ振る舞いをすることがあります。
<?php
class A {
private $A; // これは '\0A\0A' となります
}
class B extends A {
private $A; // これは '\0B\0A' となります
public $AA; // これは 'AA' となります
}
var_dump((array) new B());
?>
上の例では 'AA' というキーがふたつあるように見えますが、 そのうちひとつは、実際は '\0A\0A' ということになります。
NULL を配列に変換すると、空の配列を得ます。
比較
array_diff() と 配列演算子 を用いると、配列を比較することができます。
例
PHP の配列型は、いろいろな使い方ができます。配列の強力な機能を示すため、 ここでいくつかの例を紹介します。
<?php
// これは、
$a = array( 'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // キーは0になります
);
// これと完全に同じです。
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // キーは0になります
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
// この結果は配列 array(0 => 'a' , 1 => 'b' , 2 => 'c'),
// または単に array('a', 'b', 'c') となります
?>
例1 array() の使用例
<?php
// マップを行う配列
$map = array( 'version' => 4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
);
// 数値キーのみを有する
$array = array( 7,
8,
0,
156,
-10
);
// これは、array( 0 => 7, 1 => 8, ...) と同じです
$switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (最大の添字は5です)
'8' => 2, // key = 8 (整数!)
'02' => 77, // key = '02'
0 => 12 // 値10は12で上書きされます
);
// 空の配列
$empty = array();
?>
例2 コレクション
<?php
$colors = array('red', 'blue', 'green', 'yellow');
foreach ($colors as $color) {
echo "Do you like $color?\n";
}
?>
上の例の出力は以下となります。
Do you like red? Do you like blue? Do you like green? Do you like yellow?
PHP 5 以降では、配列を参照渡しすることでその値を直接変更できるようになりました。 それ以前のバージョンでは、以下のような回避策が必要です。
例3 コレクション
<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* これ以降の $color への書き込みが
配列の要素を書き換えてしまわないことを保証する */
// 旧バージョンでの回避策
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}
print_r($colors);
?>
上の例の出力は以下となります。
Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW )
この例は、1 から始まる配列を作成します。
例4 1 から始まる添字
<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>
上の例の出力は以下となります。
Array ( [1] => 'January' [2] => 'February' [3] => 'March' )
例5 配列に代入する
<?php
// ディレクトリから全てのアイテムを配列に代入する
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>
配列には順番が付けられます。異なったソート関数を用いて順番を変更することも可能です。 より詳細な情報については、配列関数 を参照ください。 count() 関数を使用することで、 配列の要素数を数えることが可能です。
例6 配列のソート
<?php
sort($files);
print_r($files);
?>
配列の値は何でも良いため、その値を他の配列とすることも可能です。 これにより、再帰的な配列や多次元の配列を作成することが可能です。
例7 再帰および多次元配列
<?php
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);
// 上の配列の内容を取得するための例
echo $fruits["holes"][5]; // "second" を表示します
echo $fruits["fruits"]["a"]; // "orange" を表示します
unset($fruits["holes"][0]); // "first" を削除します
// 新しい多次元配列を作成します
$juices["apple"]["green"] = "good";
?>
配列の割り当てにおいては、常に値がコピーされることに注意してください。 これは、current() および類似の関数が使用する 内部配列ポインタの値がリセットされるということを意味します。 配列をリファレンスでコピーする場合には、 リファレンス演算子を使う必要があります。
<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 が変更されます。
// $arr1 は array(2,3) のままです。
$arr3 = &$arr1;
$arr3[] = 4; // $arr1 と $arr3 は同じ内容になります。
?>
配列
13-Sep-2008 05:49
05-Jun-2008 01:43
>fmouse
Variable variables can be used with arrays as discussed in the variable variable section.
Also, from your code, $somevar can be an array and still work fine.
$fum = "somevar";
$$fum = array(); // $somevar is now an array
$foo = $$fum;
print_r( $foo); // we get an empty array printed
print_r() doesn't just print arrays, it prints any variable type.
If you pass it a variable, that hasn't been defined yet, it won't know how to print it.
You would get this same results by passing any undefined variable such as:
print_r( $bar ); // PHP Notice: undefined variable ...$bar
29-Apr-2008 10:14
Using variables as array names no longer works in PHP5.
$fum = "somevar";
$foo = $$fum;
# Still works if $somevar isn't an array. $foo contains the value of $somevar
but ....
$fum = "_POST";
print_r($$fum);
print_r(${$fum});
Neither form of indirection works in this context. $$fum comes back empty.
If you have PHP4 code that used this kind of indirection you can work around the change in PHP5 using an eval().
$fum = "_POST";
$foo = eval("return $$fum;");
print_r($foo);
This works!
18-Mar-2008 10:52
Sorting double dimensional arrays by a specified key bothe for Strings and for integers:
Ex:
$personDetails = array( array("firstName" => "Nancy", "lastName" => "Grace", "age" => 22), array("firstName" => "Andy", "lastName" => "Peter", "age" => 28), array("firstName" => "Jim", "lastName" => "Gary", "age" => 25), array("firstName" => "Lary", "lastName" => "James", "age" => 28), array("firstName" => "Peter", "lastName" => "David", "age" => 17), array("firstName" => "Raj", "lastName" => "King", "age" => 9), array("firstName" => "John", "lastName" => "Baxter","age" => 35) );
//To sort the array by firstName:-
function sortFirstName($p1, $p2) {
return strnatcmp($p1['firstName'], $p2['firstName']);
}
usort($personDetails, 'sortFirstName');
//To sort by an integer Field
function sortByInteger(&$personDetails, $field) {
$sort = "return strnatcmp(\$p1['$field'], \$p2['$field']);";
usort($personDetails, create_function('$p1,$p2', $sort));
return $personDetails;
}
$personDetails = sortByInteger($personDetails, 'age');
//To sort the array in Descending order by a key, It can be done by adding "-" sign before strnatcmp() function.
I hope this helps
09-Jan-2008 05:00
"If you convert a NULL value to an array, you get an empty array."
This turns out to be a useful property. Say you have a search function that returns an array of values on success or NULL if nothing found.
$values = search(...);
Now you want to merge the array with another array. What do we do if $values is NULL? No problem:
$combined = array_merge((array)$values, $other);
Voila.
19-Dec-2007 01:25
z on 22-Apr-2005 12:10 wrote:
-----------------------------------------------
Here's a simple function to insert a value into some position in an array
<?php
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
?>
and now for example...
<?php
$a = array("John","Paul","Peter");
$a = array_insert($a,1,"Mike");
?>
Now $a will be "John","Mike","Paul","Peter"
-----------------------------------------------
Im learning to use PHP and reading about array_splice found that
<?php
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
$a = array("John","Paul","Peter");
$a = array_insert($a,1,"Mike");
print_r($a);
?>
would output the same as
<?php
$b = array("John","Paul","Peter");
array_splice($b,1,0,array("Mike"));
print_r($b);
?>
10-Oct-2007 11:14
<?php
//Simple Login Script using associative array.
//You may modify the codes and use $_POST['FORM_FIELD_DATA'] for your web-page.
//You may spice-up the codes with more form field validation & security features.
//$user_name=$_POST['user_name'];
//$password=$_POST['password'];
$test_user_name = 'michelle_smith'; //for testing purpose only
$test_password = 'msmith321'; //for testing purpose only
$user_name = $test_user_name;
$password = $test_password;
// here user_name is key and password is the value of an array..
// website owner has to add new user/site member manually in $login_array
$login_array = array(
'user_name' => 'password',
'alex_duff' => 'alx321',
'xena78' => 'xena321',
'dela_pena' => 'delp321',
'shawn_1981' => 'shw81',
'michelle_smith' => 'msmith321');
ksort ($login_array);
reset($login_array);
if (isset($login_array[$user_name]))
{
$pass_check = $login_array[$user_name];
if ($password === $pass_check)
{
echo "Welcome, $user_name!\n<br>"; //may redirect to specific webpage.
}
else
{
echo "Please try again!"; //may redirect to Error page.
}
}
else
{
echo "Please register with us. Thanks!"; //may redirect to registration page.
exit();
}
echo("\n<br>");
echo 'Thanks to Thies C. Arntzen, Stig Bakken, Shane Caraveo, Andi Gutmans, Rasmus Lerdorf, Sam Ruby, Sascha Schumann, Zeev Suraski, Jim Winstead, Andrei Zmievski for wonderful PHP!';
?>
06-Sep-2007 08:36
Its worth noting that there does not appear to be any functional limitations on the length or content of string indexes. The string indexes for your arrays can contain any characters, including new line characters, and can be of any length:
<?php
$key = "XXXXX";
$test = array($key => "test5");
for ($x = 0; $x < 500; $x++) {
$key .= "X";
$value = "test" . strlen($key);
$test[$key] = $value;
}
echo "<pre>";
print_r($test);
echo "</pre>";
?>
Keep in mind that using extremely long array indexes is not a good practice and could cost you lots of extra CPU time. However, if you have to use a long string as an array index you won't have to worry about the length or content.
30-Aug-2007 10:56
<?php
//EXAMPLE of Multi-Dimentional Array where as an array's keys are an array itself.
//It's so easy to create one like this.
$movie_to_watch = array ('Action'=>
array('Kanu Reeves' => 'Matrix Reloaded',
'Pearce Brosnan' => 'Die Another Day',
'Tom Cruz' => 'Mission Impossible',
'Jason Statham' => 'Crank',
'Danzel Washington' => 'Man on Fire'),
'Comedy' =>
array ('Charlie Chaplin' => 'City Lights',
'Jim Carrey' => 'Cable Guy',
'Rowan Atkinson' => 'The Ultimate Disaster'));
$type_wanted = 'Action'; //You may switch type from Action to Comedy.
$hero_wanted = 'Pearce Brosnan'; // You may switch hero from Pearce Brosnan to Jim Carrey.
print ("$hero_wanted 's $type_wanted movie is " . $movie_to_watch[$type_wanted][$hero_wanted].".");
// produces browser output as under:
// Pearce Brosnan 's Action movie is Die Another Day.
?>
21-Aug-2007 11:59
Hey..
here is a function which helps to avoid using empty/isset
checkings for arrays.
(it's acts simillar to 'default' modifier in Smarty)
Using this function you will avoid 'Undefined index' or
'Undefined offset' error.
<?php
$_POST['id']['other'] = 'val1';
/*
key exist (same as $_POST['id'][other])
*/
echo getRequestParam('id[other]', 'default value');
/*
key doesn't exist, we get default value (same as $_POST['var'])
*/
echo getRequestParam('var', 'default value');
function getRequestParam( $var, $default = '', $method = 'post' )
{
preg_match_all('!(\w+)!i',$var, $match );
array_shift($match);
$_vars = $match[0];
$ret = null;
if( strtoupper($method) == 'POST' ) {
$ret = _findRequestParam($_vars, $_POST);
}
elseif( strtoupper($method) == 'GET' ) {
$ret = _findRequestParam($_vars, $_GET);
}
elseif( strtoupper($method) == 'COOKIE' ) {
$ret = _findRequestParam($_vars, $_COOKIE);
}
elseif( strtoupper($method) == 'SESSION' ) {
$ret = _findRequestParam($_vars, $_SESSION);
}
if (! $ret )
return $default;
else
return $ret;
}
/**
@access private
*/
function _findRequestParam($vars, $find_in , $curr_key = 0)
{
static $ret;
if( array_key_exists($vars[$curr_key], $find_in) ) {
if( count( $vars)-1 == $curr_key ) {
$ret = $find_in[$vars[$curr_key]];
}
elseif( $curr_key < count( $vars)-1 ) {
_findRequestParam( $vars, $find_in[$vars[$curr_key]], $curr_key+1 );
}
}
return $ret;
}
?>
Hope this will help someone!
14-Jul-2007 05:34
Another note on unquoted array indices. Because it is first interpreted as a constant, it must obey the naming convention of constants. i.e. a letter or underscore followed by optional letter, digit and/or underscore characters.
Therefore while the following array declaration is legal:
$a = array('1st'=>'First','2nd'=>'Second');
Trying to access either array item as follows causes an error:
$first = "$a[1st]";
$second = "$a[2nd]";
11-Jul-2007 02:41
<b>Mark Gukov</b> wrote below:
Regarding the fact that there's no need to quote arrays keys when enclosed in double quotes: it only applies to single dimensional arrays.
The following works fine:
<?php
$r['a'] = 'apple';
echo "$r[a] is tasty.";
?>
...but in the case of multi-dimensional arrays:
<?php
$r['a']['b'] = 'banana';
echo "$r[a][b] is tasty.";
?>
would result in "Array[c] is tasty."
-----------------------------------------------------------------
However, the following runs fine;
$r['a']['b'] = 'banana';
echo "{$r[a][b]} is tasty.";
Just box it!
24-May-2007 02:37
It's slightly faster to use array_splice to remove an element of an array:
array_splice($array, $index, 1)
than to do it using the suggested method of unset and reindex:
unset($array[$index]);
$array = array_values($array);
The difference, however, is very small. With 950 iterations I had times of
unset and reindex: 0.22837495803833
splice: 0.22392416000366
20-Mar-2007 11:14
This Indonesian number speller function is twice
faster(*) than the one provided in class Terbilang by
anghuda(at)gmail(dot)com (25-May-2006 08:52):
http://www.lesantoso.com/terbilang.html
(*) 2.1 vs. 4.2 seconds in processing 10000 random numbers
16-Mar-2007 10:44
On array recursion...
Given the following code:
<?
$myarray = array('test',123);
$myarray[] = &$myarray;
print_r($myarray);
?>
The print_r() will display *RECURSION* when it gets to the third element of the array.
There doesn't appear to be any other way to scan an array for recursive references, so if you need to check for them, you'll have to use print_r() with its second parameter to capture the output and look for the word *RECURSION*.
It's not an elegant solution, but it's the only one I've found, so I hope it helps someone.
17-Jan-2007 02:55
About the automatic conversion of bare strings...
My opinion is that it never should have been implemented. Isn't it easier to NOT implement this "handy" feature in the first place? It is such a convenient way for "smart" programmers to write unsafe, not-futureproof code. Please remove this feature from future versions of PHP, please. (Hey, if you could change the OOP mechanisms between PHP 4 and PHP 5, why can't you make this change, right?)
This page should include details about how associative arrays are implemened inside PHP; e.g. using hash-maps or b-trees.
This has important implictions on the permance characteristics of associative arrays and how they should be used; e.g. b-tree are slow to insert but handle collisions better than hashmaps. Hashmaps are faster if there are no collisions, but are slower to retrieve when there are collisions. These factors have implictions on how associative arrays should be used.
27-Sep-2006 11:18
Regarding the fact that there's no need to quote arrays keys when enclosed in double quotes: it only applies to single dimensional arrays.
The following works fine:
<?php
$r['a'] = 'apple';
echo "$r[a] is tasty.";
?>
...but in the case of multi-dimensional arrays:
<?php
$r['a']['b'] = 'banana';
echo "$r[a][b] is tasty.";
?>
would result in "Array[c] is tasty."
21-Sep-2006 05:30
Regarding the message of phoenixbytes:
The line foreach($bad as $baddies); will just yield in $baddies the last value of the array $bad.
I think that wasn't your intention, in that case there are faster and better ways than foreach.
I think what you wanted to do is:
<?php
foreach($bad as $baddies) // make a collection
{
if (preg_match("/$baddies/i", $mailto)) // find a match
{
$addrmail = "false";
}
else
{
$addrmail = "true";
}
} // foreach end
?>
26-Jul-2006 01:28
in response to ch dot martin at gmail dot com
If you are using the following code:
<?php
$r = array('05' => "abc", '35' => "def");
foreach ($r as $key=>$value)
var_dump($key);
?>
and you need the array key '35' to be a string (for looping maybe), you can make sure the key is a string by appending a 0 on the front.
'035' instead of '35'
09-Jun-2006 08:40
Extremely irritating quirk regarding the variable types of array keys:
<?php
$r = array('05' => "abc", '35' => "def");
foreach ($r as $key=>$value)
var_dump($key);
?>
The first var_dump for '05' is:
string(2) "05"
as expected. But the second, '35', turns out as:
int(35)
Php apparently decided to make the 35 became an int, but not the 05 (presumably because it leads with a zero). As far as I can see, there is absolutely no way of making string(2) "35" an array key.
25-May-2006 03:52
this is simpler tha function display_angka_bilangan by ktaufik(at)gmail(dot)com (16-Feb-2005 12:40)
<?
/*
*
* Class : Terbilang
* Spell quantity numbers in Indonesian or Malay Language
*
*
* author: huda m elmatsani
* 21 September 2004
* freeware
*
* example:
* $bilangan = new Terbilang;
* echo $bilangan -> eja(137);
* result: seratus tiga puluh tujuh
*
*
*/
Class Terbilang {
function terbilang() {
$this->dasar = array(1=>'satu','dua','tiga','empat','lima','enam',
'tujuh','delapan','sembilan');
$this->angka = array(1000000000,1000000,1000,100,10,1);
$this->satuan = array('milyar','juta','ribu','ratus','puluh','');
}
function eja($n) {
$i=0;
while($n!=0){
$count = (int)($n/$this->angka[$i]);
if($count>=10) $str .= $this->eja($count). " ".$this->satuan[$i]." ";
else if($count > 0 && $count < 10)
$str .= $this->dasar[$count] . " ".$this->satuan[$i]." ";
$n -= $this->angka[$i] * $count;
$i++;
}
$str = preg_replace("/satu puluh (\w+)/i","\\1 belas",$str);
$str = preg_replace("/satu (ribu|ratus|puluh|belas)/i","se\\1",$str);
return $str;
}
}
?>
09-May-2006 10:46
phoenixbytes: The regex you have posted for matching email addresses is incorrect. Among other things, it does not allow '+' before the '@' (which is perfectly valid and can be quite useful to separate extensions of a single address). RFC 822 [1] defines the grammar for valid email addresses, and (the extemely long) regex implementing can be found at [2]. Even the "Add Note" page here at php.net says:
[quote]
And if you're posting an example of validating email addresses, please don't bother. Your example is almost certainly wrong for some small subset of cases. See this information from O'Reilly Mastering Regular Expressions book for the gory details.
[/quote]
A note to others: please do your homework before writing another email-matching regex.
[1] http://www.ietf.org/rfc/rfc0822.txt?number=822
[2] http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html.
17-Apr-2006 02:10
i use the array() function for deciding upon an email address's validity, i have a 'wap stalker' of my site that loves to exploit every hole i leave, so i used the following script to avoid being email bombed with my own file sender script, the array() is used to filter out undesirable email providers and, of course, any and all of my own addresses. before all that i used a REGEX to make sure it's an actual email address before going any further.
$mailto = "mail.domain.org"; // the input to be tested
if (preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $mailto)) // see if it's really an email address
{
$bad = array('mytrashmail.com', 'mymail.ro', 'acasa.ro', 'gala.net', 'phoenixbytes'); // pick out the victims
foreach($bad as $baddies); // make a collection
if (preg_match("/$baddies/i", $mailto)) // find a match
{
$addrmail = "false";
}
else
{
$addrmail = "true";
}
}
else
{
$addrmail = "false";
}
$addrmail can then be used in an argument.
$baddies can be used to give a list, if necessary.
i hope this helps someone.
01-Apr-2006 10:54
Passing variables into the array constructor:
Just a NOOB pointer, I couldn't find other examples for this. If you want to pass the value of an existing variable into the array() constructor, you can quote it or not, both methods are valid.
<?
$foo_value = 'foo string';
$bar_value = 'bar string';
$myArray = array(
'foo_key' => $foo_value, // not quoted
'bar_key' => 