将包含数组元素的字符串转换为数组

时间:2016-11-05 07:16:04

标签: php arrays

我有一个我需要转换为数组的字符串。 这是我在变量中的字符串:

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",'

我想将它插入这样的数组中:

$tree = array(
    "list_Menu1" => "root",
    "list_Submenu2" => "list_Menu1",
    "list_Submenu3" => "list_Menu1",
    "list_Menu2" => "root",);

我尝试生成这样做的数组:$tree = array($text),但它不起作用。我怎么能这样做,我有点失落。

5 个答案:

答案 0 :(得分:2)

试试这个

    $text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';

    $text = str_replace("=>",":",$text);
    // trim last coma   
    $text = trim($text,",");
    $text = "{".$text."}";
    $array = json_decode($text,true);

    var_dump($array);

答案 1 :(得分:2)

它有点远,但它也有效..

function objectToArray($d) {
    if (is_object($d)) {
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        return array_map(__FUNCTION__, $d);
    }
    else {
        return $d;
    }
}    

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';

$text = str_replace("=>",':',$text);
$text = rtrim($text,",");
$text = '{'.$text.'}';

$text = json_decode($text);
$text = objectToArray($text);
print_r($text);

答案 2 :(得分:1)

可以使用

Explode字符串逗号(,)和删除空值索引array_filter

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';
$tree = array_filter( explode(',', $text) );

print '<pre>';
print_r($tree);
print '</pre>';

答案 3 :(得分:1)

希望这会有所帮助: -

<?php

function str_to_arr($str) {
  $str = str_replace('"',"",$str);
  $arr = explode(',',$str);
  for($i=0;$i<count($arr);$i++) {
    if($arr[$i]!="") {
      $tmp_arr = explode('=>',$arr[$i]);
      $arr2[$tmp_arr[0]] = $tmp_arr[1];
    }
  }
  return $arr2;
}

$text  ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';
$arr = str_to_arr($text);
print_r($arr);

?>

答案 4 :(得分:1)

str_replace和explode的结合将起到作用。这是:

Sub SaveToCSVs()
Const kWshName As String = "Data"
Dim sPathInp As String
Dim sPathOut As String
Dim sPathFile As String
Dim sCsvFile As String
Dim WbkSrc As Workbook
Dim WshSrc As Worksheet
Dim rData As Range

    sPathInp = "C:\temp\pydev\"
    sPathOut = "C:\temp\"
    sPathFile = Dir(sPathInp)

    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Do While (sPathFile <> "")
        If Right(sPathFile, 4) = ".xls" Or Right(sPathFile, 5) = ".xlsx" Then

            Rem Initialize Objects
            Set WbkSrc = Nothing
            Set WshSrc = Nothing

            Rem Set Objects
            On Error Resume Next
            Set WbkSrc = Workbooks.Open(Filename:=sPathInp & sPathFile, ReadOnly:=True)
            If Not (WbkSrc Is Nothing) Then
                Set WshSrc = WbkSrc.Sheets(kWshName)

                If Not (WshSrc Is Nothing) Then
                    On Error GoTo 0

                    Rem Set Csv Filename
                    sCsvFile = Left(sPathFile, -1 + InStrRev(sPathFile, "."))
                    sCsvFile = sCsvFile & " - " & kWshName

                    Rem Calculate, Unhide Rows & Columns & Copy Data Sheet
                    With WshSrc
                        .Calculate
                        .Cells.EntireRow.Hidden = False
                        .Cells.EntireColumn.Hidden = False

                        Rem Delete All Other Columns
                        With Range(.Cells(1), .UsedRange.SpecialCells(xlLastCell))
                            .Value = .Value
                            Set rData = Union(Columns("A"), Columns("P"), Columns("AC"))
                            rData.EntireColumn.Hidden = True
                            .SpecialCells(xlCellTypeVisible).EntireColumn.Delete
                            rData.EntireColumn.Hidden = False

                    End With: End With

                    Rem Save as Csv
                    WshSrc.SaveAs Filename:=sPathOut & sCsvFile, FileFormat:=xlCSV
                    WbkSrc.Close

        End If: End If: End If

        sPathFile = Dir
        On Error GoTo 0

    Loop

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True

    End Sub