将数据从数组传递到变量PHP

时间:2013-08-30 10:40:44

标签: php arrays

我得到一个数组作为参数,例如

$data = array ( 'name' => 'makis', 'pw' => 'sovara');

我想用我得到的数组中的数据完成下面的变量$ nxml。 'name'和'pw'。

        $nxml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
            epp-1.0.xsd">
            <command>
            <login>
            <clID>$data['name']</clID>
            <pw>$data['pw']</pw>
            <options>
            <version>1.0</version>
            <lang>en</lang>
            </options>
            <svcs>
            <objURI>urn:ietf:params:xml:ns:host-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
            </svcs>
            </login>
            <clTRID>nick-12345</clTRID>
            </command>
            </epp>';

这个原因导致错误的正确方法是什么

7 个答案:

答案 0 :(得分:2)

你可以试试这个:

$nxml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
                epp-1.0.xsd">
                <command>
                <login>
                <clID>'.$data['name'].'</clID> //your original code <clID>$data['name']</clID> see the difference?
                <pw>'.$data['pw'].'</pw> //same thing here
                <options>
                <version>1.0</version>
                <lang>en</lang>
                </options>
                <svcs>
                <objURI>urn:ietf:params:xml:ns:host-1.0</objURI>
                <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
                <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
                </svcs>
                </login>
                <clTRID>nick-12345</clTRID>
                </command>
                </epp>';

您需要正确转义'才能使其正常工作。

答案 1 :(得分:1)

像这样:

$nxml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
            epp-1.0.xsd">
            <command>
            <login>
            <clID>'.$data['name'].'</clID>
            <pw>'.$data['pw'].'</pw>
            <options>
            <version>1.0</version>
            <lang>en</lang>
            </options>
            <svcs>
            <objURI>urn:ietf:params:xml:ns:host-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
            </svcs>
            </login>
            <clTRID>nick-12345</clTRID>
            </command>
            </epp>';

单引号在这种情况下不起作用。如果要显示变量的值,请使用双引号,但这会导致您使用转义符号,因此我建议您使用单引号连接以避免复杂性。

答案 2 :(得分:0)

$nxml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
            epp-1.0.xsd">
            <command>
            <login>
            <clID>'.$data['name'].'</clID>
            <pw>'.$data['pw'].'</pw>
            <options>
            <version>1.0</version>
            <lang>en</lang>
            </options>
            <svcs>
            <objURI>urn:ietf:params:xml:ns:host-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
            </svcs>
            </login>
            <clTRID>nick-12345</clTRID>
            </command>
            </epp>';

答案 3 :(得分:0)

尝试

$nxml = '....<clID>'.$data['name'].'</clID>
        <pw>'.$data['pw'].'</pw>....';

然后你的整个代码将是

$nxml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
        epp-1.0.xsd">
        <command>
        <login>
        <clID>'.$data['name'].'</clID>
        <pw>'.$data['pw'].'</pw>
        <options>
        <version>1.0</version>
        <lang>en</lang>
        </options>
        <svcs>
        <objURI>urn:ietf:params:xml:ns:host-1.0</objURI>
        <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
        <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
        </svcs>
        </login>
        <clTRID>nick-12345</clTRID>
        </command>
        </epp>';

答案 4 :(得分:0)

试试这样。因为你的esacping不正确

$data = array ( 'name' => 'makis', 'pw' => 'sovara');
 $nxml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
            epp-1.0.xsd">
            <command>
            <login>
            <clID>$data["name"]</clID>
            <pw>$data["pw"]</pw>
            <options>
            <version>1.0</version>
            <lang>en</lang>
            </options>
            <svcs>
            <objURI>urn:ietf:params:xml:ns:host-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:contact-1.0</objURI>
            <objURI>urn:ietf:params:xml:ns:domain-1.0</objURI>
            </svcs>
            </login>
            <clTRID>nick-12345</clTRID>
            </command>
            </epp>';

答案 5 :(得分:0)

试试这个:

<clID>"'.$data['name'].'"</clID>
<pw>"'.$data['pw'].'"</pw>

答案 6 :(得分:0)

试试这个

<clID>"'.$data['name'].'"</clID>
<pw>"'.$data['pw'].'"</pw>