davidannis Posted July 1, 2014 Share Posted July 1, 2014 I am trying to locate an array in an array of arrays: $card_pos = array_search($target, $_SESSION['leitner']['boxes'][($box - 1)]); if ($card_pos !== false) { // Do my stuff here }echo '<pre> oh no -- didn\'t find '; print_r($target); echo "in ";print_r ($_SESSION['leitner']['boxes'][($box + 1)]);echo '<br>leitner';print_r ($_SESSION['leitner']); echo '</pre>'; but I don't find the target in the array though it is clearly there as you can see in the output: oh no -- didn't find Array ( [pos] => v5g [tense] => agerimasu ) in Array ( [0] => Array ( [pos] => v5g [tense] => agerimasu ) ) leitnerArray ( [boxes] => Array ( [1] => Array ( ) [2] => Array ( ) [3] => Array ( [0] => Array ( [pos] => v5g [tense] => agaru ) [1] => Array ( [pos] => v5g [tense] => agarimasu ) [2] => Array ( [pos] => v5g [tense] => agarimasen ) [3] => Array ( [pos] => v5g [tense] => agaranai ) [4] => Array ( [pos] => v5g [tense] => ageru ) [5] => Array ( [pos] => v5g [tense] => ageranai ) [6] => Array ( [pos] => v5g [tense] => agerimasen ) [7] => Array ( [pos] => v5g [tense] => agerimasend ) [8] => Array ( [pos] => v5g [tense] => agereba ) [9] => Array ( [pos] => v5g [tense] => agareba ) [10] => Array ( [pos] => v5g [tense] => agatte ) [11] => Array ( [pos] => v5g [tense] => agette ) ) [4] => Array ( [0] => Array ( [pos] => v5g [tense] => agerimasu ) ) [5] => Array ( ) ) [denom_total] => Array ( [1] => 0 [2] => 0 [3] => 36 [4] => 40 [5] => 40 ) [denom_max] => 40 ) Quote Link to comment https://forums.phpfreaks.com/topic/289356-finding-an-array-in-an-array-of-arrays/ Share on other sites More sharing options...
ginerjm Posted July 1, 2014 Share Posted July 1, 2014 Methinks you are searching too deep. Your third level index - what is the value you have set in $boxes? That is the only element you will do your array search on and that is too low. Quote Link to comment https://forums.phpfreaks.com/topic/289356-finding-an-array-in-an-array-of-arrays/#findComment-1483466 Share on other sites More sharing options...
Solution Jacques1 Posted July 1, 2014 Solution Share Posted July 1, 2014 You got the indexes wrong. Looking at your debugging code, it seems you want to search for $_SESSION['leitner']['boxes'][$box + 1]. But you're actually searching for $_SESSION['leitner']['boxes'][$box - 1]. Note the third index. Quote Link to comment https://forums.phpfreaks.com/topic/289356-finding-an-array-in-an-array-of-arrays/#findComment-1483467 Share on other sites More sharing options...
Psycho Posted July 1, 2014 Share Posted July 1, 2014 You might also consider changing the format of the array. I notice that all of the arrays within "box" 3 have the same 'pos' value. Do all the records in a "box" always have the same 'pos' value? If so, you could simplify thing by formatting the array something like this: leitnerArray ( [boxes] => Array ( [1] => Array ( ) [2] => Array ( ) [3] => Array ( [pos] => v5g [tenses] => Array ( [0] => agaru [1] => agarimasu [2] => agarimasen [3] => agaranai . . . etc. You should try avoiding repeating data such as that. Quote Link to comment https://forums.phpfreaks.com/topic/289356-finding-an-array-in-an-array-of-arrays/#findComment-1483470 Share on other sites More sharing options...
davidannis Posted July 1, 2014 Author Share Posted July 1, 2014 You might also consider changing the format of the array. I notice that all of the arrays within "box" 3 have the same 'pos' value. Do all the records in a "box" always have the same 'pos' value? If so, you could simplify thing by formatting the array something like this: All of the records in a box in this case happen to have the same pos value (part of speech) but in many cases that won't be true. Quote Link to comment https://forums.phpfreaks.com/topic/289356-finding-an-array-in-an-array-of-arrays/#findComment-1483478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.