在不知道键索引的情况下获取数组键的第一个值

时间:2019-09-20 03:35:03

标签: php arrays

   $responseData = json_decode($response);
print_r($responseData->errors);

即使我不知道第一个键的值是多少,如何获得第一个键和[0]项?这样的时间?我想获取文本“姓氏字段是必需的。”

print_r($responseData->errors[0][0]);


stdClass Object
(
    [first_name] => Array
        (
            [0] => The first name field is required.
        )

    [last_name] => Array
        (
            [0] => The last name field is required.
        )
)

6 个答案:

答案 0 :(得分:0)

这样做,

var_dump(current($array));
var_dump(key($array));

请注意,如果数组内部指针未指向第一个元素,则可以先使用reset($array);对其进行重置。

答案 1 :(得分:0)

PHP具有可以帮助您array_key_first的功能:

print_r($ responseData-> errors [array_key_first($ responseData-> errors)] [0]);

答案 2 :(得分:0)

嗨,您可以尝试使用 array_keys 来获取所有键的数组,这是PHP.net的示例代码

<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
print_r(array_keys($array));
?>

输出

Array
(
    [0] => 0
    [1] => color
)
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
Array
(
    [0] => color
    [1] => size
)

要仅获取第一个密钥,可以使用 array_key_first 示例代码

<?php
$array = ['a' => 1, 'b' => 2, 'c' => 
$firstKey = array_key_first($array);
var_dump($firstKey);
?>

输出

Above code will output a string 'a'

有关更多详细信息,请访问 https://www.php.net/manual/en/function.array-key-first.php https://www.php.net/manual/en/function.array-keys.php

答案 3 :(得分:0)

formPages

您可以使用

let formPages = [{
    name: "email",
    label: "Email",
    value: ""
}, {
    name: "password",
    label: "Password",
    value: ""
}];

如果不需要键名。

答案 4 :(得分:0)

我的测试数据

$respErrors = (object)[
  'first_name' => ['The first name field is required.'],
  'last_name' => ['The last name field is required.'],
];

如果只需要第一个错误文本,您可以这样做

$firstField = reset($respErrors)[0];

最后一个字段的信息

$lastField = end($respErrors)[0];

如果需要键和值,也可以使用foreach

foreach($respErrors as $key => $value){
  echo "key: ".$key." value[0]: ".$value[0]."<br>\n";
  break;
}

要获取所有值,请删除带有break的行。

答案 5 :(得分:0)

<?php

$json =<<<JSON
[
    {
        "first_name": [
            "The first name field is required."
        ]
    },
    {
        "last_name": [
            "The last name field is required."
        ]
    }
]
JSON;

$response = json_decode($json, true);

foreach($response as $item) {
    foreach($item as $key => $array) {
        printf("Key: '%s', First error message: '%s'\n", $key, $array[0]);
    }
}

输出:

Key: 'first_name', First error message: 'The first name field is required.'
Key: 'last_name', First error message: 'The last name field is required.'

鉴于您的回答,减少一层的使用可能会更容易:

$keyed_messages = array_merge(...$response);
var_export($keyed_messages);

输出:

array (
    'first_name' => 
    array (
      0 => 'The first name field is required.',
    ),
    'last_name' => 
    array (
      0 => 'The last name field is required.',
    ),
  )

然后循环遍历您可以执行的操作,例如first_name消息:

 foreach($keyed_messages['first_name'] as $message) {
     // Something.
 }