if ( isset($_POST['figuresugsubjectposition'])) {
$figuresubject_taxonomies = array( 'figuresugsubjectposition' => $_POST['figuresugsubjectposition']);
$figuresubject_taxonomies_from = array( 'figuresugsubjectfrom' => $_POST['figuresugsubjectfrom']);
foreach ( $figuresubject_taxonomies_from as $tax_name_from => $terms_from ) {
$figuresubject_taxonomies_from = $terms_from;
}
//this will return: Blue, White, Green, Yellow, Black.. etc
foreach ( $figuresubject_taxonomies as $tax_name => $terms ) {
$figuresubject_taxonomies = $terms;
}
//this will return: yes, yes, no, yes, yes.. etc
}
我希望$figuresubject_taxonomies_fnl
只拥有第二个字符串中带有“是”的颜色。在上面的示例中,$figuresubject_taxonomies_fnl
将回显:蓝色,白色,黄色,黑色......等等。
答案 0 :(得分:0)
如果你不介意丢失数组中的原始密钥,你可以这样做:
<?php
$colours = array('black', 'orange', 'yellow');
$yes_no = array('yes', 'no', 'yes');
$combined = array_combine($colours, $yes_no);
$filtered = array_filter($combined, function($x) { return $x === 'yes'; });
$result = array_keys($filtered));
现在$result
将是这个数组:
Array
(
[0] => black
[1] => yellow
)
array_combined
会返回一个新数组,该数组由一系列键(您的颜色)和一系列值(&#39;是&#39;或&#39; no&#39;)组成。 array_filter
仅返回回调函数返回true
的元素。
或者,您可以使用循环:
$result = array();
for ($i = 0; $i < count($colours); ++$i) {
if ($yes_no[$i] === 'yes') $result[] = $colours[$i];
}