PHP an array operation method solving solution

Source: Internet
Author: User
PHP An array manipulation method solves
One-dimensional arrays

Array (' 0 ' = ' a ',1=> ' B ',2=> ' C ',3=> ' d ',4=> ' e ',5=> ' f ');

It could be long.

I want to turn into

Array (' a ' = ' = ' B ', ' c ' = = ' d ', ' e ' = ' f ');

This is the first value as the key, the second value as the value, and so on, the method!
------to solve the idea----------------------
Landlord you do this, you have to ensure that your data must be even, or the final must be incomplete. When your data is even, you can traverse it

$arr = Array (' 0 ' = ' a ',1=> ' B ',2=> ' C ',3=> ' d ',4=> ' e ',5=> ' f ', ' 6 ' = ' g ', ' 7 ' = ' h ');
$i = 0;
$len = count ($arr);
$NEWARR = Array ();
while ($i < $len) {
$NEWARR [$arr [$i]] = $arr [$i +1];
$i + = 2;
}
Var_dump ($NEWARR);


------to solve the idea----------------------
Change the upstairs slightly. That should do it, no more odd even.

$arr = Array (' 0 ' ~ = ' a ',1=> ' B ',2=> ' C ',3=> ' d ',4=> ' e ',5=> ' f ', ' 6 ' = ' g ', ' 7 ' = ' h ', ' 8 ' = ' = ' i ');
$i = 0;
$len = count ($arr);
$NEWARR = Array ();
while ($i < $len) {
$NEWARR [$arr [$i]] = array_key_exists ($i +1, $arr)? $arr [$i +1]:null;
$i + = 2;
}
Var_dump ($NEWARR);


------to solve the idea----------------------
$a = Array (' 0 ' = ' a ',1=> ' B ',2=> ' C ',3=> ' d ',4=> ' e ',5=> ' f ', ' G ');
foreach (Array_chunk ($a, 2) as $t) {
$b [$t [0]] = @ $t [1];
}
Print_r ($b);

Array
(
[A] = b
[C] + = d
[E] = f

)

  • 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.