2017-04-10 69 views
0

我有五個字符串:將字符串轉換爲關聯數組

$array_key = "f_name, f_qty, f_price, f_cur_date"; 

$food_name = "Meal for 2, Four Seasons Pizza, Lunch Deal, Pepsi"; 
$food_qty = "1, 3, 5, 7"; 
$food_price = "10, 30, 50, 70"; 
$food_userId = "1, 2, 3, 4"; 

我需要一些數組是這樣的:

Array (
    [0] => Array (
    [f_name] => Meal for 2 
    [f_qty] => 1 
    [f_price] => 10 
    [f_cur_date] => 1 
) 
[1] => Array (
    [f_name] => Four Seasons Pizza 
    [f_qty] => 3 
    [f_price] => 30 
    [f_cur_date] => 2 
) 
[2] => Array (
    [f_name] => Lunch Deal 
    [f_qty] => 5 
    [f_price] => 50 
    [f_cur_date] => 3 
) 
[3] => Array (
    [f_name] => Pepsi 
    [f_qty] => 7 
    [f_price] => 70 
    [f_cur_date] => 4 
)) 

我將字符串轉換爲數組, 「爆炸」:

$array_key_parts = explode(",",$array_key); 
$food_name_parts = explode(",",$food_name); 
$food_qty_parts = explode(",",$food_qty); 
$food_price_parts = explode(",",$food_price); 
$food_price_parts = explode(",",$food_price); 

但是不知道如何進一步定製它。

基本上,我從查詢GROUP_CONCAT中獲取該字符串以在fullcalendar.js中使用它。

+1

什麼是這五個串的來源是什麼?也許最好先從數據庫中獲取數據。 –

+0

你怎麼得到你的數據? – hassan

回答

3

您可以用array_map功能類似下面的數組:

$food_name_parts = explode(",",$food_name); 
$food_qty_parts = explode(",",$food_qty); 
$food_price_parts = explode(",",$food_price); 
$array_key_parts = explode(",",$array_key); 


$myarray = array_map(function ($f_name, $f_qty, $f_price) { 
    return compact('f_name','f_qty','f_price'); 
}, $food_name_parts, $food_qty_parts, $food_price_parts); 

echo "<pre>"; 
print_r($myarray); 
+0

這就是我要找的,謝謝。 – Pecooou