PHP saves two-dimensional arrays to a one-dimensional array tutorial _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.