我有2个阵列
作为$ result,我得到了一个像$ first一样的数组,但是来自$ second的数据(ID,post_title,post_name)
我可以使用以下方式获得结果:
$posts = array_filter($second, function ($post) use ($first) {
return in_array($post->post_name, $first);
});
但是问题$ posts并没有像$ first那样订购。
由于
$FIRST
Array
(
[0] => Monday-5
[1] => Tuesday-5
[2] => Wednsday-5
[3] => Thursday-1
[4] => Friday-1
[5] => Saturday-1
[6] => Sunday-1
)
$SECOND
Array
(
[0] => WP_Post Object
(
[ID] => 2878
[post_title] => Monday-1
[post_name] => monday-1
)
[1] => WP_Post Object
(
ID] => 2879
[post_title] => Tuesday-5
[post_name] => tuesday-5
)
...
[34] => WP_Post Object
(
ID] => 2899
[post_title] => Sunday-5
[post_name] => sunday-5
)
)
答案 0 :(得分:0)
如果我理解你,你想要一个结果数组首先包含[post_title] => Monday-5
的所有元素,那么所有[post_title] => Tuesday-5
依此类推,对吧?
然后创建第三个空数组$result
。在$first
上迭代$second
,在$second
上迭代,并将$result
中的所有项目复制到$first[$i] == $second[$i]['post_title']
,以满足您的条件$result = array();
foreach($first as $scope){
foreach($second as $id=>$item){
if($item->post_title == $scope){
$result[$id] = $item;
}
}
}
。
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Entity")
request.returnsObjectsAsFaults = false
do {
let entityTableInCoreData = try newPrivateQueueContext.fetch(request)
for i in entityTableInCoreData {
if let a = i.valueForKey("b") as? String {
print(a)
}
}
} catch {
}