Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-返回与数组中的键值匹配的数据_Php_Arrays_Multidimensional Array - Fatal编程技术网

php-返回与数组中的键值匹配的数据

php-返回与数组中的键值匹配的数据,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在构建一个职业页面,从托管的json文件中读取可用职位。我很难将数据正确地过滤到超过值的值 此示例在与$filter数组中设置的键/值进行比较时,尝试仅输出$data数组中的匹配值: 我发现filterData函数将返回与$filter数组中的任何设置键/值匹配的结果。因此,如果部门和地点都匹配,我可以获得重复条目 如何进行调整,以便只获得所有$filter键/值匹配的数组输出 谢谢-我已经花了一天的时间在这个问题上,现在还没有进一步的答案,所以如果能在正确的方向上提供帮助,我将不胜感激。注

我正在构建一个职业页面,从托管的json文件中读取可用职位。我很难将数据正确地过滤到超过值的值

此示例在与$filter数组中设置的键/值进行比较时,尝试仅输出$data数组中的匹配值:

我发现filterData函数将返回与$filter数组中的任何设置键/值匹配的结果。因此,如果部门和地点都匹配,我可以获得重复条目

如何进行调整,以便只获得所有$filter键/值匹配的数组输出


谢谢-我已经花了一天的时间在这个问题上,现在还没有进一步的答案,所以如果能在正确的方向上提供帮助,我将不胜感激。

注意:我可能不太理解你的问题,但是,鉴于还没有答案,我决定试试。 如果我猜错了,请告诉我,我会相应地修改我的答案

现在我将忽略你的cleanString和sanitizeString函数。我相信在解决作业过滤部分后,您可以轻松实现这些功能

过滤任何数据数组,是您最好的朋友

array_filter迭代数组中的每个值,并将它们传递给回调函数。如果回调函数返回true,则数组中的当前值将返回到结果数组中。保留数组键

在array_filter回调中,循环过滤规则,并检查$job是否与它的属性匹配。 如果所有规则都匹配,我们将返回true,否则返回false,这将从数组中过滤掉该作业


尽管你的问题措辞很好,但你的问题并不完全清楚。你有两个问题吗?第一,你得到了重复的,第二,你的过滤器不够具体?如果是这样,您需要两个修复…顺便说一句,如果这只是一个重复的问题,那么数组_是唯一的。你可能已经试过了。@Kevin_Kinsey-问题是我最终得到的结果可能与某个部门以及任何与设置位置匹配的结果相匹配,而不是两者都完全匹配。如果您勾选此示例,它将显示项目管理的所有条目和伦敦的所有条目,由于错误,显示的条目以Manchestera为基础。您只需要在部门和地点上进行匹配吗?或者这是否需要能够使用任意数量的条件,即数组$filter可能有2、3、4或$n个成员?是的。这应该适用于任何数量的标准。哇,谢谢。还有人建议使用我以前从未使用过的array_过滤器,所以我将对此进行深入研究。再次感谢,这似乎返回了预期的输出:D
<?php
    /* ----------------------
    Dump the output
    ---------------------- */
    function dump($data) {
        if(is_array($data)) { //If the given variable is an array, print using the print_r function.
            print "<pre>\n";
            print_r($data);
            print "</pre>";
        } elseif (is_object($data)) {
            print "<pre>\n";
            var_dump($data);
            print "</pre>";
        } else {
            print "=========&gt; ";
            var_dump($data);
            print " &lt;=========";
        }
    }

    /* ----------------------
    Sanitize a string to url friendly
    ---------------------- */
    function cleanString($str) {
        $removetags = array("'", '"', ',', '.', '?', '& ', '&amp; ', '/', '#', '@','(',')');
        $removespace[] = ' ';
        $notags = str_replace($removetags,"",$str);
        $nospaces = strtolower(str_replace($removespace,"-",$notags));
        return preg_replace('~-{2,}~', '-', $nospaces);
    }
    function sanitizeString($val) {
        if(is_array($val)) {
            $array = array() ;
            foreach ($val as $key => $value) {
                $array[$key] = cleanString($value);
            }
            return $array;
        } else {
            $result = cleanString($val);
        }
        return $result;
    }

    /* ----------------------
    Filter the array data, should match all values;=
    ---------------------- */
    function filterData($array, $filter) {
        $result = array();
        foreach ($filter as $gk => $gv) {
            foreach ($array as $key => $value) {
                if (array_key_exists($gk, $value) && !empty($value[$gk])) {
                    if(is_array($value[$gk])) {
                        $child = array_search($gv, sanitizeString($value[$gk]));
                        if(!empty($value[$gk][$child])) {
                            $theValue = sanitizeString($value[$gk][$child]);
                            if ($theValue ===  $gv ) {
                                array_push($result,$value);
                            }
                        }
                    } else {
                        $theValue = sanitizeString($value[$gk]);
                        if ($theValue ===  $gv ) {
                            array_push($result,$value);
                        }
                    }
                }
            }
        }
        return (!empty($result)) ? $result : $array;
    };

    // sample data
    $data = '{"jobs":[{"department":"sales","location":{"country":"United States","country_code":"US","region":"New York","region_code":"NY","city":"New York","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Leeds","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Manchester","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":"Customer Success","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}}]}';

    $decode = json_decode($data, true);


    // sample filter - values should be lowercase, and sanitised;
    $filter = array (
        'department' => 'project-management',
        'location' => 'london'
    );

    $filterdata = filterData($decode['jobs'], $filter);

    $i = 1;
    foreach ($filterdata as $res) {
        echo $i . '<br />';
        echo $res['department'] . '<br />';
        echo $res['location']['city'] . '<hr />';
        $i++;
    }
?>
$data = '{"jobs":[{"department":"sales","location":{"country":"United States","country_code":"US","region":"New York","region_code":"NY","city":"New York","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Leeds","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Manchester","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":"Customer Success","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}}]}';

    $decode = json_decode($data, true);

    $filters = array(
        'department' => 'Project Management',
        'location' => 'London'
    );

    /**
     * We will use array_filter()
     * to filter out jobs that doesn't meet our rules
     */
    $filterData = array_filter($decode['jobs'], function($job) use($filters) {
        $matched = [];

        foreach ($filters as $col => $val) {
            if (isset($job[$col]) === false) {
                $matched[0] = '';
                continue;
            }

            /**
             * I am assigning filter condition to $matched key
             * PHP as a dynamic language, will convert boolean to integer
             * 
             * If you don't like this approach, you can try assigning to value
             */
            if (is_array($job[$col])) {
                $matched[in_array($val, $job[$col])] = '';
            } else {
                $matched[($job[$col] === $val)] = '';
            }
        }

        /**
         * If any of our rules returned false
         * We will return false too
         * and array_filter will filter out this job
         */
        return isset($matched[0]) === false;
    });

    print_r($filterData);