dongma1666
2015-04-16 12:17
浏览 244
已采纳

使用key和value创建数组

I have a string:

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

I want to create an array where the first item is the key and the second one the value.

It should look like this:

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

How can I do that? It's difficult to search for a solution because I don't know how to search this.

It's very similar to this: Explode string into array with key and value

But I don't have a json array.

I tried something like that:

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

But it's a dirty solution. Is there any better way?

图片转代码服务由CSDN问答提供 功能建议

我有一个字符串:

  $ content =“test  ,某事,其他事物,数据,示例“; 
   
 
 

我想创建一个数组,其中第一个项是 key second 一个

它应如下所示:

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

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

它与此类似:使用键和值将字符串分解为数组

但我没有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 ++; 
继续; 
} 
} 
 
print_r($ result); 
   
 
 

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • donglou8371 2015-04-16 12:23
    已采纳

    Try this:

    $array = explode(',',$content);
    $size = count($array);
    for($i=0; $i<$size; $i++)
        $result[$array[$i]] = $array[++$i];
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douchu5131 2015-04-16 12:25

    Try this:

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

    </div>
    
    评论
    解决 无用
    打赏 举报
  • dsaeyrq451928 2015-04-16 12:30

    Try this

    $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);
    
    评论
    解决 无用
    打赏 举报
  • dongsi0625 2015-04-16 12:30

    You could able to use the following:

    $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);
    
    评论
    解决 无用
    打赏 举报
  • dongshuzui0335 2015-04-16 12:31
    $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);
    
    评论
    解决 无用
    打赏 举报
  • douya1248 2015-04-16 12:32

    I have tried this example this is working file.

    Code:-

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

    Output:-

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

    For your reference check this Click Here

    Hope this helps.

    评论
    解决 无用
    打赏 举报
  • dongzhouhao4316 2015-04-16 12:34
    $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);
    

    Output

    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];
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题