I haven't done this for a long time.

Source: Internet
Author: User
I haven't done this for a long time. why?

Reading data from a database is mostly a two-dimensional array with neat rows and columns. let's call it a matrix array.
When I use php to calculate a large number of vertical computations (for example, sum a field), I think many people will get used to $ array [record] [field], in fact, you should think about how to allocate keys of array one and two dimensions.

Sometimes the format of the obtained matrix array is fixed. for example, from an API, I have seen many people still calculate two-dimensional values based on records. What's more, recursion is used to look really tired, it's easy to exchange rows and columns before computing.

SquareArray. php
  $value) { $temArr = new ArrayIterator($value); $mit->attachIterator($temArr, $key); }$arr = array();foreach($mit as $item) array_push($arr,$item);if (isset($inArr[0])) $arr = array_combine(array_keys($inArr[0]),$arr);return $arr;}public function swapRowColWithKey($inArr){foreach($inArr as $k1=>$v1) foreach($v1 as $k2=>$v2) $arr[$k2][$k1] = $v2;return $arr;}public function intersect1st($arr1, $arr2, $key){$arr[$key] = array_intersect($arr1[$key], $arr2[$key]);//match source's $key with target foreach($arr1 as $k=>$v) { if ($k == $key) continue; $arr[$k] = array_intersect_key($v, $arr[$key]); }return $arr;}public function intersect2nd($arr1, $arr2, $key){foreach($arr2 as $v) $tmpArr[] = $v[$key];foreach($arr1 as $k=>$v) if(in_array($v[$key], $tmpArr)) $arr[$k] = $v;return $arr;}}?>


SquareArray. sample. php
This is the method of use. it is easy to write, just to fear that you haven't forgotten it for too long, just remind me to write it.
 Array ('001 ', '002', '003 '), 'name' => array ('Zhang San', 'Li Si', 'Wang Wu '), 'age' => array (22, 23, 11); $ array = array ('id' => '001', 'name' => 'Zhang San ', 'age' => 22), array ('id' => '002', 'name' => 'Lily', 'age' => 23 ), array ('id' => '003 ', 'name' => 'Wang 5', 'age' => 11 )); $ a = array (0 => array ('Action _ id' => 3), 1 => array ('Action _ id' => 2 ), 2 => array ('Action _ id' => 1), 3 => array ('Action _ id' => 7), 4 => array (' Action_id '=> 11),); $ B = array (0 => array ('Action _ id' => 3, 'type' => 0, 'order _ num' => 67), 1 => array ('Action _ id' => 2, 'type' => 0, 'order _ num' => 66), 2 => array ('Action _ id' => 1, 'type' => 0, 'order _ num' => 65), 3 => array ('Action _ id' => 7, 'type' => 0, 'order _ num' => 64), 8 => array ('Action _ id' => 14, 'type' => 0, 'order _ num' => 40), 13 => array ('Action _ id' => 11, 'type' => 0, 'Order _ num' => 30),); // An example of one-dimensional and two-dimensional key values in the exchange matrix array $ obj = new squareArray (); $ arr = $ obj-> swapRowCol ($ array ); var_export ($ arr); // An example of one-dimension and two-dimensional key values in the exchange matrix array (retain the value key name) $ arr = $ obj-> swapRowColWithKey ($ B ); var_export ($ arr); // calculate the intersection of two-dimensional array arrays based on the specified key (one-dimensional key) $ key = 'action _ id '; $ aa = $ obj-> swapRowCol ($ a); $ bb = $ obj-> swapRowCol ($ B); $ arr = $ obj-> intersect1st ($ bb, $ aa, $ key); // Define $ B and $ a by yourself. I am too lazy to enter $ arr = $ obj-> swapRowCol ($ arr ); var_export ($ arr); // calculate the intersection of two-dimensional array arrays based on the specified key (two-dimensional key) $ ke Y = 'action _ id'; $ arr = $ obj-> intersect2nd ($ B, $ a, $ key); // define your own values for $ B and $, I am too lazy to enter var_export ($ arr); exit;?>


If any bug is found, handle it by yourself ......


Reply to discussion (solution)

Mark!

Thank you for sharing your learning experience.

How to write nest array iteration?

How to write nest array iteration?

This is not my consideration.

What is a nest array?
Why is the swapRowCol method of the key more complex than the swapRowColWithKey method of the key?
I often use this method.

    public function swapRowCol($inArr)    {        return call_user_func_array('array_merge_recursive', $inArr);    }

What is a nest array?
Why is the swapRowCol method of the key more complex than the swapRowColWithKey method of the key?
I often use this method.

    public function swapRowCol($inArr)    {        return call_user_func_array('array_merge_recursive', $inArr);    }


Well, if the output is correct, you should be nice.
I used to practice SPL writing by the way, and then I basically didn't use swapRowCol (). I used swapRowColWithKey () all the time, so I didn't think about optimization anymore.

What is a nest array?
The nested array (cell arrays? Nested arrays ?), Dimensions are more than two dimensions.


What is a nest array?
The nested array (cell arrays? Nested arrays ?), Dimensions are more than two dimensions.

If the key is not considered, the #5 method is not correct.

I have been using php for a year. I saw this object for the first time.
$ Mit = new MultipleIterator (MultipleIterator: MIT_KEYS_ASSOC );
Why ......

Learning. mark it.

Favorites + learning.

Thank you, lz.

Haoxi. Search for Hidden

Very crowded. Line feed, braces.

Very crowded. Line feed, braces.
The display is 1920 pixels, so the line feed is more crowded.

Come and learn

I have been using php for a year. I saw this object for the first time.
$ Mit = new MultipleIterator (MultipleIterator: MIT_KEYS_ASSOC );
Why ......

What does this mean?

Good stuff, worthy of favorites


I have been using php for a year. I saw this object for the first time.
$ Mit = new MultipleIterator (MultipleIterator: MIT_KEYS_ASSOC );
Why ......

What does this mean?

View the SPL chapter by yourself

An array_column function is added to php5.5. This function can be used.

An array_column function is added to php5.5. This function can be used.
I also answered a question query manual at noon yesterday. I feel that I have never used it. I can only find out what I have added after reading it.
This is quite good, reducing a lot of filters and traversal.

Who deleted my reply?

Array_column? Returns a specified column in the array.
It is easy to write one by yourself.

$b = array(  0 => array('action_id' => 3, 'type' => 0, 'order_num' => 67),  1 => array('action_id' => 2, 'type' => 0, 'order_num' => 66),  2 => array('action_id' => 1, 'type' => 0, 'order_num' => 65),  3 => array('action_id' => 7, 'type' => 0, 'order_num' => 64),  8 => array('action_id' => 14, 'type' => 0, 'order_num' => 40),  13 => array('action_id' => 11, 'type' => 0, 'order_num' => 30),);print_r(array_column($b, 'action_id'));function array_column($ar, $key) {  return array_map(function($item) use ($key) { return @$item[$key]; }, $ar);}
Array
(
[0] => 3
[1] => 2
[2] => 1
[3] => 7
[8] => 14
[13] => 11
)

Newbie.

Very helpful! Like one!

Mark it. you can check it out later.

There are many technical skills.

I'm going... After playing for less than 200 points for a month.

They are all cool people. learn more.

Learn about, thanks

A lot of points

Passing .....

Learning

Favorites ............

Thanks for sharing and learning.

Thank you for sharing! Keep learning!

Learning ..... Ah, we are weak.

Daqing has already had good things. thank you!

Ask the landlord to help with the php SQL injection vulnerability ..

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.