PHP 数组到字符串作为参数

问题描述 投票:0回答:5

我想将数组值转换为字符串。我应该使用什么来加入目标中提出的他们?

我应该使用

serialize()
implode()
http_build_query()
array_walk()
吗?

$attributes = array(
    'autocomplete' => 'off',
    'class' => 'email',
    'id' => 'myform'
);

echo http_build_query($attributes, '', '" ');

// output
autocomplete=off" class=email" id=myform

目标:

// output 
autocomplete="off" class="email" id="myform"

编辑:

我用

array_walk()
来实现目标

function myfunction($value, $key) {
    echo $key . '=" ' . $value . ' " ';
}

array_walk($atributes, "myfunction");
php arrays implode
5个回答
2
投票

如果您想确保返回完全相同的数组,则必须使用

serialize
(因为它将保留变量类型)和
unserialize
来返回数据。或者,
json_decode
json_encode
也可以工作(但仅维护简单类型,如 int/float/string/boolean/NULL)。不过,数据将大于
implode
http_build_query

示例:

考虑以下数组:

$array = array(
    'foo' => 'bar',
    'bar' => false,
    'rab' => null,
    'oof' => 123.45
);

serialize
/
unserialize
:

<?php
    var_dump( unserialize( serialize($array) ) );
    /*
        array(4) {
            ["foo"] => string(3) "bar"
            ["bar"] => bool(false)
            ["rab"] => NULL
            ["oof"] => float(123.45)
        }
    */
?>

implode
/
explode
:

<?php
    var_dump( explode('&', implode('&', $array) ) );
    /*
        array(4) {
            [0] => string(3) "bar"
            [1] => string(0) ""
            [2] => string(0) ""
            [3] => string(6) "123.45"
        }
    */
?>

json_encode
/
json_decode
:

<?php
    var_dump( json_decode( json_encode($array) , true) );
    /*
        array(4) {
            ["foo"] => string(3) "bar"
            ["bar"] => bool(false)
            ["rab"] => NULL
            ["oof"] => float(123.45)
        }
    */
?>

http_build_query
/
parse_str
:

<?php
    parse_str( http_build_query($array) , $params);
    var_dump( $params );
    /*
        array(3) {
            ["foo"] => string(3) "bar"
            ["bar"] => string(1) "0"
            ["oof"] => string(6) "123.45"
        }
    */
?>

演示


1
投票

http_build_query
是这里最好的选择,因为你有
key=>value
组合


0
投票

看起来您想将它们组合成一个字符串,以便在 HTML 标签上输出。

这个可重用的函数应该会产生您想要的结果:

function get_attribute_string($attr_array) {
    $attributes_processed = array();
    foreach($attr_array as $key => $value) {
        $attributes_processed[] = $key . '="' . $value . '"';
    }

    return implode($attributes_processed, ' ');
}

$atributes = array(
    'autocomplete' => 'off',
    'class' => 'email',
    'id' => 'myform'
);

// this string will contain your goal output
$attributes_string = get_attribute_string($atributes);

附注

atributes
应该有三个 T -
attributes
- 请注意这不会让您出局!


0
投票

使用

array_walk()
来实现目标

function myfunction($value, $key) {
    echo $key . '="' . $value . '" ';
}

array_walk($atributes, "myfunction");

0
投票

如果您正在构建带引号的 HTML 元素属性声明,您可能应该对可能干扰文档有效性的实体进行编码。

如何循环并不重要,找到一种你觉得舒服并且将来能够保持的风格。

代码:(演示

echo implode(
         ' ',
         array_map(
             fn($k, $v) => sprintf(
                 '%s="%s"',
                 $k,
                 htmlspecialchars(
                     $v,
                     ENT_QUOTES,
                     'UTF-8'
                 )
             ),
             array_keys($attributes),
             $attributes
         )
      );

输入:

$attributes = array(
    'autocomplete' => 'off',
    'class' => 'email',
    'id' => 'myform',
    'value' => 'foo "fizz buzz" bar',
);

输出:

autocomplete="off" class="email" id="myform" value="foo &quot;fizz buzz&quot; bar"
© www.soinside.com 2019 - 2024. All rights reserved.