You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中创建示例所示结构的对象?

How to Create the Desired Data Structure in PHP

Hey there! Since you're new to PHP, let's walk through exactly how to build the data structure you shared. That example you provided is essentially a nested collection of key-value pairs and lists—something PHP handles really well with arrays (and optionally objects later on).

Step 1: Break Down the Structure

First, let's map out what we need:

  • A top-level container with three keys: ERROR_CODE, M_USER, and TRANS_LANGUAGE
  • M_USER is a set of user-specific key-value attributes
  • TRANS_LANGUAGE is a list of two items, each with their own C and L keys

Step 2: Build the PHP Code

Here's the full code to create this structure, with comments to explain each part:

<?php
// First, create the M_USER attribute set as an associative array
$userDetails = [
    'CREATE_DATE' => '2018-05-09 13:57:49',
    'CREATE_USER_ID' => 't1074567',
    'FACE_PICTURE_FILE_PATH' => null, // Use null for empty values; change to '<null>' if you need that exact string
    'MAIL_ADDRESS' => 'Testing@gmail.com',
    'NATIVE_LANGUAGE_CD' => 102,
    'REQ_LANGUAGE_CD' => 102,
    'TERMINAL_ID' => 'C71B456F-EA16-4734-8C9B-00B0856143DA',
    'TERMINAL_TYPE' => 1,
    'TOTAL_GRADE' => 0,
    'TRANSLATABLE_FLG' => 1,
    'UPDATE_DATE' => '2018-05-09 13:57:49',
    'UPDATE_USER_ID' => 'tdu1074567',
    'USER_ID' => 'tdu1074567',
    'USER_NAME' => 'Testing',
    'USER_PWD' => 'testing123',
    'VALID_FLG' => 1
];

// Create the TRANS_LANGUAGE list as an indexed array of associative arrays
$languagePairs = [
    ['C' => 102, 'L' => 10203],
    ['C' => 101, 'L' => 10101]
];

// Combine everything into the top-level array
$finalData = [
    'ERROR_CODE' => 0,
    'M_USER' => $userDetails,
    'TRANS_LANGUAGE' => $languagePairs
];

// Optional: Convert to a standard JSON string (matches your example's structure with proper JSON syntax)
echo json_encode($finalData, JSON_PRETTY_PRINT);
?>

Key Notes for PHP Newbies:

  • Associative Arrays: Used for M_USER and each item in TRANS_LANGUAGE—these are key-value pairs, just like the structure you showed.
  • Indexed Arrays: Used for TRANS_LANGUAGE to hold the list of language pairs.
  • Null Values: I used null for FACE_PICTURE_FILE_PATH—if you specifically need the string <null> instead of a real null value, just replace null with '<null>'.
  • JSON Output: The json_encode function turns your PHP array into a valid JSON string, which is probably what you're aiming for given your example. The JSON_PRETTY_PRINT option makes the output formatted and easy to read, just like your sample.

Bonus: Convert to an Object

If you need this as a PHP object instead of an array, you can cast it like this:

$finalDataObj = (object)$finalData;
// Access properties like this: $finalDataObj->ERROR_CODE or $finalDataObj->M_USER->USER_ID

内容的提问来源于stack exchange,提问作者Kyaw Zin Wai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:32:37