尝试将混合字符串转换为数组但无法获得准确的输出
Trying to convert mix string into the array but unable to get exact output
您好,我正在尝试将混合字符串转换为数组,但无法获得准确的输出。
我已经使用 explode 函数删除管道符号,但我无法获得准确的输出
下面是我到目前为止尝试过的代码:
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
echo "String :<br>".$customstring;
$testarray = explode("|",$customstring);
echo "ARRAY<br><pre>";
print_r($testarray);
我得到的输出如下:
Array
(
[0] => Eye Width=3/4 in
[1] => Finish=Nickel
[2] => Hook Opening=7/16 in
[3] => Locking Type=Spring Loaded Plunger
[4] => Material=Zinc Die Cast
[5] => Mounting=Swivel Eye
[6] => Overall Length [Nom]=3 1/2 in
[7] => Type=Swiveled Securing Hook
[8] => Wt.=0.09 lb
)
但我想删除这个“=”等于并使左值为键,右值为值。在下面查看我的预期输出。
但我的预期输出是这样的:
Array
(
[Eye Width] => 3/4 in
[Finish] => Nickel
[Hook Opening] => 7/16 in
[Locking Type] => Spring Loaded Plunger
[Material] => Zinc Die Cast
[Mounting] => Swivel Eye
[Overall Length [Nom]] => 3 1/2 in
[Type] => Swiveled Securing Hook
[Wt.] => 0.09 lb
)
非常感谢
我将遍历你的新数组并展开 =
字符上的每个项目。
foreach($testarray as $item){
$arr = explode("=", $item);
$testarray[$arr[0]] = $arr[1];
}
唯一的问题是您需要整理 $testarray
以删除数字键项。
为了解决这个问题,我只创建了一个最终数组
foreach($testarray as $item){
$arr = explode("=", $item);
$final[$arr[0]] = $arr[1];
}
<?php
$string = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
$formed_data = explode("|", $string);
$desired_data = [];
foreach ($formed_data as $single_string) {
$words= explode("=", $single_string);
$desired_data[$words[0]] = $words[1];
}
var_dump($desired_data);
/**
* Output
*/
array(9) {
["Eye Width"]=>
string(6) "3/4 in"
["Finish"]=>
string(6) "Nickel"
["Hook Opening"]=>
string(7) "7/16 in"
["Locking Type"]=>
string(21) "Spring Loaded Plunger"
["Material"]=>
string(13) "Zinc Die Cast"
["Mounting"]=>
string(10) "Swivel Eye"
["Overall Length [Nom]"]=>
string(8) "3 1/2 in"
["Type"]=>
string(22) "Swiveled Securing Hook"
["Wt."]=>
string(7) "0.09 lb"
}
[Finished in 0.0s]
通过使用 foreach 你可以获得想要的结果
连续显示的两种方法:
1) 使用额外的 explode
函数结合 list
函数:
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
$pairs = explode("|", $customstring);
$result = [];
foreach ($pairs as $p) {
list($k, $v) = explode('=', $p);
$result[$k] = $v;
}
print_r($result);
2) 另一种替代解决方案是使用 preg_match_all
和 array_combine
函数:
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
preg_match_all("/([^=|]+)=([^+|]+)/", $customstring, $m);
$result = array_combine($m[1], $m[2]);
print_r($result);
输出(两种方法相同):
Array
(
[Eye Width] => 3/4 in
[Finish] => Nickel
[Hook Opening] => 7/16 in
[Locking Type] => Spring Loaded Plunger
[Material] => Zinc Die Cast
[Mounting] => Swivel Eye
[Overall Length [Nom]] => 3 1/2 in
[Type] => Swiveled Securing Hook
[Wt.] => 0.09 lb
)
@Manthan Dave simply do with foreach with make a condition like below:
<?php
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
echo "String :<br>".$customstring;
$testarray = explode("|",$customstring);
echo "ARRAY<br><pre>";
print_r($testarray); // before
foreach($testarray as $key => $val){
if(strpos($val, "=") || strpos($val, "=") == "0"){
$temp = explode("=", $val);
$testarray[$key] = $temp[1];
}
}
echo "ARRAY<br><pre>";
print_r($testarray); // after
您好,我正在尝试将混合字符串转换为数组,但无法获得准确的输出。
我已经使用 explode 函数删除管道符号,但我无法获得准确的输出
下面是我到目前为止尝试过的代码:
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
echo "String :<br>".$customstring;
$testarray = explode("|",$customstring);
echo "ARRAY<br><pre>";
print_r($testarray);
我得到的输出如下:
Array
(
[0] => Eye Width=3/4 in
[1] => Finish=Nickel
[2] => Hook Opening=7/16 in
[3] => Locking Type=Spring Loaded Plunger
[4] => Material=Zinc Die Cast
[5] => Mounting=Swivel Eye
[6] => Overall Length [Nom]=3 1/2 in
[7] => Type=Swiveled Securing Hook
[8] => Wt.=0.09 lb
)
但我想删除这个“=”等于并使左值为键,右值为值。在下面查看我的预期输出。
但我的预期输出是这样的:
Array
(
[Eye Width] => 3/4 in
[Finish] => Nickel
[Hook Opening] => 7/16 in
[Locking Type] => Spring Loaded Plunger
[Material] => Zinc Die Cast
[Mounting] => Swivel Eye
[Overall Length [Nom]] => 3 1/2 in
[Type] => Swiveled Securing Hook
[Wt.] => 0.09 lb
)
非常感谢
我将遍历你的新数组并展开 =
字符上的每个项目。
foreach($testarray as $item){
$arr = explode("=", $item);
$testarray[$arr[0]] = $arr[1];
}
唯一的问题是您需要整理 $testarray
以删除数字键项。
为了解决这个问题,我只创建了一个最终数组
foreach($testarray as $item){
$arr = explode("=", $item);
$final[$arr[0]] = $arr[1];
}
<?php
$string = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
$formed_data = explode("|", $string);
$desired_data = [];
foreach ($formed_data as $single_string) {
$words= explode("=", $single_string);
$desired_data[$words[0]] = $words[1];
}
var_dump($desired_data);
/**
* Output
*/
array(9) {
["Eye Width"]=>
string(6) "3/4 in"
["Finish"]=>
string(6) "Nickel"
["Hook Opening"]=>
string(7) "7/16 in"
["Locking Type"]=>
string(21) "Spring Loaded Plunger"
["Material"]=>
string(13) "Zinc Die Cast"
["Mounting"]=>
string(10) "Swivel Eye"
["Overall Length [Nom]"]=>
string(8) "3 1/2 in"
["Type"]=>
string(22) "Swiveled Securing Hook"
["Wt."]=>
string(7) "0.09 lb"
}
[Finished in 0.0s]
通过使用 foreach 你可以获得想要的结果
连续显示的两种方法:
1) 使用额外的 explode
函数结合 list
函数:
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
$pairs = explode("|", $customstring);
$result = [];
foreach ($pairs as $p) {
list($k, $v) = explode('=', $p);
$result[$k] = $v;
}
print_r($result);
2) 另一种替代解决方案是使用 preg_match_all
和 array_combine
函数:
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
preg_match_all("/([^=|]+)=([^+|]+)/", $customstring, $m);
$result = array_combine($m[1], $m[2]);
print_r($result);
输出(两种方法相同):
Array
(
[Eye Width] => 3/4 in
[Finish] => Nickel
[Hook Opening] => 7/16 in
[Locking Type] => Spring Loaded Plunger
[Material] => Zinc Die Cast
[Mounting] => Swivel Eye
[Overall Length [Nom]] => 3 1/2 in
[Type] => Swiveled Securing Hook
[Wt.] => 0.09 lb
)
@Manthan Dave simply do with foreach with make a condition like below:
<?php
$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb";
echo "String :<br>".$customstring;
$testarray = explode("|",$customstring);
echo "ARRAY<br><pre>";
print_r($testarray); // before
foreach($testarray as $key => $val){
if(strpos($val, "=") || strpos($val, "=") == "0"){
$temp = explode("=", $val);
$testarray[$key] = $temp[1];
}
}
echo "ARRAY<br><pre>";
print_r($testarray); // after