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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝