在 PHP 中查找数组中的元素

我不知道我是否以最好的方式管理这个数组。


我拥有的数组是这样的:


$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]. 因此,我想创建一个循环以获取之后的元素。


但我不知道在这种情况下如何管理这种情况。我在互联网上查看了所有内容并尝试了各种解决方案但没有成功。我想知道我该怎么做。谢谢


炎炎设计
浏览 87回答 1
1回答

万千封印

尝试使用下一个循环:// if value exists in mentioned indexif (in_array($bass,$major_scales[$selected_scale])){&nbsp; &nbsp; // index of that value in that array&nbsp; &nbsp; $tmp_ind = array_search($bass,$major_scales[$selected_scale]);&nbsp; &nbsp; // length of the array&nbsp; &nbsp; $len = count($major_scales[$selected_scale]);&nbsp; &nbsp; // store values after this value&nbsp; &nbsp; $res = [];&nbsp; &nbsp; for ($i=$tmp_ind;$i<$len;$i++){&nbsp; &nbsp; &nbsp; &nbsp; $res[$i] = $major_scales[$selected_scale][$i];&nbsp; &nbsp; }}print_r($res);演示1如果您需要$selected_scale在这些数组之一中按索引查找值并在此位置之后存储值:foreach($major_scales as $ar){&nbsp; &nbsp; if ($ar[$selected_scale] == $bass){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // length of the array&nbsp; &nbsp; &nbsp; &nbsp; $len = count($ar);&nbsp; &nbsp; &nbsp; &nbsp; // store values after this value&nbsp; &nbsp; &nbsp; &nbsp; $res = [];&nbsp; &nbsp; &nbsp; &nbsp; for ($i=$selected_scale;$i<$len;$i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$i] = $ar[$i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp;print_r($res);演示2
打开App,查看更多内容
随时随地看视频慕课网APP