Php multi-dimensional array problem, please solve it. Thank you.

Source: Internet
Author: User
{Code...} means that the jxs in the array are the same as their fd, fb, tcp, and fbdh. How can we convert the above array into the following result: GZYX3213BJCJ1010
$arr = array(                     array('id'=>7363,'fd'=>1,'fb'=>1,'tcp'=>0,'fbdh'=>1,'jxs'=>'GZYX'),    array('id'=>7412,'fd'=>1,'fb'=>0,'tcp'=>1,'fbdh'=>1,'jxs'=>'GZYX'),    array('id'=>7512,'fd'=>1,'fb'=>1,'tcp'=>0,'fbdh'=>1,'jxs'=>'GZYX'),    array('id'=>7516,'fd'=>1,'fb'=>0,'tcp'=>1,'fbdh'=>0,'jxs'=>'BJCJ'),);

The jxs in the array share the same fd, fb, tcp, and fbdh values.
How to convert the above array into the following result
Result:
GZYX 3 2 1 3
BJCJ 1 0 1 0

Reply content:
$arr = array(                     array('id'=>7363,'fd'=>1,'fb'=>1,'tcp'=>0,'fbdh'=>1,'jxs'=>'GZYX'),    array('id'=>7412,'fd'=>1,'fb'=>0,'tcp'=>1,'fbdh'=>1,'jxs'=>'GZYX'),    array('id'=>7512,'fd'=>1,'fb'=>1,'tcp'=>0,'fbdh'=>1,'jxs'=>'GZYX'),    array('id'=>7516,'fd'=>1,'fb'=>0,'tcp'=>1,'fbdh'=>0,'jxs'=>'BJCJ'),);

The jxs in the array share the same fd, fb, tcp, and fbdh values.
How to convert the above array into the following result
Result:
GZYX 3 2 1 3
BJCJ 1 0 1 0

/* Array of initialization operations */$ arr = array ('id' => 7363, 'fd '=> 1, 'fb' => 1, 'tcp '=> 0, 'fbdh' => 1, 'jxs '=> 'gzyx'), array ('id' => 7412, 'fd' => 1, 'fb '=> 0, 'tcp' => 1, 'fbdh '=> 1, 'jxs' => 'gzyx'), array ('id' => 7512, 'fd '=> 1, 'fb' => 1, 'tcp '=> 0, 'fbdh' => 1, 'jxs '=> 'gzyx '), array ('id' => 7516, 'fd '=> 1, 'fb' => 0, 'tcp '=> 1, 'fbdh' => 0, 'jxs '=> 'bjcj'),); $ res = array (); /* In Fact, I won't tell you that I don't need to use array_walk cyclically, just to look tall. */array_walk ($ arr, function ($ item, $ key) use (& $ re S) {/* determine whether the array exists for initialization */$ member = & $ res [$ item ['jxs ']; if (! Isset ($ member) {$ member = array ('fd '=> 0, 'fb' => 0, 'tcp '=> 0, 'fbdh '=> 0);}/* you can add the statistical results using array_walk */$ member ['fd'] + = $ item ['fd ']; $ member ['fb '] + = $ item ['fb']; $ member ['tcp '] + = $ item ['tcp']; $ member ['fbdh '] + = $ item ['fbdh'];});/* print the result */array_walk ($ res, function ($ item, $ key) {echo "$ key {$ item ['fd ']} {$ item ['fb']} {$ item ['tcp ']} {$ item ['fbdh']}
";});/** GZYX 3 2 1 3 * BJCJ 1 0 1 0 */

Because you gave me a piece of data, and I don't know if this is the subject's meaning, but I got the format you wanted. But I really want to talk about it. It is estimated that not many people can tell you what you are doing?

Do I not even read the code that I tried to call? I just added a set of data according to my requirements? It is also possible to add a group of tests, or does my code not solve your problem? It's really an insult to say nothing, ah!

Sure enough, it is not good to force the grid to be too high. Instead, change it to cycle T_T silently.

/* Array of initialization operations */$ arr = array ('id' => 7363, 'fd '=> 1, 'fb' => 1, 'tcp '=> 0, 'fbdh' => 1, 'jxs '=> 'gzyx'), array ('id' => 7412, 'fd' => 1, 'fb '=> 0, 'tcp' => 1, 'fbdh '=> 1, 'jxs' => 'gzyx'), array ('id' => 7512, 'fd '=> 1, 'fb' => 1, 'tcp '=> 0, 'fbdh' => 1, 'jxs '=> 'gzyx '), array ('id' => 7516, 'fd '=> 1, 'fb' => 0, 'tcp '=> 1, 'fbdh' => 0, 'jxs '=> 'bjcj'),); $ res = array (); /* In Fact, I won't tell you that I don't need to use array_walk cyclically to look tall. */foreach ($ arr as $ item) {/* determine whether the array exists for initialization */$ m Ember = & $ res [$ item ['jxs ']; if (! Isset ($ member) {$ member = array ('fd '=> 0, 'fb' => 0, 'tcp '=> 0, 'fbdh '=> 0);}/* you can add the statistical results using array_walk */$ member ['fd'] + = $ item ['fd ']; $ member ['fb '] + = $ item ['fb']; $ member ['tcp '] + = $ item ['tcp']; $ member ['fbdh '] + = $ item ['fbdh'];}/* print the result */foreach ($ res as $ key => $ item) {echo "$ key {$ item ['fd ']} {$ item ['fb']} {$ item ['tcp ']} {$ item ['fbdh']}
";}/** GZYX 3 2 1 3 * BJCJ 1 0 1 0 */

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.