我的Array
包含一组Arrays
...
array (size=5)
0 =>
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Naaldhout - gewone den' (length=22)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string 'Middeloud naaldhout (20 - 60j)' (length=30)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
1 =>
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Loofhout - eik of Amerikaanse eik' (length=33)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string '10' (length=2)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
我还有一个Array
,其中包含Array
个键列表,用于在添加新条目时检查主Array
(上面的定义)。如果这些键/值已经存在 - 我不想将该项添加到主Array
。如果一个或多个键/值没有激发......数组可以添加到主集合中。
array (size=7)
'BMS',
'ONT',
'SLG',
'BED',
'NISCODE',
'KLASSE',
'POLNR'
情况1 ;添加已在所有已定义键上排除的Array
。
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Loofhout - eik of Amerikaanse eik' (length=33)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string '10' (length=2)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
情况2 ;在所有已定义的键上添加一个不存在的Array
。请注意,BMS
键具有主集合尚不知道的value
。
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Unadded value' (length=33)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string '10' (length=2)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
我正在寻找检查主Array
集合的最佳方法,如果它已包含Array
,其中需要检查的密钥由其他Array
给出。< / p>
我已经尝试循环3 Arrays
来互相检查 - 我创建的代码非常强大而且很大。我希望看到一些新的尝试可以让我减少代码行...我所拥有的伪代码
/**
* @Param collection = Global collection
* @Param requirekeystocheck = Array keys to check for excistance
* @Param arraytoadd = Array to be added
* @return Boolean
*/
Function checkExcistArrayOfKeysForArray($collection, $requirekeystocheck, $arraytoadd){
Loop main Array $collection as $collectionItem
Loop Array $requirekeystocheck with keys to check as $uniqueKey
Check if main $collectionItem[$uniqueKey] contains $arraytoadd[$uniqueKey]
Check if the values are equal (already present)
If all values in $requirekeystocheck excist in $collectionItem - the item that needs to be added already excists.
Return TRUE
If not all values excist - the item can be added
Return TRUE
}
...谢谢
答案 0 :(得分:1)
考虑使用php类。这一切看起来都可以很容易地被替换。该课程可能如下所示:
class myClass {
public $bms = '';
public $ont = '';
public $slg = '';
public $bed = '';
public $niscode = '';
public $klasse = '';
public $polnr = '';
function __construct($cData) {
$this->bms = $cData[0];
$this->ont = $cData[1];
$this->slg = $cData[2];
$this->bed = $cData[3];
$this->niscode = $cData[4];
$this->klasse = $cData[5];
$this->polnr = $cData[6]
}
function equals($obj) {
// this function receives another instance of the class and checks whether both instances are identical
if ($obj->bms != $this->bms) { return false; }
if ($obj->ont!= $this->ont) { return false; }
if ($obj->slg != $this->slg) { return false; }
if ($obj->bed != $this->bed) { return false; }
if ($obj->niscode != $this->niscode) { return false; }
if ($obj->klasse != $this->klasse) { return false; }
if ($obj->polnr != $this->polnr) { return false; }
return true;
}
}
我唯一不确定的是带有“key”和“value”的小数组,你一直在使用它。有没有办法摆脱它?我说不出来,因为我不知道这些价值观的目的。如果你解释他们的感觉,我可能会想象另一个。如果这是不可能的,最好为它创建另一个类(类似于数据类型):
class myDatatype {
public $key = '';
public $value = '';
public construct __construct($key, $value)
$this->key = $key;
$this->value = $value;
}
}
myClass中的所有变量然后作为数据获取myClass的一个实例,如下所示:
$example->bms = new myDatatype("Bomsoort", "Somewhat");
向类中添加新值的方式取决于“myClass”变量的类型。以下示例假定您通过“myDatatype”使用了解决方案:
$data = array(/* bms */ new myDatatype("key", "value"), ..., /* polnr */ myDatatype("key", "value"));
$example = new myClass($data);
因为这可能变得非常混乱,想想另一个功能,它采取所有必要的值,并完全按照我上面所写的那样做。 thingy = new myClass($ data);
答案 1 :(得分:0)
尝试使用array_column搜索数组的数组搜索方法 参考下面使用会话
将项目添加到购物车的解决方案<?php
session_start();
$pr_id = $_REQUEST['id'];
$quantity = isset($_REQUEST['quantity'])?$_REQUEST['quantity']:1;
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add')
{
if(isset($_SESSION['cartitems']))
{
//find index of an array if it exits then do not add else add to cart and then
if (!array_key_exists($pr_id,$_SESSION['cartitems']))
{
$_SESSION['cartitems'][$pr_id]['id'] = $pr_id;
$_SESSION['cartitems'][$pr_id]['quantity'] = $quantity;
}
}
else
{
$_SESSION['cartitems'][$pr_id]['id'] = $pr_id;
$_SESSION['cartitems'][$pr_id]['quantity'] = $quantity;
}
$arr = array();
$arr['count'] = count($_SESSION['cartitems']);
$arr['message'] = 'Item successFully added';
echo json_encode($arr);
}