我一直在研究API,不可避免地遇到了JSON文件。我真的很喜欢PHP数组如何返回JSON格式,并且其中的信息用于显示有关网页内某人或某人的信息。 (从stipeAPI开始,真棒!)
问题:当我在数组中创建一个数组并尝试print_r()
的内容时,我得到了:
Array
(
[0] => cx_id_00002
[1] => karikanaan08@gmail.com
[2] => vsv
[3] => NULL
[4] => Array
(
[] => sv
)
[5] => Array
(
[] => FC
)
)
[4] =>数组只有1个项目,因此它可以正常显示,但是[5] =>数组有几个不显示的项目。当我var_dump($ _ POST)一切都很好。这是我的代码:
<?php
$customers = "";
$id = "";
$email = "";
$description = "";
$default_source = "";
$metadata = "";
$color_preference = "";
$cards = "";
$card_id = "";
$card_number = "";
$card_name = "";
$card_exp_month = "";
$card_exp_year = "";
$card_address_line1 = "";
$card_address_line2 = "";
$card_address_city = "";
$card_address_state = "";
$card_address_zip = "";
$card_address_country = "";
$colNum = 1;
$number = 1; // Initiallizes the varible $number as an empty string
$data = file_get_contents("stripeInfo.json");
$data = json_decode($data, true);
print_r($data, true);
echo '<pre>';
print_r($_POST);
$info = json_encode($_POST);
print_r($info);
echo '</pre>';
?>
<style type="text/css">
legend {
font-family: sans-serif;
font-weight: bold;
font-size: 12pt;
}
input {
border-radius: 5px;
padding: 5px;
margin: 5px;
}
textarea {
border-radius: 5px;
padding: 5px;
margin: 5px;
}
label {
font-family: sans-serif;
display: inline-block;
width: 130px;
}
fieldset {
display: inline-block;
border-radius: 5px;
}
#submit {
border-radius: 5px;
padding: 10px;
width: 200px;
}
</style>
<!-- form to be displayed -->
<form action="#" method="POST">
<fieldset>
<legend>Customer Account Details:</legend>
<!-- hidden input type - USE "required" for required inputs -->
<input type="hidden" name="id" id="id" value='<?php sprintf('cx_id_%05d', $number++) ?>' ><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="" placeholder="Enter your email"><br>
<label for="description">Describe Yourself:</label>
<textarea name="description" id="description" class="">
</textarea><br>
<!-- hidden input type -->
<input type="hidden" name="default_source" id="default_source" class="" value="NULL"><br>
<label for="metadata[0][color_preference]">Favorite Color:</label>
<input type="text" name="color_preference" id="color_preference" class="" placeholder="Favorite Color"><br>
</fieldset>
<br><br><br>
<fieldset>
<legend>Edit/Update Payment Information</legend>
<!-- hidden input type -->
<input type="hidden" name="cards[0]" class="" value="NULL" ><br>
<label for="cards[1][card_number]">Card Number:</label>
<input type="text" maxlength="16" name="cards[1][card_number]" id="card_number" class="" placeholder="Enter Your Card Number"><br>
<label for="card_name">Name on Card:</label>
<input type="text" name="cards[2][card_name]" id="card_name" class="" placeholder="Name On Card" ><br>
<label for="card_exp_month">Expiration Month:</label>
<input type="text" max-length="2" name="cards[3][card_exp_month]" id="card_exp_month" class="" placeholder="Exp Date ex. 02" ><br>
<label for="cards[3][card_exp_year">Expiration Year:</label>
<input type="text" max-length="4" name="cards[4][card_exp_year]" id="card_exp_year" class="" placeholder="Expiration Year"><br>
<label for="address_line1">Address Line 1:</label>
<input type="text" name="cards[5][address_line1]" id="address_line1" class="" placeholder="Address Line 1" value="" ><br>
<label for="address_line2">Address Line 2:</label>
<input type="text" name="cards[6][address_line2]" id="address_line2" class="" placeholder="Address Line 1" value="" ><br>
<label for="address_city">City:</label>
<input type="text" name="cards[7][address_city]" id="address_city" class="" placeholder="City"><br>
<label for="address_state">State:</label>
<input type="text" name="cards[8][address_state]" id="address_state" class="" placeholder="State" ><br>
<label for="address_zip">Zip:</label>
<input type="text" max-length="5" name="cards[9][address_zip]" id="address_zip" class="" placeholder="Zip"><br>
<label for="address_country">Country:</label>
<input type="text" name="cards[10][address_country]" id="address_country" class="" placeholder="Country"><br>
</fieldset>
<br><br>
<button id="submit" class="" name="submit">Edit/Update Account Details</button>
</form>
<?php
$customers = array(
$id = sprintf('cx_id_%05d', $number++), // $data['customers'][0]['id'];
$email = htmlentities($_POST['email']), // $data['customers'][0]['email'];
$description = htmlentities($_POST['description']), // $data['customers'][0]['description'];
$default_source = htmlentities($_POST['default_source']), // $data['customers'][0]['default_source'];
$metadata = array(
$color_preference => htmlentities($_POST['color_preference'])
),
// $data['customers'][0]['metadata'];
// = $metadata['color_preference'];
$cards = array(
$card_id => sprintf('cx_id_%05d', $number++), // $data['customers'][0]['cards']; // = $cards[0]['id'];
$card_number => htmlentities($_POST['cards'][1]['card_number']), // = $cards[0]['number'];
$card_name => htmlentities($_POST['cards'][2]['card_name']), // $cards[0]['name'];
$card_exp_month => htmlentities($_POST['cards'][3]['card_exp_month']), // $cards[0]['exp_month'];
$card_exp_year => htmlentities($_POST['cards'][4]['card_exp_year']), // $cards[0]['exp_year'];
$card_address_line1 => htmlentities($_POST['cards'][5]['address_line1']), // $cards[0]['address_line1'];
$card_address_line2 => htmlentities($_POST['cards'][6]['address_line2']), // $cards[0]['address_line2'];
$card_address_city => htmlentities($_POST['cards'][7]['address_city']), // $cards[0]['address_city'];
$card_address_state => htmlentities($_POST['cards'][8]['address_state']), // $cards[0]['address_state'];
$card_address_zip => htmlentities($_POST['cards'][9]['address_zip']), // $cards[0]['address_zip'];
$card_address_country => htmlentities($_POST['cards'][10]['address_country']), // $cards[0]['address_country'];
),
);
echo '<pre>';
print_r($customers);
echo 'My email is ' . $email . " and my card number is " . $card_number;
echo '</pre>';
/*
$customers = array(
"id" => $id,
"email" => $email,
"description" => $description,
"default_source" => $default_source,
"metadata" => $metadata = array(
"color_preference" => $color_preference),
"cards" => $cards = array(
"id" => $card_id,
"number" => $card_number,
"name" => $card_name,
"exp_month" => $card_exp_month,
"exp_year" => $card_exp_year,
"address_line1" => $card_address_line1,
"address_line2" => $card_address_line2,
"address_city" => $card_address_city,
"address_state" => $card_address_state,
"address_zip" => $card_address_zip,
"address_country" => $card_address_country),
);
*/
$myJson = json_encode($customers);
var_dump($myJson);
?>
我知道现在很乱。我只是想了解JSON / PHP交互以及如何最好地将其实现到我的站点中。
我的目标:使用PHP脚本制作JSON文件,其内容如下:
$customers = array(
$id => "incremented ID",
$email => "$_POST['whatever the user puts in']", etc!
理想情况下,我会将其保存到JSON文件中,并能够为我的网站使用用户输入的数据。我知道我已经接近了,但是我现在就觉得自己很茫然。
[编辑!]:现在,我已经拥有了!请帮助!
{ "part_1":{
"section_1":"editors_and_enviorments",
"section_2":"working_backwards",
"section_3":"prep_work",
"section_4":{
"section_4.1":"career_facts",
"section_4.2":"encouraging_word"
}
},
"0":{
"part_2 ":{
"section_1":"html5",
"section_2":"css3",
"section_3":"javaScript",
"section_4":"jQuery_and_UI",
"section_5":"php7",
"section_6":"mySql",
"section_7":"bootstrap",
"section_8":"apis_and_ajax",
"section_9":"json",
"section_10":"stripeApi"
},
"part_3":{
"section_1":"final_project_info",
"section_2":"mobileApp"
}
}
}
我正在使用App.js-我想在<li>
中以<ul>
的形式遍历所有部分...我知道这是可能的,但是怎么办?
谢谢。