PHP Tutorial to save a two-dimensional array to a one-dimensional array tutorial
$array = Array (
Array (),
Array (3,4),
Array (' www.bkjia.com ', ' php100.com ')
);
See the two-dimensional structure above, let's use a foreach example
function Array_2to1 ($array)
{
Static $result _array=array ();
foreach ($array as $value)
{
if (Is_array ($value))
{
Arrau_2to1 ($value);
}
Else
$result _array[]= $value;
}
return $result _array;
}
The above code can also be abbreviated
function _rebuild_array ($arr) {//rebuild a array
Static $tmp =array ();
for ($i =0; $i
if (Is_array ($arr [$i])) _rebuild_array ($arr [$i]);
else $tmp []= $arr [$i];
}
return $tmp;
}
$arr = Array_2to1 ($array);
foreach ($arr as $v)
{
Echo $v;
}
Now, let's look at an example of the inverse one-dimensional data saved to two-dimensional data
$arr _new=array ();
$insert _key =array (' uid ', ' hostname ', ' shopname ', ' Province ', ' City ', ' County ', ' address ', ' www.bkjia.com ', ' QQ ', ' Mobile ', ' MSN ');
$insert _value=array (' 2 ', ' hostname ', ' shopname ', ' Province ', ' City ', ' www.bkjia.com ', ' address ', ' shopimg ', ' QQ ', ' Mobile ', ' MSN ');
One in which we write 2 array one by one corresponding to a new two-dimensional array
foreach ($insert _key as $key = + $val) {
$arr _new[$val]= $insert _value[$key];
}
Print_r ($arr _new);
http://www.bkjia.com/PHPjc/444948.html www.bkjia.com true http://www.bkjia.com/PHPjc/444948.html techarticle The PHP tutorial saves a two-dimensional array to a one-dimensional array tutorial $array = Array (array), array (3,4), array (' www.bkjia.com ', ' php100.com '));//See the two-dimensional structure above, Below we ...