尝试将混合字符串转换为数组,但无法获得准确的输出

时间:2017-02-15 10:04:08

标签: php arrays string multidimensional-array 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
)

但我想删除这个&#34; =&#34;等于并将左值作为键,将右值作为值。请参阅下面的预期输出。

但我的预期输出是:

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
)

高级谢谢

4 个答案:

答案 0 :(得分:1)

我循环播放新数组并展开=字符上的每个项目。

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];
}

答案 1 :(得分: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,您可以获得所需的结果

答案 2 :(得分:1)

连续显示两种方法:

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_allarray_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
)

答案 3 :(得分:1)

  

@Manthan Dave只是对foreach做了如下的条件:

<?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