遍历数组的对象值并根据值添加到新数组?

时间:2014-04-29 20:50:50

标签: php arrays foreach

好的我有以下内容:

 [0]=>
        object(stdClass)#1026 (9) {
          ["Level"]=>
          object(stdClass)#1027 (2) {
            ["ID"]=>
            int(1)
            ["Name"]=>
            string(17) "Beginner"
          }
          ["ID"]=>
          int(1)
          ["Name"]=>
          string(23) "Salsa - Beginner/Novice"
          ["Description"]=>
          string(47) "Learn fun and easy basics for Salsa."
          ["Prereq"]=>
          string(37) "No prior lessons required."
          ["Notes"]=>
          string(0) ""
          ["LastUpdated"]=>
          string(22) "2012-11-30T18:57:29.91"
          ["Program"]=>
          object(stdClass)#1028 (3) {
            ["ID"]=>
            int(22)
            ["Name"]=>
            string(13) "Group Classes"
            ["ScheduleType"]=>
            string(6) "DropIn"
          }
          ["SessionType"]=>
          object(stdClass)#1029 (4) {
            ["DefaultTimeLength"]=>
            NULL
            ["ProgramID"]=>
            int(22)
            ["ID"]=>
            int(6)
            ["Name"]=>
            string(19) "Social Group Lesson"
          }
        }
        [1]=>
        object(stdClass)#1030 (9) {
          ["Level"]=>
          object(stdClass)#1031 (2) {
            ["ID"]=>
            int(1)
            ["Name"]=>
            string(17) "Absolute Beginner"
          }
          ["ID"]=>
          int(2)
          ["Name"]=>
          string(26) "Two Step - Beginner/Novice"
          ["Description"]=>
          string(51) "Learn fun & easy basics for Two Step"
          ["Prereq"]=>
          string(37) "No prior lessons required."
          ["Notes"]=>
          string(0) ""
          ["LastUpdated"]=>
          string(22) "2012-11-30T19:02:40.29"
          ["Program"]=>
          object(stdClass)#1032 (3) {
            ["ID"]=>
            int(22)
            ["Name"]=>
            string(13) "Group Classes"
            ["ScheduleType"]=>
            string(6) "DropIn"
          }
          ["SessionType"]=>
          object(stdClass)#1033 (4) {
            ["DefaultTimeLength"]=>
            NULL
            ["ProgramID"]=>
            int(22)
            ["ID"]=>
            int(6)
            ["Name"]=>
            string(19) "Social Group Lesson"
          }
       }
        [3]=>
        object(stdClass)#1030 (9) {
          ["Level"]=>
          object(stdClass)#1031 (2) {
            ["ID"]=>
            int(1)
            ["Name"]=>
            string(17) "Absolute Beginner"
          }
          ["ID"]=>
          int(2)
          ["Name"]=>
          string(26) "Two Step - Beginner/Novice"
          ["Description"]=>
          string(51) "Learn fun & easy basics for Two Step"
          ["Prereq"]=>
          string(37) "No prior lessons required."
          ["Notes"]=>
          string(0) ""
          ["LastUpdated"]=>
          string(22) "2012-11-30T19:02:40.29"
          ["Program"]=>
          object(stdClass)#1032 (3) {
            ["ID"]=>
            int(22)
            ["Name"]=>
            string(13) "Group Classes"
            ["ScheduleType"]=>
            string(6) "DropIn"
          }
          ["SessionType"]=>
          object(stdClass)#1033 (4) {
            ["DefaultTimeLength"]=>
            NULL
            ["ProgramID"]=>
            int(22)
            ["ID"]=>
            int(6)
            ["Name"]=>
            string(19) "Social Group Lesson"
          }
       }   
        [4]=>
        object(stdClass)#1030 (9) {
          ["Level"]=>
          object(stdClass)#1031 (2) {
            ["ID"]=>
            int(1)
            ["Name"]=>
            string(17) "Absolute Beginner"
          }
          ["ID"]=>
          int(2)
          ["Name"]=>
          string(26) "Two Step - Beginner/Novice"
          ["Description"]=>
          string(51) "Learn fun & easy basics for Two Step"
          ["Prereq"]=>
          string(37) "No prior lessons required."
          ["Notes"]=>
          string(0) ""
          ["LastUpdated"]=>
          string(22) "2012-11-30T19:02:40.29"
          ["Program"]=>
          object(stdClass)#1032 (3) {
            ["ID"]=>
            int(22)
            ["Name"]=>
            string(13) "Group Classes"
            ["ScheduleType"]=>
            string(6) "DropIn"
          }
          ["SessionType"]=>
          object(stdClass)#1033 (4) {
            ["DefaultTimeLength"]=>
            NULL
            ["ProgramID"]=>
            int(22)
            ["ID"]=>
            int(6)
            ["Name"]=>
            string(19) "Social Group Lesson"
          }
       }           

如何将此值转换为["Name"]=> string(17) "Absolute Beginner"检查值是否为"绝对初学者"如果它返回整个数组IE:

[4]=>
        object(stdClass)#1030 (9) {
          ["Level"]=>
          object(stdClass)#1031 (2) {
            ["ID"]=>
            int(1)
            ["Name"]=>
            string(17) "Absolute Beginner"
          }
          ["ID"]=>
          int(2)
          ["Name"]=>
          string(26) "Two Step - Beginner/Novice"
          ["Description"]=>
          string(51) "Learn fun & easy basics for Two Step"
          ["Prereq"]=>
          string(37) "No prior lessons required."
          ["Notes"]=>
          string(0) ""
          ["LastUpdated"]=>
          string(22) "2012-11-30T19:02:40.29"
          ["Program"]=>
          object(stdClass)#1032 (3) {
            ["ID"]=>
            int(22)
            ["Name"]=>
            string(13) "Group Classes"
            ["ScheduleType"]=>
            string(6) "DropIn"
          }
          ["SessionType"]=>
          object(stdClass)#1033 (4) {
            ["DefaultTimeLength"]=>
            NULL
            ["ProgramID"]=>
            int(22)
            ["ID"]=>
            int(6)
            ["Name"]=>
            string(19) "Social Group Lesson"
          }
       }

并将其保存到新数组中...... 这让我很生气任何帮助都很有帮助.. 到目前为止我有:

    foreach ($resultTest as $key => $value) {
    # code...
    foreach ($value as $keysub => $valuesub) {
        # code...

        if(is_object($valuesub)){
            foreach ($valuesub as $keysubsub => $valuesubsub) {
                # code...
                    echo "<br>";
                    echo $keysubsub;
            }
        }else{
//          echo "<br>";
//          echo $keysub;
        }
    }
}

但是我迷失在一个深深的... ...

1 个答案:

答案 0 :(得分:2)

假设您正在搜索的数组名为$lessons

$result = array();

foreach ($lessons as $lesson) {
    if ($lesson->Level->Name === 'Absolute Beginner') {
        $result[] = $lesson;
        break;
    }
}

var_dump($result);