2016-07-21 43 views
3

我有一個string這樣的..如何使一個字符串到一個數組在PHP

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

我應該怎麼做,有人請幫助我..

預先感謝您..

+0

你的第一個「字符串」是一個數組,而不是一個字符串 –

+0

也許他把它作爲一個字符串...... – Ivan

+0

也許,但它有點令人困惑 –

回答

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

注: - 這是最簡單的事情要做,而且要了解also.thanks

+0

謝謝sooo :) :) D最後我明白了...你救了我的一天... –

+0

@SuganyaRajasekar很樂意幫助你 –

+0

@ Anant是的,我做到了:) –

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' 
) 
+0

謝謝你的回答.. :) –

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

OUTPUT

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

因爲我們得到了答案,我想後不同的解決方案(給定的字符串很像參數字符串,可以在陣列解析或在parse_str功能增值經銷商):

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

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

parse_str($paramString, $data); 

需要現在我們得到的數據和代碼看起來有點清潔

相關問題