简体   繁体   English

PHP 多维混合数组

[英]PHP Multidimensional mixed arrays

How Do I access the innermost array?如何访问最里面的数组? the grade is giving me a Notice: Array to string conversion in /scripts/array.php on line 34 grade: Array成绩给了我一个通知:第 34 行 /scripts/array.php 中的数组到字符串转换等级:数组

$data = array();
$data[0] = 78;
$data[1] = 34;
$data[2] = 87;
$student = array(0 => array(
        "Stdno" => "212",
        "name" => "Lorem Ipsum",
        "subject" => "Networking",
        "grade" => $data
    ),
    1 => array(
        "Stdno" => "212",
        "name" => "Jimmy Shu",
        "subject" => "Informatics",
        "grade" => $data
    ),
    2 => array(
        "Stdno" => "212",
        "name" => "Amet Dolor",
        "subject" => "Discrete Combinatorics",
        "grade" => $data
    )
);
foreach ($student as $key => $value) {
    foreach ($value as $key => $value) {
        echo "<b>{$key}</b>: {$value}";
        echo "<br />";
    }
    echo "<br />";
}

First of all, you should really not use $key and $value again (in fact, I thought foreach ($value as $key=>$value) didn't work).首先,你真的不应该再次使用$key$value (事实上​​,我认为foreach ($value as $key=>$value)不起作用)。

Assuming you want to echo the $data element at the same position than in your $student array (ie echo $data[0] for $student[0] ), you should use the first key :假设您想在与$student数组相同的位置回显$data元素(即 echo $data[0]$student[0] ),您应该使用第一个键:

foreach ($student as $key => $value) {
    foreach ($value as $key2 => $value2) {
        echo "<b>{$key2}</b>: ";
        if ($key2 == 'grade')
            echo $value2[$key];
        else
            echo $value2;
        echo "<br />";
    }
    echo "<br />";
}

First, just a comment please avoid using same keys on foreach .首先,请避免在foreach上使用相同的键。 like in your $value .就像你的$value

To fix your issue, it clearly says, it's an array but you try to echo it, you could try to use this instead.为了解决您的问题,它清楚地表明,它是一个数组,但您尝试回应它,您可以尝试使用它。

echo "<b>{$key}</b>: " . json_encode($value);

As stated by @roberto06 you should avoid using same variables for cycles that are nested.正如@roberto06 所述,您应该避免对嵌套循环使用相同的变量。 Those variables will be overwriten by new values.这些变量将被新值覆盖。

To the question: You could check wether $value is string or array问题:您可以检查 $value 是字符串还是数组

is_array($val) || is_string($val)

based on result you could write another foreach cycle or print string.根据结果​​,您可以编写另一个 foreach 循环或打印字符串。 in your second foreach you are foreaching this array:在您的第二个 foreach 中,您正在 foreach 这个数组:

array(
    "Stdno" => "212",
    "name" => "Lorem Ipsum",
    "subject" => "Networking",
    "grade" => $data
)

so the (second) $key will be "Stdno", "name", "subject", "grade" and values will be "212", "Lorem Ipsum", "Networking" (those are strings) and $data (this is array)所以(第二个)$key 将是“Stdno”、“name”、“subject”、“grade”,值将是“212”、“Lorem Ipsum”、“Networking”(这些是字符串)和 $data(这是数组)

to print this array, you need to create new foreach and use it only when $key == "grade" or use implode on it:要打印此数组,您需要创建新的 foreach 并仅在 $key == "grade" 或在其上使用 implode 时才使用它:

 if($key == "grade"){
     $i = implode(', ', $value);
     //print or something
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM