31

我有一个类似的字符串1-350,9-390.99,...,我需要把它变成这样的关联数组:

 Array
    (
        [1] => 350
        [9] => 390.99
        ...........
    )

是否可以仅使用数组函数而不使用循环来执行此操作?

4

10 回答 10

36

PHP 5.5+ 两行解决方案,使用array_chunkand array_column

$input  = '1-350,9-390.99';

$chunks = array_chunk(preg_split('/(-|,)/', $input), 2);
$result = array_combine(array_column($chunks, 0), array_column($chunks, 1));

print_r($result);

产量:

Array
(
    [1] => 350
    [9] => 390.99
)

在 3v4l.org 上在线查看。

于 2015-09-24T18:03:33.763 回答
21

这是一种无需 for 循环的方法,使用array_walk

$array = explode(',', $string);
$new_array = array();
array_walk($array,'walk', $new_array);

function walk($val, $key, &$new_array){
    $nums = explode('-',$val);
    $new_array[$nums[0]] = $nums[1];
}

Ideone.com上的示例。

于 2013-01-03T06:45:39.537 回答
8

像这样的东西应该工作:

$string = '1-350,9-390.99';

$a = explode(',', $string);

foreach ($a as $result) {
    $b = explode('-', $result);
    $array[$b[0]] = $b[1];
}
于 2013-01-03T06:05:47.143 回答
7

这将array_walk闭包一起使用。

<?php
$string = "1-350,9-390.99";
$partial = explode(',', $string);
$final = array();
array_walk($partial, function($val,$key) use(&$final){
    list($key, $value) = explode('-', $val);
    $final[$key] = $value;
});
print_r($final);
?>

互动小提琴

于 2013-01-03T06:44:04.623 回答
3
$string = '1-350,9-390.99........';
$final_result = array();
foreach (explode(',', $string) as $piece) {
    $result = array();
    $result[] = explode('-', $piece);
    $final_result[$result[0]] = $result[1];
}

print_r($final_result);
于 2013-01-03T06:03:32.387 回答
2
$x='1-350,9-390.99';
$arr1=explode(',',$x);
$res_arr=array();
foreach($arr1 as $val){
    $arr2=explode('-',$val);
    $res_arr[$arr2[0]]=$arr2[1];
    }

print_r($res_arr);
于 2013-01-03T06:06:45.567 回答
2

从技术上讲,可以在不使用循环的情况下执行此操作( codepad.org 上的演示):

$string = '1-350,9-390.99';

// first, split the string into an array of pairs
$output = explode(',', $string);
function split_pairs ($str) {
    return explode('-', $str, 2);
}
$output = array_map(split_pairs, $output);

// then transpose it to get two arrays, one for keys and one for values
array_unshift($output, null);
$output = call_user_func_array(array_map, $output);

// and finally combine them into one
$output = array_combine($output[0], $output[1]);

var_export($output);

然而,这真的不是你想要在真实代码中做的事情——它不仅非常复杂,而且几乎肯定比foreach其他人已经给出的基于简单的解决方案效率低(codepad.org 上的演示):

$output = array();
foreach ( explode( ',', $string ) as $pair ) {
    list( $key, $val ) = explode( '-', $pair, 2 );
    $output[$key] = $val;
} 
于 2013-01-03T06:29:49.887 回答
1

试试这个:

$string = '1-350,9-390.99';
$array = explode(',', $string);

$output = array();
foreach($array as $arr){
    $chunk = explode('-', $arr);
    $output[$chunk[0]] = $chunk[1];
}
echo '<pre>'; print_r($output); echo '</pre>';
于 2013-01-03T06:04:36.610 回答
0

这是你想要的吗?

<?php 
//the string to process
$str = "1-350,9-390.99";

//explode it
$str_exploded = explode(",",$str);

$final_arr;
foreach($str_exploded as $str_elem){
    //extract
    $str_elem_final = explode("-",$str_elem);
    //the first elem is the index and the last elem is the value
    $final_arr[(int)$str_elem_final[0]] = $str_elem_final[1];

}
print_r($final_arr);

?>

http://rextester.com/VEY59445

于 2013-01-03T06:18:43.853 回答
0

我个人使用:

/**
 * @desc String to associative array
 * 
 * @param string $string
 * @param string $element_delimiter
 * @param string $value_delimiter
 * 
 * @example
  $string = "1:9|class:fa fa-globe";
  $array = string_to_array($string);
 * 
 * @return array $results
 */
function string_to_array($string, $element_delimiter = '|', $value_delimiter = ':') {
    $results = array();
    $array = explode($element_delimiter, $string);
    foreach ($array as $result) {
        $element = explode($value_delimiter, $result);
        $results[$element[0]] = $element[1];
    }
    return $results;
}

/**
 * @desc Associative array to string
 * 
 * @param type $array
 * @param type $element_delimiter
 * @param type $value_delimiter
 * 
 * @example
  $array = array('class' => 'in-line', 'rel' => 'external');
  $string = array_to_string($array);
 * 
 * @return string
 */
function array_to_string($array, $element_delimiter = '|', $value_delimiter = ':') {
    array_walk($array, create_function('&$i,$k', 'if (strlen($k) > 0){$i="' . $element_delimiter . '$k' . $value_delimiter . '$i";}'));
    return substr(implode($array, ""), 1);
}
于 2014-08-04T10:52:57.987 回答