从此文本创建一个简单的PHP数组

时间:2015-08-02 22:29:52

标签: php arrays

我在XML元素中找到了这个可爱的文本块,并希望将其转换为简单的数组,XML来自wikia.com。

阵列将是这样的:

card name => [Martial] Ares, character name => Ares, release_date => May 1 2013 and so on..

我尝试了各种爆炸和爆炸变化,但没有运气..这个让我难过......

|card name=[Martial] Ares
|character name=Ares
|release_date=May 1 2013
|image 1=MartialAres5.jpg
|rarity 1=Super Special Rare
|pwr req 1=28
|sale price 1=94200
|max card lv 1=60
|max mastery lv 1=40
|quote 1=Ares prefers weapons that were used during the age of Greek myth: sword, axe, and spear.  But he can use any weapon expertly, and turn most ordinary objects into lethal weapons.
|base atk 1=2440
|base def 1=2650
|max atk 1=7015
|max def 1=7613
|mastery bonus atk 1=915
|mastery bonus def 1=993
|image 2=MartialAres6.jpg
|rarity 2=Ultimate Rare
|sale price 2=188400
|max mastery lv 2=200
|quote 2=Next time I see Hercules, We're going to have a steel conversation. It's about time for him to answer for massacring my Stymphalian Birds.
|max atk 2=9822
|max def 2=10660
|mastery bonus atk 2=1098
|mastery bonus def 2=1192
|alignment=Bruiser
|ability=Warhawk
|gender=Male
|usage=Average
|faction=Super Hero
|effect=Significantly harden DEF of your Bruisers.
|centretrait=None

我试过的代码:

if (file_exists('card.xml')) {
    $xml = simplexml_load_file('card.xml');


    $text = $xml->page->revision->text;
    $newtext = explode('|', $text);
    foreach($newtext as $newnewtext) {
        $newtext2 = explode('=', $newnewtext);
        print_r($newtext2);

    }


} else {
    exit('Failed to open card.xml.');
}

6 个答案:

答案 0 :(得分:5)

按要求:

<?php
$file="|card name=[Martial] Ares
|character name=Ares
|release_date=May 1 2013
|image 1=MartialAres5.jpg
|rarity 1=Super Special Rare
|pwr req 1=28
|sale price 1=94200
|max card lv 1=60
|max mastery lv 1=40
|quote 1=Ares prefers weapons that were used during the age of Greek myth: sword, axe, and spear.  But he can use any weapon expertly, and turn most ordinary objects into lethal weapons.
|base atk 1=2440
|base def 1=2650
|max atk 1=7015
|max def 1=7613
|mastery bonus atk 1=915
|mastery bonus def 1=993
|image 2=MartialAres6.jpg
|rarity 2=Ultimate Rare
|sale price 2=188400
|max mastery lv 2=200
|quote 2=Next time I see Hercules, We're going to have a steel conversation. It's about time for him to answer for massacring my Stymphalian Birds.
|max atk 2=9822
|max def 2=10660
|mastery bonus atk 2=1098
|mastery bonus def 2=1192
|alignment=Bruiser
|ability=Warhawk
|gender=Male
|usage=Average
|faction=Super Hero
|effect=Significantly harden DEF of your Bruisers.
|centretrait=None";


$x=explode("\n",$file);
$out=array();
foreach($x as $each){
  $xx=explode('=',$each);
 $out[ltrim($xx[0],'|')]=$xx[1]; 
}
echo '<pre>';
print_r($out);

工作演示:http://codepad.viper-7.com/3BkXD6

答案 1 :(得分:1)

实现这一目标的最简单,最有效的方法是使用正则表达式。鉴于 $ string 保存数据,请执行以下操作:

preg_match_all('/^\|(?<key>[^=]+)=(?<value>.*)$/m', $string, $matches);
print_r(array_combine($matches['key'], $matches['value']));

对于您提供的数据,您将获得:

Array
(
  [card name] => [Martial] Ares
  [character name] => Ares
  [release_date] => May 1 2013
  [image 1] => MartialAres5.jpg
  [rarity 1] => Super Special Rare
  [pwr req 1] => 28
  [sale price 1] => 94200
  [max card lv 1] => 60
  [max mastery lv 1] => 40
  ...and so on
)

答案 2 :(得分:0)

这应该适合你:

首先explode()你的字符串换一行。然后使用array_map()浏览每个元素,并使用等号1的偏移量展开substr()

最后,只需使用array_column()将第一列用作值,将0列用作键。

$arr = array_column(array_map(function($v){
    return explode("=", substr($v, 1));
}, explode(PHP_EOL, $str)), 1, 0);

print_r($arr);

答案 3 :(得分:0)

@Dagon是正确的。这就是实现的外观:

<?php
$keyvalues = array();
$text = file_get_contents('path/to/your/file');
$rows = explode('|',$text);
foereach($rows as $row) {
    if (strpos($row,'=')) {
        $kv = array_map('trim',explode('=',$row));
        $keyvalues[ $kv[0] ] = $kv[1];
    }
}
?>

答案 4 :(得分:0)

首先,我会将字符串拆分为'|'系统字符。

$ parts = explode(“|”,$ str);

这将产生如下数组:

newString = oldString[6:len(oldString)-6]

接下来,我将遍历数组并将每个字符串拆分为“=”字符,并从这些字符构建一个关联数组。

    Array
    (
        [0] => 
        [1] => card name=[Martial] Ares

        [2] => character name=Ares

        [3] => release_date=May 1 2013

        [4] => image 1=MartialAres5.jpg

        [5] => rarity 1=Super Special Rare

        [6] => pwr req 1=28

        [7] => sale price 1=94200

        [8] => max card lv 1=60

        [9] => max mastery lv 1=40

        [10] => quote 1=Ares prefers weapons that were used during the age of Greek myth: sword, axe, and spear.  But he can use any weapon expertly, and turn most ordinary objects into lethal weapons.

        [11] => base atk 1=2440

        [12] => base def 1=2650

        [13] => max atk 1=7015

        [14] => max def 1=7613

        [15] => mastery bonus atk 1=915

        [16] => mastery bonus def 1=993

        [17] => image 2=MartialAres6.jpg

        [18] => rarity 2=Ultimate Rare

        [19] => sale price 2=188400

        [20] => max mastery lv 2=200

        [21] => quote 2=Next time I see Hercules, We're going to have a steel conversation. It's about time for him to answer for massacring my Stymphalian Birds.

        [22] => max atk 2=9822

        [23] => max def 2=10660

        [24] => mastery bonus atk 2=1098

        [25] => mastery bonus def 2=1192

        [26] => alignment=Bruiser

        [27] => ability=Warhawk

        [28] => gender=Male

        [29] => usage=Average

        [30] => faction=Super Hero

        [31] => effect=Significantly harden DEF of your Bruisers.

        [32] => centretrait=None
    )

这应该会产生你正在寻找的结构。

    //First remove the first empty element from the array.
    array_shift($parts);

    $finalArray = array();
    foreach($parts as $part)
    {
        $b = explode("=", $part);
        $finalArray[$b[0]] = $b[1];
    }

答案 5 :(得分:0)

使用最小的错误检查:

Array
(
    [card name] => [Martial] Ares

    [character name] => Ares

    [release_date] => May 1 2013

    [image 1] => MartialAres5.jpg

    [rarity 1] => Super Special Rare

    [pwr req 1] => 28

    [sale price 1] => 94200

    [max card lv 1] => 60

    [max mastery lv 1] => 40

    [quote 1] => Ares prefers weapons that were used during the age of Greek myth: sword, axe, and spear.  But he can use any weapon expertly, and turn most ordinary objects into lethal weapons.

    [base atk 1] => 2440

    [base def 1] => 2650

    [max atk 1] => 7015

    [max def 1] => 7613

    [mastery bonus atk 1] => 915

    [mastery bonus def 1] => 993

    [image 2] => MartialAres6.jpg

    [rarity 2] => Ultimate Rare

    [sale price 2] => 188400

    [max mastery lv 2] => 200

    [quote 2] => Next time I see Hercules, We're going to have a steel conversation. It's about time for him to answer for massacring my Stymphalian Birds.

    [max atk 2] => 9822

    [max def 2] => 10660

    [mastery bonus atk 2] => 1098

    [mastery bonus def 2] => 1192

    [alignment] => Bruiser

    [ability] => Warhawk

    [gender] => Male

    [usage] => Average

    [faction] => Super Hero

    [effect] => Significantly harden DEF of your Bruisers.

    [centretrait] => None
)
相关问题