ThinkPHP array deformation

Source: Internet
Author: User
Previously there was such an array {code ...} to convert it into an array {code ...} could you tell me how to volist the array in the first format of ThinkPHP to the table in the view? Do you have any good solutions? Thank you ~ Once upon a time there was such an array

Array ([id] => Array ([0] => 500002-016 [1] => 500471-012) [name] => Array ([0] => domestic drug stent (made in China) [quota] [1] => disposable positive pressure needle-free connecting indwelling needle (made in China) [B 10%]) [specification] => Array ([0] => y [1] => x) [quantity] => Array ([0] => 22 [1] => 23 ))

Want to change to such an array

Array ([0] => Array ([id] => 500002-016 [name] => domestic drug stent (made in China) [quota] [specification] => y [quantity] => 22) [1] => Array ([id] => 500471-012 [name] => disposable positive pressure needle-less connector indwelling needle (made in China) [B 10%] [specification] => x [quantity] => 23 ))

Could you tell me how to volist the array in the first format of ThinkPHP to the table in the view? Do you have any good solutions? Thank you ~

Reply content:

Once upon a time there was such an array

Array ([id] => Array ([0] => 500002-016 [1] => 500471-012) [name] => Array ([0] => domestic drug stent (made in China) [quota] [1] => disposable positive pressure needle-free connecting indwelling needle (made in China) [B 10%]) [specification] => Array ([0] => y [1] => x) [quantity] => Array ([0] => 22 [1] => 23 ))

Want to change to such an array

Array ([0] => Array ([id] => 500002-016 [name] => domestic drug stent (made in China) [quota] [specification] => y [quantity] => 22) [1] => Array ([id] => 500471-012 [name] => disposable positive pressure needle-less connector indwelling needle (made in China) [B 10%] [specification] => x [quantity] => 23 ))

Could you tell me how to volist the array in the first format of ThinkPHP to the table in the view? Do you have any good solutions? Thank you ~

This is easy to understand.

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... The only method I have come up with 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' => $ data ['name'] [$ key], 'specification '=> $ data ['specification'] [$ key], 'quantity '=> $ data ['quantity'] [$ key]);} unset ($ data );

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' => $name[$i],            'specification' => $specification[$i],            'quantity' => $quantity[$i]        );    }    return $result;}

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.