在 PHP 中如何将字符串数组转换为关联数组?

我目前有一个包含三个字符串的数组,我的目标是将数组中的这三个字符串转换为单独的关联数组。


这是数组:


$arr = array(


  "action: Added; amount: 1; code: RNA1; name: Mens Organic T-shirt; colour: White; size: XL",


  "action: Subtracted; amount: 7; code: RNC1; name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",


  "action: Added; amount: 20; code: RNV1; name: Gift Voucher; style: Mens; value: £20",

我想让关联数组的键是操作、数量、项目代码等,首先我想我应该尝试将数组的内容转换为字符串,以使其更易于管理。


// Convert $arr to a string.

$imploded = implode(": ",$arr);

//echo $imploaded;

$str = $imploded;

$results = preg_split('/: [;,]/', $str);

print_r($results);

理想情况下,我希望数组看起来像这样。我对 PHP 很陌生,所以任何帮助和建议将不胜感激。


array(

    'action'    =>   '',

    'amount'    =>   '',

    'code'  =>   '',

    'name'  =>   '',

    'colour'    =>   '',

    'size'      =>   '',

);


牧羊人nacy
浏览 209回答 4
4回答

Cats萌萌

这可能是您所需要的:<?php$arr = array(     "action: Added; amount: 1; code: RNA1; name: Mens Organic T-shirt; colour: White; size: XL",      "action: Subtracted; amount: 7; code: RNC1; name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",      "action: Added; amount: 20; code: RNV1; name: Gift Voucher; style: Mens; value: £20");    foreach ($arr as $string) {   //Build array   preg_match_all("/ [ ]?([^:]+): ([^;]+)[ ;]? /x", $string, $p);   $array = array_combine($p[1], $p[2]);   //Print it or do something else with it    print_r($array);}https://3v4l.org/0cnhZ

守候你守候我

很简单:$arr = array(&nbsp; &nbsp; "action: Added; amount: 1; code: RNA1; name: Mens Organic T-shirt; colour: White; size: XL",&nbsp; &nbsp; "action: Subtracted; amount: 7; code: RNC1; name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",&nbsp; &nbsp; "action: Added; amount: 20; code: RNV1; name: Gift Voucher; style: Mens; value: £20",);&nbsp;&nbsp;$data = [];foreach ($arr as $line) {&nbsp; &nbsp; $newItem = [];&nbsp; &nbsp; foreach (explode(';', $line) as $item) {&nbsp; &nbsp; &nbsp; &nbsp; $parts = explode(':', $item);&nbsp; &nbsp; &nbsp; &nbsp; $newItem[trim($parts[0])] = trim($parts[1]);&nbsp; &nbsp; }&nbsp; &nbsp; $data[] = $newItem;}print_r($data);在此处拨动。

眼眸繁星

您可以使用 :$tempArray = array();foreach($arr as $data){&nbsp; &nbsp; $dataExplode = explode(';', $data);&nbsp; &nbsp; &nbsp; $temp = array();&nbsp; &nbsp; foreach($dataExplode as $key => $child){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $childExplode = explode(':', $child);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp[$childExplode[0]] = $childExplode[1];&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; array_push($tempArray, $temp);}print_r($tempArray); //to print array collection您将获得单独的集合array()

繁华开满天机

只是为了好玩,还有一种方法可以做到这一点:$arr = array(&nbsp; "action: Added; amount: 1; code: RNA1; name: Mens Organic T-shirt; colour: White; size: XL",&nbsp; "action: Subtracted; amount: 7; code: RNC1; name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",&nbsp; "action: Added; amount: 20; code: RNV1; name: Gift Voucher; style: Mens; value: £20",);$result = array_map(function($line) {&nbsp; &nbsp; return array_combine(...array_map(null, ...array_chunk(preg_split('/[:;]\s+/', $line), 2)));}, $arr);print_r($result);基本上,我们用两个可能的分隔符 : 或 ; 后跟空格来分割该行。然后 array_chunk 函数将键和值分组在一起,我们为每一行得到如下所示的内容:&nbsp; &nbsp; &nbsp; &nbsp; Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => action&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Added&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => amount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )* * *通过扩展和映射上面的数组,我们得到它的旋转版本,如下所示:Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => action&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => amount* * *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Added&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 1* * *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )我们现在可以扩展这个数组并将其传递给array_combine函数,这样我们就有了一个很好的关联数组,其中填充了所需的键和值。然后我们映射所有的线,这样我们就得到了格式良好的结果。这是最后的小提琴。
打开App,查看更多内容
随时随地看视频慕课网APP