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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14133780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:43:32  来源:igfitidea点击:

Explode a string to associative array without using loops?

phparrays

提问by Pramod

I have a string like 1-350,9-390.99,..., and I need to turn it into an associative array like this:

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

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

Is it possible to do this using only array functions, without a loop?

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

采纳答案by bozdoz

Here's a way to do it without a for loop, using array_walk:

这是一种无需 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];
}

Example on Ideone.com.

Ideone.com上的示例

回答by bishop

PHP 5.5+ two-line solution, using array_chunkand array_column:

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

Yields:

产量:

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

See it online at 3v4l.org.

在 3v4l.org 上在线查看。

回答by jel

Something like this should work:

这样的事情应该工作:

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

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

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

回答by hafichuk

This uses array_walkwith a closure.

这使用带有闭包的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);
?>

Interactive fiddle.

交互式小提琴

回答by MuteX

Code is tested.

代码经过测试。

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

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

All the best.

祝一切顺利。

回答by Ilmari Karonen

Technically, it ispossible to do this without using loops (demo on codepad.org):

从技术上讲,这可能做到这一点,而不使用(循环演示上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);

However, this is reallynot something you'd want to do in real code — not only is it ridiculously convoluted, but it's also almost certainly less efficient than the simple foreach-based solution others have already given (demo on codepad.org):

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

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

回答by Amit Garg

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

回答by Vinoth Babu

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

回答by Hanky Panky

Try this

尝试这个

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

回答by Sithu

Try this:

尝试这个:

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