あなたの関数が宣言されている方法は、はにパラメータを渡します。
EDIT
私が正しくあなたのコメントを読めば、$myArray
は次のようになります:何がやりたいことは関数内のコードがそれを検出できるように、デフォルト値を持っています
$myArray=array
(
'apple'=>3, // 3 apples
'orange'=>4, // 4 oranges
'banana'=>2, // 2 bananas
'eggs'=>12, // 12 eggs
'coconut'=>1, // 1 coconut
);
何をしたい、それが本当だと仮定すると
です3210
function myfunction($params=NULL)
{
global $myArray;
$count=0;
if (empty($params)) // count everything
{
foreach ($myArray as $num) // keys are ignored
$count += $num;
}
else if (!is_array($params)) // sanity check
{
// display an error, write to error_log(), etc. as appropriate
}
else // count what's listed in the $params array
{
foreach ($params as $key) // check each item listed in $params
if (isset($myArray[$key])) // insure request in $myArray
$count += $myArray[$key]; // add item's count to total
}
return $count;
}
これは、これはあなたが探している結果ではない場合、あなたはあなたの質問を書き直す必要があるあなたに
myfunction(); // returns 22
myfunction(array('banana')); // returns 2
myfunction(array('apple','eggs')); // returns 15
myfunction(array('tomatoes')); // returns 0 - not in $myArray
を与えるだろう。
EDIT明示的なキーなしで指定された配列は、要素が記載されているために、数値キー止めされているので、私は上記示した関数呼び出しは、これらとまったく同じであること#2
注
myfunction(); // returns 22
myfunction(array(0=>'banana')); // returns 2
myfunction(array(0=>'apple',1=>'eggs')); // returns 15
myfunction(array(0=>'tomatoes')); // returns 0 - not in $myArray
ただし、通話はこれらと同等ないます:
myfunction(); // returns 22
myfunction(array('0'=>'banana')); // returns 2
myfunction(array('0'=>'apple','1'=>'eggs')); // returns 15
myfunction(array('0'=>'tomatoes')); // returns 0
この場合
、明示ストリングキーアレイに対して指定され、文字列の値は、数値指標として下最も状況を同じを評価する一方、列インデックスはと同じではありません数字のもの。
あなたはあなたの答えで提案されているコードは、いくつかの誤りがあります。常に
foreach($myArray as $item)
{
foreach ($params as $key => $value)
{
if (isset($params[$key]) && $params[$key] == $item[$key] )
{
$count += $item
}
}
}
まず、isset($params[$key])
ますは、自然や配列とforeach
によってTRUEに評価されます。
'apple' == 'apple'[0] // testing 'apple' == 'a'
'apple' == 'orange'[0] // testing 'apple' == 'o'
'apple' == 'banana'[0] // testing 'apple' == 'b'
'apple' == 'eggs'[0] // testing 'apple' == 'e'
'apple' == 'coconut'[0] // testing 'apple' == 'c'
あなたが見ることができるように、これは生成されません:私は$key
が0
であるため、これらのテストを行う$params[$key] == $item[$key]
になりますmyfunction(array('apple'))
を呼び出して、上に示したように、第2、なぜならあなたの外側のforeach
ループで、あなたの$myArray
場合は構成されています予想された結果。
コードの3番目の問題は、$count += $item
行の最後にセミコロンがないことです。答えとして提案する前にこのコードを実行しようとしなかったと思います。
EDIT#3
あなたの元の質問がひどく明確ではないので、多分あなたが何をしようとしていること$myArray
にタイプのもののの数を数えるのではなくであることを私に起こりました要求された各カテゴリのアイテムの合計数を取得します。その場合には、myfunction()
の最後の分岐がさらに簡単である:これらの結果のどちらがあなたに何をしている場合
else // count what's listed in the $params array
{
foreach ($params as $key) // check each item listed in $params
if (isset($myArray[$key])) // insure request in $myArray
$count++; // add the item to the total
}
サンプル私が示され$myArray
では、上記の変更は、再びあなたに
myfunction(); // returns 5
myfunction(array('banana')); // returns 1
myfunction(array('apple','eggs')); // returns 2
myfunction(array('tomatoes')); // returns 0 - not in $myArray
を与えますあなたの質問を書き直す必要があります。は、$myArray
のサンプルを含みます。 @FKEinternet解答後
もし 'if'と一緒になる' else'についてのべる – alzee
デフォルト値を設定したいのであれば、デフォルト値の配列を作成して、 '$ params'配列に' array_merge() 'を使ってみてください。 – Terry
わからない質問! – Jerald