Jump to content

reconstructing an array.


Michdd

Recommended Posts

I use this code to break apart an array into a string:

 

$count = count($array);
$count2 = count($array[0]);
for ( $i=0; $i < $count; $i++ ) {
for ( $i=0; $j < $count2; $j++ ) {
	$mapStr = $mapStr . outArr[$i][$j] . "-";
}
$mapStr = $mapStr . "_";  
}

It's a 2 dimensional array, then it's broken down into a string. What code would I use if I wanted to reconstruct the string generated from this back into an array?

Link to comment
https://forums.phpfreaks.com/topic/148155-reconstructing-an-array/
Share on other sites

To clean up yours:

 

<?php
foreach($array as &$value) {
$value = implode('-', $value);
}
$mapStr = implode('_', $array);

 

To do the opposite:

 

<?php
$array = explode('_', $mapStr);
foreach($array as &$value) {
$value = explode('-', $value);
}

 

There are shorter ways of doing it, but this is the cleanest I can think of. The best way to break down an array into a string though is to use serialize.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.