【问题标题】:Transform an array in String. Result => "array...array"在 String 中转换数组。结果=>“数组...数组”
【发布时间】:2013-11-01 10:24:58
【问题描述】:

我想用 implode 将这个数组转换成 String :

Array ( [0] => Array ( [technologies_id] => 1 [nom] => PHP [type_technologies_id] => 1 [nom_techno] => Developpement web ) [1] => Array ( [technologies_id] => 3 [nom] => ASP3 [type_technologies_id] => 1 [nom_techno] => Developpement web ) [2] => Array ( [technologies_id] => 5 [nom] => JavaScript [type_technologies_id] => 1 [nom_techno] => Developpement web ) [3] => Array ( [technologies_id] => 6 [nom] => CSS [type_technologies_id] => 1 [nom_techno] => Developpement web ) [4] => Array ( [technologies_id] => 7 [nom] => AJAX [type_technologies_id] => 1 [nom_techno] => Developpement web ) [5] => Array ( [technologies_id] => 17 [nom] => HTML [type_technologies_id] => 1 [nom_techno] => Developpement web ) ) Array ( [0] => Array ( [technologies_id] => 8 [nom] => Jquery [type_technologies_id] => 2 [nom_techno] => Frameworks web ) ) Array ( [0] => Array ( [technologies_id] => 22 [nom] => VB 6 [type_technologies_id] => 3 [nom_techno] => Developpement applicatif ) [1] => Array ( [technologies_id] => 23 [nom] => VB.NET [type_technologies_id] => 3 [nom_techno] => Developpement applicatif ) ) Array ( ) Array ( ) Array ( [0] => Array ( [technologies_id] => 28 [nom] => MySQL [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) [1] => Array ( [technologies_id] => 30 [nom] => SQL Server Express [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) [2] => Array ( [technologies_id] => 32 [nom] => SQLite [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) [3] => Array ( [technologies_id] => 34 [nom] => Microsoft Access [type_technologies_id] => 6 [nom_techno] => SGDB(R) ) ) Array ( [0] => Array ( [technologies_id] => 39 [nom] => SQL [type_technologies_id] => 7 [nom_techno] => Langages d'interrogation de bases de donnees ) ) Array ( [0] => Array ( [technologies_id] => 46 [nom] => Windows [type_technologies_id] => 9 [nom_techno] => OS ) [1] => Array ( [technologies_id] => 49 [nom] => MAC OS [type_technologies_id] => 9 [nom_techno] => OS ) ) Array ( [0] => Array ( [technologies_id] => 54 [nom] => Merise [type_technologies_id] => 11 [nom_techno] => Methodologies ) ) 

我试过这样:

$string_comp = implode($competences);

但我得到了这个:

阵列阵列阵列阵列阵列

提前感谢您的帮助

【问题讨论】:

  • 你有一个多维数组,一个数组数组。 Implode 仅适用于数组的 1 级。

标签: php


【解决方案1】:

试试这个自定义函数:)

function multi_implode($sep, $array)
{
    $_array = array();

    foreach($array as $val)
    {
        if(is_array($val)) $_array[] = multi_implode($sep, $val);
        else $_array[] = $val;
    }

    return implode($sep, $_array);
}

您的内爆不起作用,因为您有一个多阵列。

【讨论】:

  • 当我调用这个函数时,我要发送什么分隔符?谢谢
  • @beegees,你想要什么 :) 例如,输入 ' '(空格)或 ','(逗号)。 $sep 是数组的分隔符。
【解决方案2】:

这样试试->

$array = array(1,2,3,4);
echo serialize($array);

// Prints

a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:4;}

【讨论】:

    【解决方案3】:

    就用这个吧:

    $res = '';
    foreach ($arr as $item) {
        $res .= implode('', $item);
    }
    

    【讨论】:

    • 或者在一行中:$str = implode(array_map('implode', $array))
    【解决方案4】:

    要以字符串格式获取输出,请使用 php 的 serialize() 函数 您也可以将unserialize() 设置为之前的状态。

    【讨论】:

      【解决方案5】:

      取决于您希望如何访问它,但就个人而言,我只需使用json_encode($myvar); 将其转换为 JSON。或者看看 Stackoverflow 上提供的这个答案:Multidimensional Array PHP Implode

      【讨论】:

        【解决方案6】:

        你有一个数组数组。您必须像这样内爆每个子数组:

        foreach ($competences as $subArray)
        {
            $string_comp .= implode($subArray);
        }
        

        【讨论】:

        • @beegees 对不起,我忘记了foreach 语法。我已经进行了适当的更正。
        猜你喜欢
        • 2011-04-06
        • 2015-04-09
        • 1970-01-01
        • 2011-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-09
        相关资源
        最近更新 更多