例如,我有以下JSON:
{
"status": "OK",
"response": {
"1": {
"name": "Personal Information",
"fields": {
"1": {
"name": "About Me",
"value": ""
},
"4": {
"name": "Discord name",
"value": "TestAccount#1012"
}
}
}
}
}
例如,我是否有机会搜索 Discord名称,并得到 TestAccount#1012 ?
因为ID号码或称为ID的号码有时会“可能”更改。 (我正在尝试一种更简单的方法来阅读我的论坛rest api)
更新: 这是我当前的代码:
<?php {
if ( $_GET ) //Make sure it is a post request.
{
if ( isset( $_GET[ "forum" ] ) && isset( $_GET[ "api" ] ) && isset( $_GET[ "username" ] ) ) //Make sure username and serial are provided.
{
$communityUrl = $_GET[ "forum" ];
$apiKey = $_GET[ "api" ];
$curl = curl_init( $communityUrl . 'api/index.php?/core/members&name=' . $_GET[ "username" ] );
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "{$apiKey}:"
) );
$response = curl_exec( $curl );
header( 'Content-Type: application/json' );
if ( $response === false ) {
// Website down so result is this
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test1'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
} else {
$requestjson = json_decode( $response, true );
$checkuser = $requestjson[ 'results' ];
switch ( json_last_error() ) {
case JSON_ERROR_NONE:
// Check if there is a member
if ( empty( $checkuser ) ) {
$jsonanswer = array(
"status" => 'NOTFOUND',
"response" => 'Test2'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
} else {
$jsonanswer = array(
"status" => 'OK',
"response" => $requestjson[ 'results' ][ 0 ][ 'customFields' ]
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
}
break;
case JSON_ERROR_DEPTH:
// Maximum stack depth exceeded
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test3'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
break;
case JSON_ERROR_STATE_MISMATCH:
// Underflow or the modes mismatch
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test 4'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
break;
case JSON_ERROR_CTRL_CHAR:
// Unexpected control character found
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test 5'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
break;
case JSON_ERROR_SYNTAX:
// Syntax error, malformed JSON
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test 6'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
break;
case JSON_ERROR_UTF8:
// Malformed UTF-8 characters, possibly incorrectly encoded
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test 7'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
break;
default:
// Unknown error
$jsonanswer = array(
"status" => 'FAILED',
"response" => 'Test 8'
);
echo json_encode( $jsonanswer, JSON_PRETTY_PRINT );
break;
}
}
} else {
die( "Access Denied!" ); //Not a post request. Display Access Denied and exit.
}
} else {
die( "Access Denied!" ); //Not a post request. Display Access Denied and exit.
}
}
当前,我正在请求自定义字段,但是在我不知道如何过滤后,如果我搜索 Discord名称,就会得到该值。
答案 0 :(得分:0)
如果name
的值是唯一的,这是一种有趣的方式:
$name = 'Discord name';
echo array_column(current(json_decode($json, true)['response'])['fields'],
'value', 'name')[$name];
如果您已经解码了JSON:
echo array_column(current($array['response'])['fields'], 'value', 'name')[$name];
这是使用name
作为键并以value
作为值来提取新数组,并访问键$name
。