Php Data foreach

Source: Internet
Author: User
1. The data structure is as follows: {code...}. How can I use foreach to traverse the data structure? After the traversal is complete, save the expected result as an array: $ arr [& #039; B _id & #039;] [& #039; cat_list & #039;] [& #039; 1 & #039;]; $ arr [& #039; B _id & #039;] [& #039; cat_list & #039;] [& #039; 2 & #039;]... 1. The data structure is as follows:


  (store,img_parth,cat_list=>array(1=>array(),2=>array()),1=>(store,img_parth,cat_list=>array(1=>array(),2=>array())));

How can I use foreach to traverse the data structure? Save as an array after traversing

The expected result is as follows:$arr['b_id']['cat_list']['1'];$arr['b_id']['cat_list']['2'] ...

Reply content:

1. The data structure is as follows:


  (store,img_parth,cat_list=>array(1=>array(),2=>array()),1=>(store,img_parth,cat_list=>array(1=>array(),2=>array())));

How can I use foreach to traverse the data structure? Save as an array after traversing

The expected result is as follows:$arr['b_id']['cat_list']['1'];$arr['b_id']['cat_list']['2'] ...

$ Arr = array ('B _ id', 'store', 'img _ parth', 'cat _ list' => array (1 => array (), 2 => array ()));

Foreach ($ arr as $ k => $ v)
{

if(!is_array($v)){    $data[] = $v;}else{    foreach($v as $c_k => $c_v)    {        foreach($data as $v1)        {            $result[$v1][$c_k] = $c_v;        }    }}

}

Var_dump ($ result );

I'm not sure about the order. It's easy to write.

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.