简体   繁体   English

从二维数组中内爆数据

[英]Implode data from a Two-dimensional array

I think it should not be so difficult to solve but i can't get the solution. 我认为解决起来不应该那么困难,但我无法得到解决方案。

I'm trying to implode an array in order to have this result : 我试图内爆一个数组,以便得到这个结果:

$new_array = "755, 646, 648, 260, 257, 261, 271, 764, ..."

Here is my array : 这是我的数组:

Array
(
    [0] => Array
        (
            [0] => 755
            [1] => 646
        )

    [1] => Array
        (
            [0] => 648
            [1] => 260
            [2] => 257
            [3] => 261
        )

    [2] => Array
        (
            [0] => 271
        )

    [3] => Array
        (
            [0] => 764
            [1] => 643
            [2] => 260
            [3] => 263
            [4] => 756
            [5] => 763
            [6] => 762
            [7] => 259
            [8] => 257
            [9] => 758
            [10] => 261
            [11] => 768
            [12] => 757
            [13] => 647
        )
)

I know how to do this with a simple array : 我知道如何用一个简单的数组做到这一点:

$newarray = implode(", ", $array);

I know how to get the key (with array_keys ) 我知道如何获取密钥(使用array_keys

$new_array = implode(", ", array_keys($array));

I've tried with array_map but without result for the moment. 我已经尝试过使用array_maparray_map没有结果。

Coud you help me and point me in the right direction ? Coud你帮助我并指出我正确的方向?

You have to merge inner arrays to one (if our multidimensional array is called $list ), we can: 你必须将内部数组合并为一个(如果我们的多维数组称为$list ),我们可以:

$merged = array_reduce($list, 'array_merge', array());

now you can easily implode it: 现在你可以很容易地破坏它:

$string = implode(', ', $merged);

echoing given string would print: 回显给定的字符串将打印:

755, 646, 648, 260, 257, 261, 271, 764, ... 755,646,648,260,257,261,271,764,......

To know you can't implode multidimensional array because it's elements are arrays itself so array to string conversion error may arise, thus we should make our array flat, for this purpose you can also check How to Flatten a Multidimensional Array? 要知道你不能破坏多维数组,因为它的元素是数组本身,因此可能会出现array to string conversion错误,因此我们应该使数组变平,为此你还可以检查如何展平多维数组? .

Also to know what array_reduce do is: 还要知道array_reduce作用是什么:

Iteratively reduce the array to a single value using a callback function.. see more 使用回调函数迭代地将数组减少为单个值。 查看更多

Simple solution using array_walk_recursive function: 使用array_walk_recursive函数的简单解决方案:

// $arr is your initial array
$new_arr = [];
array_walk_recursive($arr, function($v) use(&$new_arr){ $new_arr[] = $v; });

DEMO link DEMO链接

http://php.net/manual/en/function.array-walk-recursive.php http://php.net/manual/en/function.array-walk-recursive.php

try this: 试试这个:

<?php 
      $array = array
      (
      array(755,646),
      array(648,260,257,261),
      array(271),
      array(764,643,260,263,756,763,762,259,257,758,261,768,757,647)
      );
      echo "<pre>";
      print_r($array);
      foreach ($array as $value) {
        $newstring[]=implode(",",$value);
      }
      echo implode(",",$newstring);

    ?>

You can do it with foreach loop like above 您可以使用上面的foreach循环来完成它

With loop: 带循环:

$array = array(
    0 => array(
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ),
    1 => array(
        'name' => 'Jane Doe2',
        'email' => 'jane@example.com2'
    ),
);


//function to display values
function ImplodeDataValue($array) {
$tosinglearray=array();
for ($i = 0; $i <= count($array)-1; $i++) {
$tosinglearray[]=implode(',',$array[$i]);
}
return implode(',',$tosinglearray);
}

echo ImplodeDataValue($array);
//returns John Doe,john@example.com,Jane Doe2,jane@example.com2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM