需要爆炸和字符串并使其成为键值对

时间:2014-06-07 11:52:10

标签: php dynamic-arrays

我有这个数据

[0] => 1#*#1-1

我的要求是从#*#爆炸,必须将生成的数组元素作为键值对

实施例

$data = explode("#*#",'1#*#1-1');

$ data [0] = 1; $ data [1] = 1-1; 现在我的要求是制作动态关联数组

array($data[1] => $data[0])

2 个答案:

答案 0 :(得分:1)

<?
$str = '1#*#1-1
     3#*#1-2
     5#*#1-3
     7#*#1-4
     9#*#1-5
     11#*#1-6
     13#*#1-7
     15#*#1-8
     17#*#1-9
     19#*#1-10
     2#*#2-1
     4#*#2-2
     6#*#2-3
     8#*#2-4
     10#*#2-5
     12#*#2-6
     14#*#2-7
     16#*#2-8
     18#*#2-9';

$ex = array_map('trim',explode("\n",$str));
$out = array();
foreach($ex as $e){
    $ex2 = explode('#*#',$e);
    $out[$ex2[1]] = $ex2[0];
}

print_r($out);

Array
(
    [1-1] => 1
    [1-2] => 3
    [1-3] => 5
    [1-4] => 7
    [1-5] => 9
    [1-6] => 11
    [1-7] => 13
    [1-8] => 15
    [1-9] => 17
    [1-10] => 19
    [2-1] => 2
    [2-2] => 4
    [2-3] => 6
    [2-4] => 8
    [2-5] => 10
    [2-6] => 12
    [2-7] => 14
    [2-8] => 16
    [2-9] => 18
)
?>

答案 1 :(得分:0)

<?php
$mydata = array();
$data = array('1#*#1-1'); 
list($key, $val) = explode('#*#', $data[0]);
$mydata[$val] = $key;
// Check work.
echo '<pre>' . print_r($mydata, TRUE) . '</pre>';