Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在尝试将混合字符串转换为数组,但无法获得准确的输出_Php_Arrays_String_Multidimensional Array_Explode - Fatal编程技术网

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

Php 正在尝试将混合字符串转换为数组,但无法获得准确的输出,php,arrays,string,multidimensional-array,explode,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 i

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

我已经使用爆炸功能删除管道标志,但我无法获得准确的输出

以下是我迄今为止尝试的代码

$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);
但我想删除等于的“=”并将左值作为键,将右值作为值。请参阅下面我的预期输出。

但我的预期输出是:

foreach($testarray as $item){
    $arr = explode("=", $item);
    $testarray[$arr[0]] = $arr[1];
}

高级感谢

我会在你的新数组中循环并分解
=
字符上的每个项目

foreach($testarray as $item){
    $arr = explode("=", $item);
    $final[$arr[0]] = $arr[1];
}
唯一的问题是您需要整理
$testarray
以删除数字键控项

要解决这个问题,我只需要创建一个最终数组

<?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($testarray as $item){
    $arr = explode("=", $item);
    $final[$arr[0]] = $arr[1];
}
唯一的问题是您需要整理
$testarray
以删除数字键控项

要解决这个问题,我只需要创建一个最终数组

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

连续显示两种方法:

1)使用附加的
分解
功能和
列表
功能:

$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);

2)另一种替代解决方案是使用
preg\u match\u all
array\u combine
功能:

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
)
输出(两种方法相同):


连续显示两种方法:

1)使用附加的
分解
功能和
列表
功能:

$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);

2)另一种替代解决方案是使用
preg\u match\u all
array\u combine
功能:

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只需使用foreach创建如下条件:


@Manthan Dave只需使用foreach创建如下条件:


谢谢你的帮助:)谢谢你的帮助:)谢谢你的帮助:)谢谢你的帮助:)谢谢你的帮助:)谢谢你的帮助:)