PHP数组结构中的未定义索引

时间:2018-08-21 04:41:46

标签: php

我在变量$ theArray中有这样的数组结构:

Array
(
    [name] => Me1
    [email] => me_1@email.com
    [week2018012920180204] => Approved
    [week2018020520180211] => Approved
    [week2018021220180218] => Approved
    [week2018021920180225] => Approved
    [week2018022620180304] => Approved
    [data] => 5
)

Array
(
    [name] => Me2
    [email] => me_2@email.com
    [week2018012920180204] => Approved
    [week2018020520180211] => Approved
    [week2018021220180218] => Approved
    [week2018021920180225] => Approved
    [week2018022620180304] => Approved
    [data] => 5
)

但是当我尝试循环获取价值时,通过以下代码:

foreach ($theArray as $key => $val)
{
   print_r($val['week2018012920180204'])
}

它总是抛出错误:

  

未定义索引:week2018012920180204

当我尝试从名称,电子邮件或数据中获取其他价值时,总是成功而没有错误。

请问关于我的问题的任何建议?

预先感谢

1 个答案:

答案 0 :(得分:1)

您的问题看起来不正确。 如果您的数组是这样的,则可以获得这样的结果,

buildscript {
    ext {
        kotlinVersion = '1.2.51'
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.kotlinexample'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('com.fasterxml.jackson.module:jackson-module-kotlin')
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

但是,如果您的数组是这样的,则可以获得这样的结果,

<?php
$theArray = array
(
    "name" => "Me1",
    "email" => "me_1@email.com",
    "week2018012920180204" => "Approved",
    "week2018020520180211" => "Approved",
    "week2018021220180218"=> "Approved",
    "week2018021920180225" => "Approved",
    "week2018022620180304" => "Approved",
    "data" => 5
);
print_r($theArray['week2018012920180204']);

?>