如果其他字符串值包含yes,则获取字符串的值

时间:2014-08-23 06:21:43

标签: php string comparison

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将回显:蓝色,白色,黄色,黑色......等等。

1 个答案:

答案 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];
}