douwen7905 2017-04-10 12:37
浏览 70
已采纳

将字符串转换为关联数组

I have five strings:

$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";

I need some array like this:

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

I convert strings to array with "explode":

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

But don't know how to customize it further.

Basically, I'm getting this strings from query GROUP_CONCAT to use it in fullcalendar.js.

  • 写回答

1条回答 默认 最新

  • dounanyin3179 2017-04-10 13:16
    关注

    You can make an array using array_map function like below :

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?