The problem of thinkphp array deformation

Source: Internet
Author: User
Once there was such an array

Array (    [id] = = array        (            [0] = 500002-016            [1] = 500471-012        )    [name] = = Array        (            [0] = home-made drug stent (domestic) [limit]            [1] = one-time positive pressure needle-free connection type indwelling needle (domestic) [B 10%]        )    [specification] = = Array        (            [0] = y            [1] = X        )    [Quantity] = = Array        (            [0] =            [1] = +        ))

Want to become such an array

Array (    [0] = = Array        (            [id] = 500002-016            [name] + le PU domestic drug stent (domestic) [Quota] [            specification ] = = y            [quantity] [    1] = = Array        (            [id] = 500471-012            [name] = = One-time positive pressure needle-free connection type indwelling needle (domestic) [B 10%]            [specification] + x            [Quantity] [] =)        )

Or ask Thinkphp's master. How to Volist an array of the first format to a table in the view what good way do you have?

Reply content:

Once there was such an array

Array (    [id] = = array        (            [0] = 500002-016            [1] = 500471-012        )    [name] = = Array        (            [0] = home-made drug stent (domestic) [limit]            [1] = one-time positive pressure needle-free connection type indwelling needle (domestic) [B 10%]        )    [specification] = = Array        (            [0] = y            [1] = X        )    [Quantity] = = Array        (            [0] =            [1] = +        ))

Want to become such an array

Array (    [0] = = Array        (            [id] = 500002-016            [name] +-Domestic drug stent (domestic) [limit] [            specification] = > y            [Quantity] = [    1] = = Array        (            [id] = 500471-012            [name] + = One-time positive pressure needle-free connection type indwelling needle (domestic) [B 10%]            [specification] + x            [Quantity] [] =)        )

Or ask Thinkphp's master. How to Volist an array of the first format to a table in the view what good way do you have?

Easy to understand, I would write like this.

function Rebuild ($data) {$result = array (); $keys = Array_keys ($data); $num = count ($data [' id ']); for ($i = 0; $i < $num; + + $i) {$item = Array (), foreach ($keys as $key) {$item [$key] = $data [$key] [$i];} $result [] = $item;} return $result;}

This one... The only way I can think of is foreach.

function Arr_format ($arr) {    $res = array ();    foreach ($arr as $k + $v) {        foreach ($v as $kk + = $VV) {            $res [$KK] [$k] = $VV;        }    }    return $res;}

$data = your data; $temp = array ();//Save the changed data foreach ($data [' id '] as $key = = $val) {    Array_push ($temp, Array (                              ' id ' = > $val,                              ' name ' and ' = ' $data [' name '] [$key],                              ' specification ' + $data [' specification '] [$key],                              ' Quantity ' = $data [' Quantity '] [$key])              );} Unset ($data);

Come in a different way if the field is fixed.

function Arr_format ($arr) {    $result = array ();    List ($id, $name, $specification, $quantity) = Array_values ($arr);    for ($i = 0, $count = count ($id), $i < $count, $i + +) {        $result [] = Array (            ' id ' = = $id [$i],            ' name ' = = $na me[$i],            ' specification ' = $specification [$i],            ' quantity ' = $quantity [$i]        );    }    return $result;}
  • Related Article

    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.