PHP array operation go to heavy t.t

Source: Internet
Author: User
The great gods, my number-leader, this way.
Array (2) {
[+] = Array (6) {

["job_name"] => string(44) "苹果,香蕉,苹果"

}
[3] = = Array (6) {

["job_name"] => string(44) "梨子,梨子,西红柿"

}
}

I want it to become

Array (2) {
[+] = Array (6) {

["job_name"] => string(44) "苹果2,香蕉1"

}
[3] = = Array (6) {

["job_name"] => string(44) "梨子2,西红柿1"

}
}

Is this jiangzi going to be hard t.t a friend? q.q

Reply content:

The great gods, my number-leader, this way.
Array (2) {
[+] = Array (6) {

["job_name"] => string(44) "苹果,香蕉,苹果"

}
[3] = = Array (6) {

["job_name"] => string(44) "梨子,梨子,西红柿"

}
}

I want it to become

Array (2) {
[+] = Array (6) {

["job_name"] => string(44) "苹果2,香蕉1"

}
[3] = = Array (6) {

["job_name"] => string(44) "梨子2,西红柿1"

}
}

Is this jiangzi going to be hard t.t a friend? q.q

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

I didn't read the question clearly, here is the revised 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);});

This means that the field in the two-dimensional array, job_name if there are duplicates, will be repeated after the corresponding number of repetitions can be set, right!

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']);//分割字符串        $a1[$key] = array_count_values($a0[$key]);//统计个数        $temp=array();        foreach ($a1[$key] as $key2 => $value2) {            $temp[$key2]=$key2.$value2;        }        $res[$key]['job_name']=implode(',',$temp);//字符串转数组            }     return $res;}

Input array-related error checking pending

$arr[14]['job_name']="苹果,香蕉,苹果";$arr[3]['job_name']="梨子,梨子,西红柿";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; }
  • 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.