php 中的多维数组重新格式化
Multidimensional Array Reformatting in php
尝试实现我想要的结果。但不幸的是,在我绞尽脑汁几个小时后无法实现。到目前为止,我尝试了什么:
$post_data = array(
'email_address' => "sda",
'status_if_new' => 'subscribed',
'merge_fields' => array(
'FNAME' => "asd",
'LNAME' => "sad")
);
$st= "{\"Field0\":\"Value0\",\"Field1\":\"Value1\"}";
$string = str_replace(array('{','}','"'),'',$st);
$custom_array=explode(",",$string);
$len= count($custom_array);
for($i=0;$i<$len;$i++) {
$fields = explode(":", $custom_array[$i]);
array_push($post_data["merge_fields"],$fields);
}
echo "<pre>";
var_dump($post_data);
die("sad");
现在的结果:字符串格式化后并将该字符串推入数组。
array(3) {
["email_address"]=>
string(3) "sda"
["status_if_new"]=>
string(10) "subscribed"
["merge_fields"]=>
array(4) {
["FNAME"]=>
string(3) "asd"
["LNAME"]=>
string(3) "sad"
[0]=>
array(2) {
[0]=>
string(6) "Field0"
[1]=>
string(6) "Value0"
}
[1]=>
array(2) {
[0]=>
string(6) "Field1"
[1]=>
string(6) "Value1"
}
}
}
sad
我想要实现的目标: 但我想实现的有点不同,您可以在下面看到:
array(3) {
["email_address"]=>
string(3) "sda"
["status_if_new"]=>
string(10) "subscribed"
["merge_fields"]=>
array(4) {
["FNAME"]=>
string(3) "asd"
["LNAME"]=>
string(3) "sad"
["Field0"]=>
string(6) "Value0"
["Field1"]=>
string(6) "Value1"
}
}
sad
如有任何问题,请随时问我。
任何人?帮助我达到我想要的结果? TIA。
使用$post_data["merge_fields"][$fields[0]] = $fields[1]
而不是
array_push($post_data["merge_fields"],$fields);
$post_data = array(
'email_address' => "sda",
'status_if_new' => 'subscribed',
'merge_fields' => array(
'FNAME' => "asd",
'LNAME' => "sad")
);
$st= "{\"Field0\":\"Value0\",\"Field1\":\"Value1\"}";
$string = str_replace(array('{','}','"'),'',$st);
$custom_array=explode(",",$string);
$len= count($custom_array);
for($i=0;$i<$len;$i++) {
$fields = explode(":", $custom_array[$i]);
$post_data["merge_fields"][$fields[0]] = $fields[1];
}
echo "<pre>";
var_dump($post_data);
die("sad");
尝试实现我想要的结果。但不幸的是,在我绞尽脑汁几个小时后无法实现。到目前为止,我尝试了什么:
$post_data = array(
'email_address' => "sda",
'status_if_new' => 'subscribed',
'merge_fields' => array(
'FNAME' => "asd",
'LNAME' => "sad")
);
$st= "{\"Field0\":\"Value0\",\"Field1\":\"Value1\"}";
$string = str_replace(array('{','}','"'),'',$st);
$custom_array=explode(",",$string);
$len= count($custom_array);
for($i=0;$i<$len;$i++) {
$fields = explode(":", $custom_array[$i]);
array_push($post_data["merge_fields"],$fields);
}
echo "<pre>";
var_dump($post_data);
die("sad");
现在的结果:字符串格式化后并将该字符串推入数组。
array(3) {
["email_address"]=>
string(3) "sda"
["status_if_new"]=>
string(10) "subscribed"
["merge_fields"]=>
array(4) {
["FNAME"]=>
string(3) "asd"
["LNAME"]=>
string(3) "sad"
[0]=>
array(2) {
[0]=>
string(6) "Field0"
[1]=>
string(6) "Value0"
}
[1]=>
array(2) {
[0]=>
string(6) "Field1"
[1]=>
string(6) "Value1"
}
}
}
sad
我想要实现的目标: 但我想实现的有点不同,您可以在下面看到:
array(3) {
["email_address"]=>
string(3) "sda"
["status_if_new"]=>
string(10) "subscribed"
["merge_fields"]=>
array(4) {
["FNAME"]=>
string(3) "asd"
["LNAME"]=>
string(3) "sad"
["Field0"]=>
string(6) "Value0"
["Field1"]=>
string(6) "Value1"
}
}
sad
如有任何问题,请随时问我。 任何人?帮助我达到我想要的结果? TIA。
使用$post_data["merge_fields"][$fields[0]] = $fields[1]
而不是
array_push($post_data["merge_fields"],$fields);
$post_data = array(
'email_address' => "sda",
'status_if_new' => 'subscribed',
'merge_fields' => array(
'FNAME' => "asd",
'LNAME' => "sad")
);
$st= "{\"Field0\":\"Value0\",\"Field1\":\"Value1\"}";
$string = str_replace(array('{','}','"'),'',$st);
$custom_array=explode(",",$string);
$len= count($custom_array);
for($i=0;$i<$len;$i++) {
$fields = explode(":", $custom_array[$i]);
$post_data["merge_fields"][$fields[0]] = $fields[1];
}
echo "<pre>";
var_dump($post_data);
die("sad");