Home  >  Article  >  Backend Development  >  PHP save two-dimensional array to one-dimensional array tutorial_PHP tutorial

PHP save two-dimensional array to one-dimensional array tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:07:421302browse

php tutorial save two-dimensional array to one-dimensional array tutorial
$array = array(
array(1,2),
array(3,4),
array('www .bkjia.com','php100.com')
);

//See the above two-dimensional number structure, let’s use foreach to example

function array_2to1( $array)
{
static $result_array=array();
foreach($array as $value)
{
if(is_array($value))
{
arrau_2to1($value);
} }
else
                                                                                                     🎜>
//Above This code can also be abbreviated as

function _rebuild_array($arr){ //rebuild a array

static $tmp=array();

for($i=0; $i< ;count($arr); $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 saving one-dimensional data 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');

//Now we write the two arrays into a new two-dimensional array in one-to-one correspondence

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

http: //www.bkjia.com/PHPjc/444948.htmlTechArticlephp tutorial save two-dimensional array to one-dimensional array tutorial $array = array( array(1,2), array(3,4), array('www.bkjia.com','php100.com') ); //See the two-dimensional number structure above, let's...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn