によって配列の要素のインデックスを取得し、私はPHPでこの配列を持っている:私は、「ユーザー名」の文字列を持っている場合は値
array(
[0] => array('username' => 'user1')
[1] => array('username' => 'user2')
)
は、どのように私は数としてインデックス値を得ることができますか?
私が 'user1'を持っている場合、どうすれば0を得ることができますか?
によって配列の要素のインデックスを取得し、私はPHPでこの配列を持っている:私は、「ユーザー名」の文字列を持っている場合は値
array(
[0] => array('username' => 'user1')
[1] => array('username' => 'user2')
)
は、どのように私は数としてインデックス値を得ることができますか?
私が 'user1'を持っている場合、どうすれば0を得ることができますか?
あなたは2次元配列を持っている場合は、あなたの例のように、あなたは物事を少しカスタマイズする必要がありますが:
function array_search2d($needle, $haystack) {
for ($i = 0, $l = count($haystack); $i < $l; ++$i) {
if (in_array($needle, $haystack[$i])) return $i;
}
return false;
}
$myArray = array(
array('username' => 'user1'),
array('username' => 'user2')
);
$searchTerm = "user1";
if (false !== ($pos = array_search2d($searchTerm, $myArray))) {
echo $searchTerm . " found at index " . $pos;
} else {
echo "Could not find " . $searchTerm;
}
あなたがしたい場合特定のフィールドだけで検索すると、次のような関数に変更できます。
function array_search2d_by_field($needle, $haystack, $field) {
foreach ($haystack as $index => $innerArray) {
if (isset($innerArray[$field]) && $innerArray[$field] === $needle) {
return $index;
}
}
return false;
}
array_searchをご覧ください。 PHPのヘルプファイルから
:
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
2D配列を持っているので、これはうまくいかないでしょう。しかし...それは奇妙に受け入れられています。 – nickf
ええ、ごめんなさい、あまりにも早く受け入れました。どのように2次元配列でそれを行う上の任意のアイデア? – roflwaffle
@nickf、それは実際に質問のタイトルに答える、それは確かにそれのボディに答えることはないと思った。 –
おそらくarray_filter
とarray_keys
が一緒に役立ちます。
クラスベースのアプローチ。
<?php
class ArraySearch2d {
static protected $_key;
static protected $_value;
static function isMatch($element)
{
if (!is_array($element)) return false;
return $element[self::$_key] == self::$_value;
}
static function filter(array $arrayToSearch, $key, $value)
{
if (!is_string($key)) throw new Exception("Array Key must be a string");
self::$_key = $key;
self::$_value = $value;
return array_filter($arrayToSearch, 'ArraySearch2d::isMatch');
}
// to directly answer your question.
static function getIndex(array $arrayToSearch, $key, $value)
{
$matches = self::filter($arrayToSearch, $key, $value);
if (!count($matches)) return false;
$indexes = array_keys($matches);
return $indexes[0];
}
}
$array = array("1"=>array('username'=>'user1'), "3"=>array('username'=>'user2'));
$matches = ArraySearch2d::filter($array, 'username', 'user2');
var_dump($matches);
$indexs = array_keys($matches);
var_dump($indexs);
// Demonstrating quick answer:
echo "Key for first 'username'=>'user1' element is: "
.ArraySearch2d::getIndex($array, 'username', 'user1')."\n";
が生成されます
array(1) {
[3]=>
array(1) {
["username"]=>
string(5) "user2"
}
}
array(1) {
[0]=>
int(3)
}
Key for first 'username'=>'user1' element is: 1
クラスを使用しない - これは、同じ結果を生成します。
<?php
$field="username";
$value = "user2";
function usernameMatch($element)
{
global $field, $value;
if (!is_array($element)) return false;
return $element[$field] == $value;
}
function getFirstIndex(array $array)
{
if (!count($array)) return false;
$indexes = array_keys($array);
return $indexes[0];
}
$array = array("1"=>array('username'=>'user1'), "3"=>array('username'=>'user2'));
$matches = array_filter($array, 'usernameMatch');
var_dump($matches);
$indexs = array_keys($matches);
var_dump($indexs);
// Demonstrating quick answer - and why you should probably use the class-
// you don't want to have to remember these "globals" all the time.
$field = 'username';
$value = 'user1';
echo "Key for first 'username'=>'user1' element is: "
.getFirstIndex(array_filter($array, 'usernameMatch'));
あなたがいることを知っていればこれは
private function getArrayKey($haystack, $needle)
{
foreach($haystack as $key => $product)
{
if ($product['id'] === $needle)
return $key;
}
return false;
}
を非常にシンプルになりますキーはですの場合、検索パラメータとして配列を使用してください:
$username = 'user1';
$key = array_search(array('username' => $username), $array);
for/countループで配列をループする代わりに、 "foreach($ haystack as $ key => $ value)"と同じように、 "username1"だけでなく、任意のフィールドで 'user1'を見つけるでしょう。 – gnarf
ああ。 OPは、問題のその側面について少し曖昧でした。私は今編集します。 – nickf
非常にいい解決策+1 – nikhil