跳过数组,然后继续最后一个给定的数据 - php



所以我有这个代码:

<?php
$prefectures = array();
$prefectures = array(array("id"=>"01","name"=>"北海道"),array("id"=>"02","name"=>"青森県"),array("id"=>"03","name"=>"岩手県"),array("id"=>"04","name"=>"宮城県"),array("id"=>"05","name"=>"秋田県"),array("id"=>"06","name"=>"山形県"),array("id"=>"07","name"=>"福島県"),array("id"=>"08","name"=>"茨城県"),array("id"=>"09","name"=>"栃木県"),array("id"=>"10","name"=>"群馬県"),array("id"=>"11","name"=>"埼玉県"),array("id"=>"12","name"=>"千葉県"),array("id"=>"13","name"=>"東京都"),array("id"=>"14","name"=>"神奈川県"),array("id"=>"15","name"=>"新潟県"),array("id"=>"16","name"=>"富山県"),array("id"=>"17","name"=>"石川県"),array("id"=>"18","name"=>"福井県"),array("id"=>"19","name"=>"山梨県"),array("id"=>"20","name"=>"長野県"),array("id"=>"21","name"=>"岐阜県"),array("id"=>"22","name"=>"静岡県"),array("id"=>"23","name"=>"愛知県"),array("id"=>"24","name"=>"三重県"),array("id"=>"25","name"=>"滋賀県"),array("id"=>"26","name"=>"京都府"),array("id"=>"27","name"=>"大阪府"),array("id"=>"28","name"=>"兵庫県"),array("id"=>"29","name"=>"奈良県"),array("id"=>"30","name"=>"和歌山県"),array("id"=>"31","name"=>"鳥取県"),array("id"=>"32","name"=>"島根県"),array("id"=>"33","name"=>"岡山県"),array("id"=>"34","name"=>"広島県"),array("id"=>"35","name"=>"山口県"),array("id"=>"36","name"=>"徳島県"),array("id"=>"37","name"=>"香川県"),array("id"=>"38","name"=>"愛媛県"),array("id"=>"39","name"=>"高知県"),array("id"=>"40","name"=>"福岡県"),array("id"=>"41","name"=>"佐賀県"),array("id"=>"42","name"=>"長崎県"),array("id"=>"43","name"=>"熊本県"),array("id"=>"44","name"=>"大分県"),array("id"=>"45","name"=>"宮崎県"),array("id"=>"46","name"=>"鹿児島県"),array("id"=>"47","name"=>"沖縄県"));
$industries = array();
$industries=array(array("id"=>"1","name"=>"グルメ"),array("id"=>"2","name"=>"住まい"),array("id"=>"3","name"=>"医療・健康・介護"),array("id"=>"4","name"=>"美容・ファッション"),array("id"=>"5","name"=>"暮らし"),array("id"=>"6","name"=>"ショッピング"),array("id"=>"7","name"=>"ペット"),array("id"=>"8","name"=>"旅行宿泊"),array("id"=>"9","name"=>"ビジネス"),array("id"=>"10","name"=>"教育・習い事"),array("id"=>"11","name"=>"趣味"),array("id"=>"12","name"=>"公共機関・団体"),array("id"=>"13","name"=>"レジャー・スポーツ"),array("id"=>"14","name"=>"冠婚葬祭・イベント"),array("id"=>"15","name"=>"自動車・バイク"));
$completed_steps = array();
$completed_steps = array(array("pref"=>01,"cat_id"=>1),array("pref"=>01,"cat_id"=>2),array("pref"=>01,"cat_id"=>3));
echo "<pre>";
print_r($completed_steps);
foreach ( $prefectures as $prefecture ) {
    $prefectureId = $prefecture["id"];
    foreach ( $industries as $industry ) {
        $industryId = $industry["id"];
        foreach ($completed_steps as $inc) {
            $var = $inc;
            if (in_array($prefectureId, $var)) {
                echo "We will pass prefectureId: ";
                print_r($prefectureId);
                echo "</br>";
                if (in_array($industryId, $var)) {
                    echo "We will pass industryId: ";
                    print_r($industryId);
                    echo "</br></br>";
                    echo "P   A   S   S   E   D";
                    echo "</br></br>";
                    continue;
                }
            }
        }
        echo "Success! Will now continue... ";
        print_r("pref: " . $prefectureId . "  ind: " . $industryId);
        echo "</br></br>";
    }
}
?>

有这个给定的数组,$completed_steps .因此,最后一个$completed_steps数组是我接下来要继续的地方。

在此示例中,$completed_steps上的最后一个数据是 pref = 01cat_id = 3 。我想在pref = 01cat_id 4中继续循环.

我现在的问题是我不知道如何使用in_array进行检查。

当前的结果会给我:

We will pass prefectureId: 01
We will pass industryId: 1
P   A   S   S   E   D
We will pass prefectureId: 01
We will pass industryId: 1
P   A   S   S   E   D
We will pass prefectureId: 01
We will pass industryId: 1
P   A   S   S   E   D
Success! Will now continue... pref: 01  ind: 1
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass industryId: 2
P   A   S   S   E   D
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 2
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass industryId: 3
P   A   S   S   E   D
Success! Will now continue... pref: 01  ind: 3
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 4
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 5
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 6
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 7
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 8
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 9
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 10
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 11
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 12
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 13
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 14
We will pass prefectureId: 01
We will pass prefectureId: 01
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 15
We will pass prefectureId: 02
We will pass industryId: 1
P   A   S   S   E   D
Success! Will now continue... pref: 02  ind: 1
We will pass prefectureId: 02
We will pass industryId: 2
P   A   S   S   E   D
Success! Will now continue... pref: 02  ind: 2
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 3
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 4
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 5
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 6
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 7
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 8
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 9
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 10
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 11
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 12
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 13
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 14
We will pass prefectureId: 02
Success! Will now continue... pref: 02  ind: 15
We will pass prefectureId: 03
We will pass industryId: 1
P   A   S   S   E   D
Success! Will now continue... pref: 03  ind: 1
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 2
We will pass prefectureId: 03
We will pass industryId: 3
P   A   S   S   E   D
Success! Will now continue... pref: 03  ind: 3
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 4
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 5
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 6
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 7
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 8
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 9
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 10
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 11
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 12
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 13
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 14
We will pass prefectureId: 03
Success! Will now continue... pref: 03  ind: 15
Success! Will now continue... pref: 04  ind: 1
Success! Will now continue... pref: 04  ind: 2
Success! Will now continue... pref: 04  ind: 3
Success! Will now continue... pref: 04  ind: 4
Success! Will now continue... pref: 04  ind: 5
Success! Will now continue... pref: 04  ind: 6
Success! Will now continue... pref: 04  ind: 7
Success! Will now continue... pref: 04  ind: 8
Success! Will now continue... pref: 04  ind: 9
Success! Will now continue... pref: 04  ind: 10
Success! Will now continue... pref: 04  ind: 11
Success! Will now continue... pref: 04  ind: 12
Success! Will now continue... pref: 04  ind: 13
Success! Will now continue... pref: 04  ind: 14
Success! Will now continue... pref: 04  ind: 15
Success! Will now continue... pref: 05  ind: 1
Success! Will now continue... pref: 05  ind: 2
Success! Will now continue... pref: 05  ind: 3
Success! Will now continue... pref: 05  ind: 4
Success! Will now continue... pref: 05  ind: 5
Success! Will now continue... pref: 05  ind: 6
Success! Will now continue... pref: 05  ind: 7
Success! Will now continue... pref: 05  ind: 8
Success! Will now continue... pref: 05  ind: 9
Success! Will now continue... pref: 05  ind: 10
Success! Will now continue... pref: 05  ind: 11
Success! Will now continue... pref: 05  ind: 12
Success! Will now continue... pref: 05  ind: 13
Success! Will now continue... pref: 05  ind: 14
Success! Will now continue... pref: 05  ind: 15
Success! Will now continue... pref: 06  ind: 1
Success! Will now continue... pref: 06  ind: 2
Success! Will now continue... pref: 06  ind: 3
Success! Will now continue... pref: 06  ind: 4
Success! Will now continue... pref: 06  ind: 5
Success! Will now continue... pref: 06  ind: 6
Success! Will now continue... pref: 06  ind: 7
Success! Will now continue... pref: 06  ind: 8
Success! Will now continue... pref: 06  ind: 9
Success! Will now continue... pref: 06  ind: 10
Success! Will now continue... pref: 06  ind: 11
Success! Will now continue... pref: 06  ind: 12
Success! Will now continue... pref: 06  ind: 13
Success! Will now continue... pref: 06  ind: 14
AND SO AND AND SO FORTH...

我的预期结果是:

We will pass prefectureId: 01
We will pass industryId: 1
P   A   S   S   E   D
We will pass prefectureId: 01
We will pass industryId: 2
P   A   S   S   E   D
We will pass prefectureId: 01
We will pass industryId: 3
P   A   S   S   E   D
We will pass prefectureId: 01
Success! Will now continue... pref: 01  ind: 4
Success! Will now continue... pref: 01  ind: 5
Success! Will now continue... pref: 01  ind: 6
Success! Will now continue... pref: 01  ind: 7
Success! Will now continue... pref: 01  ind: 8
Success! Will now continue... pref: 01  ind: 9
Success! Will now continue... pref: 01  ind: 10
Success! Will now continue... pref: 01  ind: 11
Success! Will now continue... pref: 01  ind: 12
Success! Will now continue... pref: 01  ind: 13
Success! Will now continue... pref: 01  ind: 14
Success! Will now continue... pref: 01  ind: 15
AND SO ON AND SO FORTH...

解决了!

foreach ( $prefectures as $prefecture ) {
    $prefectureId = $prefecture["id"];
    foreach ( $industries as $industry ) {
        $industryId = $industry["id"];
        if (in_array(array("pref"=>$prefectureId,"cat_id"=>$industryId), $completed_steps)) {
            echo "We will pass prefectureId: ";
            print_r($prefectureId);
            echo "</br>";
            echo "We will pass industryId: ";
            print_r($industryId);
            echo "</br>";
            continue;
        }
        echo "Success! Will now continue... ";
        print_r("pref: " . $prefectureId . "  ind: " . $industryId);
        echo "</br></br>";
    }
}

最新更新