Multidimensional Array to String

Multidimensional Array to String

You are overwriting $array, which contains the original array. But in a foreach a copy of $array is being worked on, so you are basically just assigning a new variable.

What you should do is iterate through the child arrays and "convert" them to strings, then implode the result.

function convert_multi_array($array) {
$out = implode("&",array_map(function($a) {return implode("~",$a);},$array));
print_r($out);
}

How can I declare a two dimensional string array?

string[,] Tablero = new string[3,3];

You can also instantiate it in the same line with array initializer syntax as follows:

string[,] Tablero = new string[3, 3] {{"a","b","c"},
{"d","e","f"},
{"g","h","i"} };

Convert string to a multidimensional array

If the string is certain to be secure, the simplest would be to concatenat [ and ] to the beginning and end, and then eval it.

var arr = eval("[" + myString + "]");

If you wanted greater safety, use double quotes for your strings, and use JSON.parse() in the same way.

var myString = '["Item", "Count"],["iPad",2],["Android",1]';

var arr = JSON.parse("[" + myString + "]");

This will limit you to supported JSON data types, but given your example string, it'll work fine.

Multidimensional array to string conversion in PHP

use array_column with implode function in php

implode(',',array_column($a, 'pk_id'));

convert two-dimensional array to string

If you want to create one-line representation of array you can use Arrays.deepToString.


In case you want to create multi-line representation you will probably need to iterate over all rows and append result of Array.toString(array[row]) like

String[][] array = { { "a", "b" }, { "c" } };

String lineSeparator = System.lineSeparator();
StringBuilder sb = new StringBuilder();

for (String[] row : array) {
sb.append(Arrays.toString(row))
.append(lineSeparator);
}

String result = sb.toString();

Since Java 8 you can even use StringJoiner with will automatically add delimiter for you:

StringJoiner sj = new StringJoiner(System.lineSeparator());
for (String[] row : array) {
sj.add(Arrays.toString(row));
}
String result = sj.toString();

or using streams

String result = Arrays
.stream(array)
.map(Arrays::toString)
.collect(Collectors.joining(System.lineSeparator()));

Multidimensional Array to String conversion

You can simply use array_walk_recursive like as

$result = [];
array_walk_recursive($arr, function($v) use (&$result) {
$result[] = $v;
});
echo implode('&', $result);

Demo

Php Convert Multidimensional array to string

If your array looks like this, then the foreach should create the array your looking for.

array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)

The following php will output array(2,4,7);

<?php

$aNewArray = array();

foreach($aArray as $aArray){

$aNewArray[] = $aArray['customer_id'];

}

var_dump($aNewArray);

?>

You dont need a multidimensional array for this though.



Related Topics



Leave a reply



Submit