我有一个页面只是为了在WordPress中使用高级自定义字段存储我需要的数据而我将其命名为 page_categories-animation.php
在我的 index.php 中,我写了这个
// get post id from page feature animation categories
$anim_id = url_to_postid('feature-animation-categories');
// get all the data from it
$variable = get_field('animCategory', $anim_id);
print_r($variable);
$ variable 的输出是这样的(实际上它有20个数组,但我使用2个数组就这么容易理解)
Array
(
[0] => Array
(
[category_name] => Coffee Machines
[categoryId] => 12
[animateTitle] => Better ways to cleaner dishes.
[animateIcon] =>
[animateLink] => http://google.com
[animateBG] =>
[fram1] => Array
(
[ID] => 315
[id] => 315
[title] => coffee-frame1
[filename] => coffee-frame1.png
)
[fram2] => Array
(
[ID] => 316
[id] => 316
[title] => coffee-frame2
[filename] => coffee-frame2.png
)
)
[1] => Array
(
[category_name] => Cooking & Baking
[categoryId] => 13
[animateTitle] => Better ways to cleaner dishes.
[animateIcon] =>
[animateLink] => http://google.com
[animateBG] =>
[fram1] => Array
(
[ID] => 318
[id] => 318
[title] => cookie-frame1
[filename] => cookie-frame1.png
)
[fram2] => Array
(
[ID] => 319
[id] => 319
[title] => cookie-frame2
[filename] => cookie-frame2.png
)
)
)
我的问题是:我如何使用与该值匹配的数组,在这种情况下假设我们已经有 categoryId 13 并且我想获取具有 categoryId 13 <的数组/ strong>并使用它。
答案 0 :(得分:2)
这将有效:
$use_array = [];
foreach($variable as $v) // loop through your array of data
{
if($v['categoryId'] == 13) // look for categoryId = 13
{
$use_array = $v; // save this array for later use
break; // quit looping to save processing time and resources
}
}
print_r($use_array); // this will be your array with categoryId = 13
答案 1 :(得分:1)
你可以简单地过滤你的数组
您的数组
$variable = [
"0" => [
"category_name" => 'Coffee Machines',
"categoryId" => 12,
"animateTitle" => 'Better ways to cleaner dishes.',
"animateIcon" => '',
"animateLink" => 'http://google.com',
"animateBG" => "",
"fram1" => [
"ID" => 315,
"id" => 315,
"title" => 'coffee-frame1',
"filename" => 'coffee-frame1.png',
],
"fram2" => [
"ID" => 316,
"id" => 316,
"title" => 'coffee-frame2',
"filename" => 'coffee-frame2.png',
]
],
"1" => [
"category_name" => 'Cooking & Baking',
"categoryId" => 13,
"animateTitle" => 'Better ways to cleaner dishes.',
"animateIcon" => '',
"animateLink" => 'http://google.com',
"animateBG" => '',
"fram1" => [
"ID" => 318,
"id" => 318,
"title" => 'cookie-frame1',
"filename" => 'cookie-frame1.png',
],
"fram2" => [
"ID" => 319,
"id" => 319,
"title" => 'cookie-frame2',
"filename" => 'cookie-frame2.png',
]
]
];
<强>解决方案强>
$result = array_filter($variable, function($item) {
return $item['categoryId'] == 13;
});
print_r($result);