douji8033 2015-11-25 08:55
浏览 56
已采纳

如何将数组的值转换为数组的键?

I am parsing a text file that looks more or less like this:

123.animal=cat
123.name=fred
123.food=fish
345.animal=dog
petshop=zoonoria

This is how I am parsing it:

$file_path = $filename;
$linesArray = file($file_path);    
$properties = array();

foreach ($linesArray AS $line) {

    if (strlen($line) && $line[0] == '#') {
        $pdate = substr($line, 1);
        $date = rtrim($pdate);
        $formatted = DateTime::createFromFormat('* M d H:i:s T Y',$date);
        }



    if (false !== ($pos = strpos($line, '='))) {
        $prop=array();    
        $prop[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
        $lineContArray = explode("=", $line);
        $identArray = explode(".", $lineContArray[0]);

        $ident = $identArray[0];
        $type = $identArray[1];     

        $value = trim($lineContArray[1]);

        $found = 0;
        for ($i=0; $i<count($properties); $i++) {
            if ($properties['number'] == $ident) {
                $properties[$i][$type]= $value;
                $found=1;
                break;
                }
            }
        if ($found == 0) { 
            if (!empty($type)) { 
                $properties[] = array('number' => $ident, $type => $value); 
                } else { 
                $properties[] = array($ident => $value); } 
            }
        }

My result is:

array(3) {
  [0]=>
  array(2) {
    ["number"]=>
    string(3) "123"
    ["animal"]=>
    string(3) "cat"
  }
  [1]=>
  array(2) {
    ["number"]=>
    string(3) "123"
    ["name"]=>
    string(4) "fred"
  }
  [3]=>
  array(2) {
    ["number"]=>
    string(3) "345"
    ["animal"]=>
    string(4) "dog"
  }
  [4]=>
  array(1) {
   ["petshop"]=>
   string(5) "zoonoria"
  }
}

But I need the array to be different, this is the result I like to achieve:

  array(3) {
      [123]=>
      array(3) {
        ["animal"]=>
        string(3) "cat"
        ["name"]=>
        string(4) "fred"
        ["food"]=>
        string(4) "fish"
      }

      [345]=>
      array(1) {
        ["animal"]=>
        string(3) "dog"
      }
      [petshop]=>
        string(8) "zoonoria"
      }
    }

So my main problem is, I do not know how to turn number into the key. I tried various things, but I failed. I am really happy for every hint.


I tried the solution of Svetlio:

$file_path = $filename;
$linesArray = file($file_path);    
$properties = array();


foreach ( $linesArray as $str) {
        $exp = explode ('=', $str);
        if(count($exp) == 2){
                $exp2 = explode('.', $exp[0]);
                if( count($exp2) == 2 ) {
                        $properties [$exp2[0]][$exp2[1]] = $exp[1];
                } else {   
                        $properties [$exp[0]] = $exp[1];
                }
        } else {

        }

}

My result:

array(3) {
  ["123"]=>
  array(3) {
    ["animal"]=>
    string(3) "cat
"
    ["name"]=>
    string(4) "fred
"
    ["food"]=>
    string(4) "fish
"
  }
  [345]=>
  array(1) {
    ["animal"]=>
    string(3) "dog
"
  }
  ["petshop"]=>
  string(3) "zoonoria
"
}
  • 写回答

4条回答 默认 最新

  • dsfsdf5646 2015-11-25 09:06
    关注

    Here is working solution but it doesn't cover the cases where stings don't contain = or have more than 1 of them..

    $strings = array(
        '123.animal=cat',
        '123.name=fred',
        '123.food=fish',
        '345.animal=dog',
        'petshop=zoonoria'
    );
    $array = array();
    foreach ( $strings as $str) {
            // I accept that there is 1 = at strings possible 
            $exp = explode ('=', $str);
            if(count($exp) == 2){
                    $exp2 = explode('.', $exp[0]);
                    if( count($exp2) == 2 ) {
                            $array[$exp2[0]][$exp2[1]] = $exp[1];
                    } else {
                            // if there are multiple/null dots i set the string as key
                            $array[$exp[0]] = $exp[1];
                    }
            } else {
                    // what to do if there are no or many  = = = in the string ?
            }
    
    }
    var_dump($array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?