【发布时间】:2018-11-07 08:14:04
【问题描述】:
我有两个数组,一个包含一个名称列表(数组名称 = canNAMES),
["name 1","name 2","name 3"];
第一个数组中有大约 70 个值,而我的第二个数组中有大约 600 个对象(数组名称=data),
[
{
"agency": "test agency",
"work_end": "21-Oct",
"contractor": "name n",
"rate": "£30.00",
"hours": 32,
"exp": null,
"net": "£960.00",
"vat": "£192.00",
"gross": "£1,152.00"
},
{
"agency": "test agency",
"work_end": "21-Oct",
"contractor": "name n",
"rate": "£25.00",
"hours": 30,
"exp": null,
"net": "£750.00",
"vat": "£150.00",
"gross": "£900.00"
}
]
我正在尝试使用 php in_array 函数来获取具有第一个数组中列出的名称的对象。
当我如下使用它时,我可以获得所需的结果,但它最多只能读取 70 条记录
foreach ($canNAMES as $index => $row) {
if (in_array($row, (array) $data[$index]["contractor"])) {
$MAIN[] = $data[$index];
}
}
上面的代码是我遍历第一个数组(canNAMES 数组)的地方,它有 70 条记录。当我尝试遍历第二个数组(数据数组)时,我得到一个未定义的偏移错误,因为第一个数组的索引不超过 69。
我的问题是如何解决这个问题,有没有更好的方法来做我正在尝试的事情。
谢谢
【问题讨论】:
-
array_in- 你是说in_array? -
您是否尝试过搜索
$data数组并检查$canNAMES? -
@Lithilion 是的,它不起作用