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

时间:2015-11-25 08:55:55

标签: php arrays

我正在解析一个看起来或多或少像这样的文本文件:

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

这就是我解析它的方式:

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

我的结果是:

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"
  }
}

但我需要数组不同,这是我想要实现的结果:

  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"
      }
    }

所以我的主要问题是,我不知道如何将number转换为密钥。我尝试了各种各样的东西,但我失败了。我很满意每一个提示。

我尝试了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 {

        }

}

我的结果:

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 个答案:

答案 0 :(得分:2)

这是一个有效的解决方案,但它不包括蜇伤不包含=或者有多于一个的情况。

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

答案 1 :(得分:1)

获得结果后,您可以使用array_reduce获得所需的结果

$result = array_reduce($initialArray, function ($result, $item) {
    $number = isset($item['number']) ? $item['number'] : end(array_keys($result));

    if (!isset($result[$number])) {
        $result[$number] = array();
    }
    foreach ($item as $key => $value) {
        if ($key == 'number') continue;
        $result[$number][$key] = $value;
    }
    return $result;
}, array());

答案 2 :(得分:1)

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) { 
        if ($type && $ident) { 
                $properties[$ident][$type] = $value; 
            } else if (!$type && $ident) { 
                $properties[$ident][] = $value;
            }else if ($type && !$ident){
                $properties[$type][] = $value;
            }

    }

答案 3 :(得分:1)

if($this->Paginator->current($model) != $last) {
  echo $this->Paginator->last($last, array('rest of options'));
}

Demo

在您的情况下,使用$data = '123.animal=cat 123.name=fred 123.food=fish 345.animal=dog petshop=zoonoria'; $ini = parse_ini_string($data); $result = []; foreach ($ini as $key => $value) { $splitKey = explode('.', $key); $iniPtr = &$result; foreach($splitKey as $subKey) { if (!isset($iniPtr[$subKey])) { $iniPtr[$subKey] = null; } $iniPtr = &$iniPtr[$subKey]; } $iniPtr = $value; } unset($iniPtr); var_dump($result); 将您的文件数据读入$ini = parse_ini_file($file_path);

相关问题