PHP array operation deduplication T.T

Source: Internet
Author: User
Tags pears
My team lead looks like this: array (2) {[14] & amp; gt; array (6) {code ...}} [3] & amp; gt; array (6) {code ...}}} I want to change it to array (2) {[14] & amp; gt; array (6) {code ...}} [3] & amp; gt; array (6) {code ...}}}... my team lead looks like this.
Array (2 ){
[14] => array (6 ){

["Job_name"] => string (44) "apple, banana, apple"

}
[3] => array (6 ){

["Job_name"] => string (44) "pears, pears, tomatoes"

}
}

I want it to become

Array (2 ){
[14] => array (6 ){

["Job_name"] => string (44) "Apple 2, banana 1"

}
[3] => array (6 ){

["Job_name"] => string (44) "pear 2, tomato 1"

}
}

Will it be difficult for this sauce to T.T? Q. Q

Reply content:

My team lead looks like this.
Array (2 ){
[14] => array (6 ){

["Job_name"] => string (44) "apple, banana, apple"

}
[3] => array (6 ){

["Job_name"] => string (44) "pears, pears, tomatoes"

}
}

I want it to become

Array (2 ){
[14] => array (6 ){

["Job_name"] => string (44) "Apple 2, banana 1"

}
[3] => array (6 ){

["Job_name"] => string (44) "pear 2, tomato 1"

}
}

Will it be difficult for this sauce to T.T? Q. Q

array_walk($arr, function(&$item) {    $item['job_name'] = join(',', array_unique(explode(',', $item['job_name'])));});

I didn't see the question clearly. The following is the modified answer.

array_walk($arr, function(&$item) {    $after = array_values(array_unique(explode(',', $item['job_name'])));    foreach ($after as $key=>$value) {        preg_match_all("/$value/", $item['job_name'], $matches);        $after[$key] = $value . count($matches[0]);    }    $item['job_name'] = join(',', $after);});

That is, in this two-dimensional arrayjob_nameField. if there are duplicates, set the number of duplicates next to the duplicates!

foreach ($arr as $fruits){    $fruits_array = explode(',',$fruits['job_name']);    $count_array = array();    foreach ($fruits_array as $fruit){        $count_array[$fruit][] =1;    }    $sum_data =array();    foreach ($count_array as $fruit=>$fruit_array){        $sum_data[] = $fruit.  count($fruit_array);    }    $sum_data = implode(',',$sum_data);    $return_data[]["job_name"] = $sum_data;}

Function dealArr ($ arr = array () {foreach ($ arr as $ key => $ value) {$ a0 [$ key] = explode (',', $ value ['job _ name']); // Split string $ a1 [$ key] = array_count_values ($ a0 [$ key]); // count $ temp = array (); foreach ($ a1 [$ key] as $ key2 => $ value2) {$ temp [$ key2] = $ key2. $ value2;} $ res [$ key] ['job _ name'] = implode (',', $ temp ); // Convert string to array} return $ res ;}

Error check related to input array not processed yet

$ Arr [14] ['job _ name'] = "apple, banana, apple"; $ arr [3] ['job _ name'] = "pear, pear, tomato "; var_dump (dealArr ($ arr ));

foreach ($arr as &$val){    $job_name_array = explode(',',$val['job_name']);    $return = array_count_values($job_name_array);    $str = '';    foreach ($return as $key => $value){        $str .= $key.$value.',';    }    $str = rtrim($str,",");    $val['job_name'] = $str; }
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.