在不使用循环的情况下将字符串分解为关联数组?

时间:2013-01-03 05:57:46

标签: php arrays

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

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

是否可以仅使用数组函数执行此操作,而无需循环?

12 个答案:

答案 0 :(得分:31)

PHP 5.5+双线解决方案,使用array_chunkarray_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
)

See it online at 3v4l.org.

答案 1 :(得分:20)

使用array_walk

,可以在没有for循环的情况下执行此操作
$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上的示例。

答案 2 :(得分:7)

这样的事情应该有效:

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

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

foreach ($a as $result) {
    $b = explode('-', $result);
    $array[$b[0]] = $b[1];
}

答案 3 :(得分:7)

这使用array_walkclosure

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

Interactive fiddle

答案 4 :(得分:3)

代码已经过测试。

<?php
    $array = "1-350,9-390.99";
    $arr = explode(",",$array);

    $desireArray = array();
    foreach($arr as $value)
    {
        $val = explode("-",$value);
        $desireArray[$val[0]] = $val[1];
    }
?>

一切顺利。

答案 5 :(得分:2)

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

答案 6 :(得分: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);

答案 7 :(得分:2)

从技术上讲, 可以在不使用循环(demo on 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效率低 - 其他人已经给出的基础解决方案(demo on codepad.org):

$output = array();
foreach ( explode( ',', $string ) as $pair ) {
    list( $key, $val ) = explode( '-', $pair, 2 );
    $output[$key] = $val;
} 

答案 8 :(得分:1)

试试这个

<?php
 $string="1-350,9-390.99,4-569.34";
 $values=explode(",",$string);
 $output=array();
 foreach($values as $value)
 {
    list($k,$v)=explode("-",$value);
    $output[$k]=$v;
 }
 print_r($output);
?>

答案 9 :(得分: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>';

答案 10 :(得分: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

答案 11 :(得分: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);
}