如何找到一个键中是否存在特定值,然后在php中的新数组中提取该数组

时间:2017-12-06 23:10:06

标签: php arrays

我有以下数组:

        let wordGuesses = ["woody", "buzz", "sid", "bopee", "yog"];
        let lettersGuessed = [];
        let guessesRemain = 10;
        let randomWord = [Math.floor(Math.random()*wordGuesses.length)];

            let underScore = 
            for (let i = 0; i < randomWord.length; i++){
            wordGuesses.charAt(i) = " _ ";
        };

        // let wordLetter = function (letter){
        //  for (let i = 0; i < letter.length; i++){
        //      letter.charAt(i);
        //      console.log(letter.charAt(i));
        //  }
        // }


        //for (let i = 0; i < wordGuesses.length; i++)

        // window.addEventListener('keyup', function(event){
        //  let userGuess = event.key;
        //  console.log(userGuess);
        //  lettersGuessed.push(userGuess);
        //  console.log(lettersGuessed);
        // })

        document.getElementById('word').innerHTML = underScore;


<!-- Paste the part of the code that shows the problem. (Please indent 4 spaces.) -->


            let underScore = 
            for (let i = 0; i < randomWord.length; i++){
            wordGuesses.charAt(i) = " _ ";
        };

如果数组包含值为17063649000的company_id,我需要在新数组中提取该数组(1),以便我可以进一步操作它。

我制作了大量的代码片段,但我甚至都不接近解决方案。我仍然无法弄清楚我怎么能找到数组中是否存在$ value(17063649000)....更不用说如何从现有数组中提取特定数组(1).... 我最近的尝试是修改它并使其工作,但我仍然没有成功:

Array
(
[0] => Array
    (
        [CODE] => OK
        [company_id] => 170647449000
        [taxnumber] => 944703420
        [name] => SOME NAME
        [title] => S.A

    )

[1] => Array
    (
        [CODE] => OK
        [company_id] => 17063649000
        [taxnumber] => 999033420
        [name] => SOME OTHER NAME
        [title] => ANOTHER DIFFERENT TITLE
    )
)

我总是假的,即使我发送的是存在的价值。

2 个答案:

答案 0 :(得分:1)

return $key替换为return $agent,将===替换为=====也检查类型,这可能是它无效的原因。

答案 1 :(得分:0)

如果您的阵列是$ companies,那么

function getCompany($search_id, $companies) {
    foreach($company in $companies) {
        if ($companies['company_id'] == $search_id) {
            return $company;
        }
    }
    return false;
}

$companies = [...];
$search = 17063649000;
if ($company = getCompany($search, $companies) ) {
    // do something with $company
} else {
    // not found
}