This question already has an answer here:
这个问题已经有了答案:
- PHP Split Delimited String into Key/Value Pairs (Associative Array) 3 answers
- PHP将分隔字符串分割为键/值对(关联数组)3个答案
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, with no loops?
是否可以只使用数组函数,而不使用循环?
12 个解决方案
#1
18
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上的例子。
#2
29
PHP 5.5+ two-line solution, using array_chunk
and array_column
:
PHP 5.5+两行解决方案,使用array_chunk和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);
Yields:
收益率:
Array
(
[1] => 350
[9] => 390.99
)
在3v4l.org上可以看到。
#3
7
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];
}
#4
7
This uses array_walk with 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);
?>
互动的小提琴。
#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
Technically, it is possible 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 really not 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;
}
#8
2
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.
愿一切都好!
#9
1
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);
?>
#10
1
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>';
#11
0
Is this the thing you want?
这就是你想要的吗?
<?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
#12
0
Personally i use:
我个人使用:
/**
* @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);
}
#1
18
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上的例子。
#2
29
PHP 5.5+ two-line solution, using array_chunk
and array_column
:
PHP 5.5+两行解决方案,使用array_chunk和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);
Yields:
收益率:
Array
(
[1] => 350
[9] => 390.99
)
在3v4l.org上可以看到。
#3
7
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];
}
#4
7
This uses array_walk with 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);
?>
互动的小提琴。
#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
Technically, it is possible 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 really not 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;
}
#8
2
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.
愿一切都好!
#9
1
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);
?>
#10
1
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>';
#11
0
Is this the thing you want?
这就是你想要的吗?
<?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
#12
0
Personally i use:
我个人使用:
/**
* @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);
}