Jump to content

Finding an Array in an Array of Arrays


Go to solution Solved by Jacques1,

Recommended Posts

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
)
Link to comment
https://forums.phpfreaks.com/topic/289356-finding-an-array-in-an-array-of-arrays/
Share on other sites

  • Solution

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.