0

So here is an array:

$prefectures = Array
    (
    [0] => Array
        (
            [id] => 1
            [name] => グルメ
        )

    [1] => Array
        (
            [id] => 2
            [name] => 住まい
        )

    [2] => Array
        (
            [id] => 3
            [name] => 医療・健康・介護
        )
    [3] => Array
    (
        [id] => 4
        [name] => 美容・ファッション
    )
    [4] => Array
    (
        [id] => 5
        [name] => 暮らし
    )

    [5] => Array
    (
        [id] => 6
        [name] => ショッピング
    )

    [6] => Array
    (
        [id] => 7
        [name] => ペット
    )
)

I want to set the key pointer according to a variable inside a foreach loop.

Here is my function:

$setKey = '6';

foreach ( $prefectures as $prefecture ) {
            $prefectureId = $prefecture["id"];
            // TODO:
            // set the internal array pointer based on $setKey
}

Suppose the current key pointer is at the start, I want to move to the 4th array key.

How could it be done right?

andil01
  • 377
  • 4
  • 19
  • what do you mean by internal array pointer ? i don't get it – Frankich Oct 02 '17 at 11:42
  • 2
    Perhaps you mean skip, in the loop, data to directly access let's say, ```$prefectures[6]``` ? Maybe ```$prefectures[$setKey]``` ? – NaeiKinDus Oct 02 '17 at 12:08
  • @NaeiKinDus could you show me how does it look? – andil01 Oct 02 '17 at 12:11
  • Not really no, I'm sorry but I don't understand what you need. Could you rephrase your issue without referring to your code ? What do you want to achieve functionally speaking ? – NaeiKinDus Oct 02 '17 at 12:47
  • @NaeiKinDus I want to move the current array pointer into say for example, array key 4. The array starts at 0, but I want to skip to 4 directly. – andil01 Oct 02 '17 at 12:57
  • Using your array, if $setKey = 4 then you want the loop to start processing data on the record with an id of 5 and a name set to "暮らし" ? If so, you could simply use a for loop instead, like this: ```$count = count($prefectures); for ($i = $setKey; $i < count($prefectures); $i++) { $pref = $prefectures[$i]; // your code; }``` – NaeiKinDus Oct 02 '17 at 15:41

0 Answers0