调用时传递引用错误,无法修复代码

时间:2014-03-08 01:16:09

标签: php arrays function error-handling associative-array

自从我更新到php 5.4后我得到错误调用时间传递引用已被删除,正如我读到的,删除&应解决它。确实如此,但现在我的代码不再起作用了。

我需要从字符串raw400中创建一个关联数组,其中键为t和f加上数字(例如t410和f410),值必须是 - 之后的值 - 没有 -

示例:

t410-1:06,f410-15.4

schould成为:

key: t410
value: 1:06

key: f410
value 15.4

代码..

$raw400 = 't410-1:06,f410-15.4,t390-1:06,f390-15.6,t370-1:05,f370-16.0,t350-1:05,f350-16.2,t330-1:04,f330-16.3,t310-1:00,f310-16.7';

$array = explode(',',$raw400);
$nm400 = array();
array_walk($array,'walk', $nm400);
function walk($val, $key, $nm400){
$nums = explode('-',$val);
$nm400[$nums[0]] = $nums[1];
}

非常感谢帮助,谢谢。

2 个答案:

答案 0 :(得分:3)

只需使用parse_str()即可。

<?php
$raw400 = 't410-1:06,f410-15.4,t390-1:06,f390-15.6,t370-1:05,f370-16.0,t350-1:05,f350-16.2,t330-1:04,f330-16.3,t310-1:00,f310-16.7';
parse_str(str_replace(array('-',','),array('=','&'),$raw400),$arr);
print_r($arr);

<强> OUTPUT :

Array
(
    [t410] => 1:06
    [f410] => 15.4
    [t390] => 1:06
    [f390] => 15.6
    [t370] => 1:05
    [f370] => 16.0
    [t350] => 1:05
    [f350] => 16.2
    [t330] => 1:04
    [f330] => 16.3
    [t310] => 1:00
    [f310] => 16.7
)

Demo

答案 1 :(得分:1)

使用preg_match_all()array_combine

<?php
$raw400 = 't410-1:06,f410-15.4,t390-1:06,f390-15.6,t370-1:05,f370-16.0,t350-1:05,f350-16.2,t330-1:04,f330-16.3,t310-1:00,f310-16.7';
preg_match_all("/([^-, ]+)\,([^-,]+)/", $raw400, $res);
$array = array_combine($res[2], $res[1]);
print_r($array);

输出:

 Array
(
    [f410] => 1:06
    [t390] => 15.4
    [f390] => 1:06
    [t370] => 15.6
    [f370] => 1:05
    [t350] => 16.0
    [f350] => 1:05
    [t330] => 16.2
    [f330] => 1:04
    [t310] => 16.3
    [f310] => 1:00
)