如何在php中将字符串转换为数组

时间:2016-07-21 11:32:20

标签: php arrays

我有string喜欢这个..

food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'

我尝试爆炸选项

但是我得到了array这样的

Array
(
    [0] => 'food_item' => 'Butter Croissant'
    [1] => 'food_id' => '1'
    [2] => 'quantity' => '1'
    [3] => 'price' => '140'
)

但我需要将其设为array,如下所示,

Array
(
    [food_item] =>  'Butter Croissant'
    [food_id]   =>  '1'
    [quantity]  =>  '1'
    [price]     =>  '140'
)

我应该怎么做,有人请帮助我..

提前谢谢..

5 个答案:

答案 0 :(得分:4)

尝试:

$str = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
$mainArray = explode(",",$str);
$newArray = array();
foreach($mainArray as $main) {
  $innerArray = explode("=>",$main);
  $newArray[trim($innerArray[0])] = trim($innerArray[1]);
}

输出:

Array
(
    [food_item] => 'Butter Croissant'
    [food_id] => '1'
    [quantity] => '1'
    [price] => '140'
)

答案 1 :(得分:2)

您可以执行以下操作: -

<?php
$string = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'"; // original string

$first_array = explode(',',$string); // your first explode with `,`

$final_array = array(); // create a new empty array

foreach($first_array as $arr){ // iterate over previously exploded array
    $data = explode('=>',$arr); // now explode the value again with `=>`
    $final_array[trim($data[0])] = trim($data[1]);  // add it to final array in the form of key=>value pair
}

echo "<pre/>";print_r($final_array);

输出: - https://eval.in/609356

注意: - 这是最容易做的事情,也可以理解。谢谢

答案 2 :(得分:1)

您可以使用:

$str = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
$exploded = array_map('myExplode', explode(',', $str));
$result = [];
foreach ($exploded as $itemPieces) {
    $result[trim($itemPieces[0])] = trim($itemPieces[1]);
}

function myExplode($item) {
    return explode("=>", str_replace("'", '', $item));
}

var_dump($result);

<强>输出

array (size=4)
  'food_item' => string 'Butter Croissant' (length=16)
  'food_id' => string '1' (length=1)
  'quantity' => string '1' (length=1)
  'price' => string '140' (length=3)

答案 3 :(得分:1)

您可以将preg_splitforeach循环结合使用,如下所示:

    <?php
        $strArray       = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";
        $arrKVPairs     = preg_split("#,\s?#", $strArray);
        $arrStrToArray  = array();

        foreach($arrKVPairs as $ikey=>$strData){
            $arrKeyVal      = preg_split("#\s?\=>\s?#", trim($strData, "'"));
            if(count($arrKeyVal) == 2){
                list($key, $val)        = $arrKeyVal;
                $arrStrToArray[$key]    = trim($val, "'");
            }
        }
        var_dump($arrStrToArray);
        // PRODUCES::

        array (size=4)
          'food_item' => string 'Butter Croissant' (length=16)
          'food_id' => string '1' (length=1)
          'quantity' => string '1' (length=1)
          'price' => string '140' (length=3)

答案 4 :(得分:1)

由于我们得到了答案,我想发布不同的解决方案(给定的字符串很像参数字符串,可以通过parse_str函数在数组或变量中进行解析):

$string = "food_item => 'Butter Croissant',food_id => '1',quantity => '1',price => '140'";

$paramString = str_replace([',', " => ", "'"], ['&', '=', ''], $string);

parse_str($paramString, $data);

现在我们根据需要获得数据,代码看起来更清晰