为了账号安全,请及时绑定邮箱和手机立即绑定

在 PHP 中查找数组中的元素

在 PHP 中查找数组中的元素

PHP
炎炎设计 2022-10-14 16:15:49
我不知道我是否以最好的方式管理这个数组。我拥有的数组是这样的:$bass = $_POST['bass'];$selected_scale = $_POST['scale'];$major_scales = array     (    array("C","D","E","F","G","A","B","C","D","E","F","G","A","B",),    array("C#","D#","E#","F#","G#","A#","B#","C#","D#","E#","F#","G#","A#","B#",),    array("Db","Eb","F","Gb","Ab","Bb","C","Db","Eb","F","Gb","Ab","Bb","C",),    array("D","E","F#","G","A","B","C#","D","E","F#","G","A","B","C#"),    array("D#","E#","F##","G#","A#","B#","C##","D#","E#","F##","G#","A#","B#","C##"),    array("Eb","F","G","Ab","Bb","C","D","Eb","F","G","Ab","Bb","C","D"),    array("E","F#","G#","A","B","C#","D#","E","F#","G#","A","B","C#","D#"),    array("E#","F##","G##","A#","B#","C##","D##","E#","F##","G##","A#","B#","C##","D##"),    array("Fb","Gb","Ab","Bbb","Cb","Db","Eb","Fb","Gb","Ab","Bbb","Cb","Db","Eb"),    array("F","G","A","Bb","C","D","E","F","G","A","Bb","C","D","E"),    array("F#","G#","A#","B","C#","D#","E#","F#","G#","A#","B","C#","D#","E#"),    array("Gb","Ab","Bb","Cb","Db","Eb","F","Gb","Ab","Bb","Cb","Db","Eb","F"),    array("G","A","B","C","D","E","F#","G","A","B","C","D","E","F#"),    array("G#","A#","B#","C#","D#","E#","F##","G#","A#","B#","C#","D#","E#","F##"),    array("Ab","Bb","C","Db","Eb","F","G","Ab","Bb","C","Db","Eb","F","G"),    array("A","B","C#","D","E","F#","G#","A","B","C#","D","E","F#","G#"),    array("A#","B#","C##","D#","E#","F##","G##","A#","B#","C##","D#","E#","F##","G##"),    array("Bb","C","D","Eb","F","G","A","Bb","C","D","Eb","F","G","A"),    array("B","C#","D#","E","F#","G#","A#","B","C#","D#","E","F#","G#","A#"),    array("B#","C##","D##","E#","F##","G##","A##","B#","C##","D##","E#","F##","G##","A##"),    array("Cb","Db","Eb","Fb","Gb","Ab","Bb","Cb","Db","Eb","Fb","Gb","Ab","Bb")    );$bass是一个字符串,就像数组中的那个。$selected_scale只是一个数字。我想要做的是$bass在其中一个数组中的$selected_scale. 基本上,$bass =  $major_scales[$selected_scale]. 因此,我想创建一个循环以获取之后的元素。但我不知道在这种情况下如何管理这种情况。我在互联网上查看了所有内容并尝试了各种解决方案但没有成功。我想知道我该怎么做。谢谢
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

尝试使用下一个循环:


// if value exists in mentioned index

if (in_array($bass,$major_scales[$selected_scale])){

    // index of that value in that array

    $tmp_ind = array_search($bass,$major_scales[$selected_scale]);


    // length of the array

    $len = count($major_scales[$selected_scale]);


    // store values after this value

    $res = [];

    for ($i=$tmp_ind;$i<$len;$i++){

        $res[$i] = $major_scales[$selected_scale][$i];

    }

}

print_r($res);

演示1

如果您需要$selected_scale在这些数组之一中按索引查找值并在此位置之后存储值:


foreach($major_scales as $ar){

    if ($ar[$selected_scale] == $bass){ 


        // length of the array

        $len = count($ar);


        // store values after this value

        $res = [];

        for ($i=$selected_scale;$i<$len;$i++){

            $res[$i] = $ar[$i];

        }

    }

print_r($res);

演示2


查看完整回答
反对 回复 2022-10-14
  • 1 回答
  • 0 关注
  • 85 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信