मैं निम्नलिखित सरणियों तुलना करने के लिए उचित तर्क के साथ संघर्ष कर रहा हूँ के लिए दो सरणियों तुलना:दो मानों
$a = [
"ip" => [
"1.2.3.4",
"4.3.2.1",
],
"domain" => [
"example.com",
"another.domain",
],
];
$b = [
[
"id"=> 136589,
"metaname" => "ip",
"metavalue" => "1.2.3.4",
],
[
"id"=> 136590,
"metaname" => "domain",
"metavalue" => "example.com",
],
];
मैं कुंजी खोजने के लिए ('ip'
) मूल्य ('1.2.3.4'
) संयोजन है कि $a
से अधिक पाश की जरूरत है $b
में मौजूद नहीं है। सरणी $a
में, मैं आईपी '4.3.2.1'
और डोमेन 'another.domain'
$b
अलग कुंजी के साथ मिलान मूल्यों के लिए के लिए यह संभव है पर कब्जा करने की ज़रूरत है?
एक अच्छा उदाहरण 'ip'
पते का एक अच्छा उदाहरण है। संभावित आईपी से संबंधित मेटानाम मान 'ip'
, 'ip.dst'
और 'ip.src'
हैं। नमूना डेटा पर वापस - भले ही 'ip'
मैचों, यदि मेटानाम मेल नहीं खाता है तो इसे छोड़ दिया जाना चाहिए।
foreach ($a as $metaName => $metaValues)
{
foreach ($metaValues as $metaValue)
{
foreach ($b as $row)
{
if (in_array($metaName, $row) && in_array($metaValue, $row))
{
# this pair exists, move on to next $metaName-$metaValue pair
break;
}
# this is where i am now, making small progress
# more trial and error going on
}
}
}
मेरे नमूना कोड में टिप्पणी है जहां मुझे सहायता चाहिए। मैं अलग चेकों के विभिन्न पुनरावृत्तियों की कोशिश की और कोई लाभ नहीं हुआ उपयुक्त डेटा पर कब्जा करने के लूप है ...
in_array($metaValue, $row)
array_keys($row, $metaValue)
इतने पर विभिन्न if
बयान के साथ संयुक्त और लेकिन वह जीता मदद नहीं
यदि मेरा विवरण स्पष्ट होने में असफल रहा तो शायद निम्न तालिका सहायता करेगी।
+ A ---------------------+----+ B ------------------+ Comment ------------------------+
| ip, 1.2.3.4 | == | ip, 1.2.3.4 | Skip, no more checks |
+------------------------+----+---------------------+---------------------------------+
| ip, 4.3.2.1 | != | ip, 1.2.3.4 | Keep checking |
| | != | domain, example.com | No more B to compare, I want A! |
+------------------------+----+---------------------+---------------------------------+
| domain, example.com | != | ip, 1.2.3.4 | Keep checking |
| | == | domain, example.com | Skip, no more checks |
+------------------------+----+---------------------+---------------------------------+
| domain, another.domain | != | ip, 1.2.3.4 | Keep checking |
| | != | domain, example.com | No more B to compare, I want A! |
+------------------------+----+---------------------+---------------------------------+
आह! यहां तक कि मेरा केवल आंशिक रूप से काम करने के साथ ही मेरे पास पूर्ण कोड की तुलना में काफी बेहतर है। बहुत बहुत धन्यवाद। – user1801810