使用key和value创建数组

时间:2015-04-16 12:17:21

标签: php arrays explode

我有一个字符串:

$content = "test,something,other,things,data,example";

我想创建一个数组,其中第一个项为key第二个项为value

它应该是这样的:

Array
(
    [test] => something
    [other] => things
    [data] => example
)

我该怎么做?搜索解决方案很困难,因为我不知道如何搜索它。

与此非常相似:Explode string into array with key and value

但我没有json数组。

我尝试过类似的东西:

$content = "test,something,other,things,data,example";

$arr = explode(',', $content);

$counter = 1;
$result = array();

foreach($arr as $item) {
    if($counter % 2 == 0) {
        $result[$temp] = $item;
        unset($temp);
        $counter++;
    } else {
        $temp = $item;
        $counter++;
        continue;
    }
}

print_r($result);

但这是一个肮脏的解决方案。还有更好的办法吗?

7 个答案:

答案 0 :(得分:4)

试试这个:

$array = explode(',',$content);
$size = count($array);
for($i=0; $i<$size; $i++)
    $result[$array[$i]] = $array[++$i];

答案 1 :(得分:2)

试试这个:

$content = "test,something,other,things,data,example";
$data = explode(",", $content);// Split the string into an array
$result = Array();
$size = count($data); // Calculate the size once for later use
if($size%2 == 0)// check if we have even number of items(we have pairs)
for($i = 0; $i<$size;$i=$i+2){// Use calculated size here, because value is evaluated on every iteration
$result[$data[$i]] = $data[$i+1];
}

var_dump($result);

答案 2 :(得分:0)

试试这个

$content = "test,something,other,things,data,example";
$firstArray = explode(',',$content);

print_r($firstArray);

$final = array();
for($i=0; $i<count($firstArray); $i++)
{
    if($i % 2 == 0)
    {
        $final[$firstArray[$i]] = $firstArray[$i+1];
    }   
}
print_r($final);

答案 3 :(得分:0)

您可以使用以下内容:

$key_pair = array();
$arr = explode(',', $content);
$arr_length = count($arr);
if($arr_length%2 == 0)
{
   for($i = 0; $i < $arr_length; $i = $i+2)
   {
     $key_pair[$arr[$i]] = $arr[$i+1];
   }
}
print_r($key_pair);

答案 4 :(得分:0)

$content = "test,something,other,things,data,example";
$x = explode(',', $content);

$z = array();
for ($i=0 ; $i<count($x); $i+=2){
    $res[$x[$i]] = $x[$i+1];
    $z=array_merge($z,$res);
}

print_r($z);

答案 5 :(得分:0)

我试过这个例子,这是工作文件。

<强>代码: -

    <?php
 $string = "test,something|other,things|data,example";
 $finalArray = array();

 $asArr = explode( '|', $string );
 foreach( $asArr as $val ){
 $tmp = explode( ',', $val );
 $finalArray[ $tmp[0] ] = $tmp[1];
 }
 echo "After Sorting".'<pre>';
 print_r( $finalArray );
 echo '</pre>';
 ?>

<强>输出: -

Array
(
    [test] => something
    [other] => things
    [data] => example
)

如需参考,请查看此Click Here

希望这有帮助。

答案 6 :(得分:0)

$content = "test,something,other,things,data,example";

$contentArray = explode(',',$content);
for($i=0; $i<count($contentArray); $i++){
    $contentResult[$contentArray[$i]] = $contentArray[++$i];
}
print_r( $contentResult);

输出

Array
(
    [test] => something
    [other] => things
    [data] => example
)
  1. $ contentResult [$ contentArray [1]] = $ contentArray [2];
  2. $ contentResult [$ contentArray [3]] = $ contentArray [4];
  3. $ contentResult [$ contentArray [5]] = $ contentArray [6];