PHP:使用相同的元素合并数组

时间:2015-06-02 06:30:27

标签: php arrays multidimensional-array array-merge

我需要一个新数组来将所有category_name与相同的exercise_name合并,即它将是:

Array
    (
[0] => stdClass Object
    (

        [exercise_name] => Hang Clean
        [category_name] => Heavy,Pull,Core
    )

[1] => stdClass Object
    (

        [exercise_name] => Ice Skaters
        [category_name] => Ground,Lower,Plyometrics
    )

[2] => stdClass Object
    (

        [exercise_name] => Ice Skaters
        [category_name] => Basketball,Sport Specific
    )

输出:

Array
(
    [0] => stdClass Object
        (
            [exercise_name] => Hang Clean
            [multiple_category_name] => Array
                (
                    [0] => Heavy,Pull,Core
                )

        )

    [1] => stdClass Object
        (
            [exercise_name] => Ice Skaters
            [multiple_category_name] => Array
                (
                    [0] => Ground,Lower,Plyometrics
                    [1] => Basketball,Sport Specific
                )

        )

)

将所有category_name与相同的exercise_name合并的最佳方法是什么?

任何解决方案?

3 个答案:

答案 0 :(得分:1)

这可能对您有所帮助

 [akshay@localhost tmp]$ cat test.php
 <?php 

 $array= array(
               (object)array("exercise_name" => "Hang Clean","category_name" => "Heavy,Pull,Core"),
               (object)array("exercise_name" => "Ice Skaters","category_name" => "Ground,Lower,Plyometrics"),
               (object)array("exercise_name" => "Ice Skaters","category_name" => "Basketball,Sport Specific")
              );


 function simple_merge($array)
 {

    foreach($array as $k)
    {
       if(isset($output[$k->exercise_name]))
       { 
           array_push($output[$k->exercise_name]->multiple_category_name,$k->category_name);    
       }else
       {    
           $k->multiple_category_name  = array($k->category_name);
           unset($k->category_name);
           $output[$k->exercise_name]  = $k;
       }        
    }

    return array_values($output);
 }


 // Input 
 print_r($array);

 // Output
 print_r(simple_merge($array));

 ?>

<强> 输出

 [akshay@localhost tmp]$ php test.php

 Array
 (
     [0] => stdClass Object
         (
             [exercise_name] => Hang Clean
             [category_name] => Heavy,Pull,Core
         )

     [1] => stdClass Object
         (
             [exercise_name] => Ice Skaters
             [category_name] => Ground,Lower,Plyometrics
         )

     [2] => stdClass Object
         (
             [exercise_name] => Ice Skaters
             [category_name] => Basketball,Sport Specific
         )

 )
 Array
 (
     [0] => stdClass Object
         (
             [exercise_name] => Hang Clean
             [multiple_category_name] => Array
                 (
                     [0] => Heavy,Pull,Core
                 )

         )

     [1] => stdClass Object
         (
             [exercise_name] => Ice Skaters
             [multiple_category_name] => Array
                 (
                     [0] => Ground,Lower,Plyometrics
                     [1] => Basketball,Sport Specific
                 )

         )

 )

答案 1 :(得分:0)

试试这个,

import org.apache.commons.cli.ParseException
import org.apache.commons.cli.UnrecognizedOptionException
import org.codehaus.groovy.cli.GroovyPosixParser

def cli = new CliBuilder(usage: 'myprog', stopAtNonOption: false)
cli.a("option1")
def unknowns = [];
cli.parser = new GroovyPosixParser() {
    @Override
    protected void processOption(String arg, ListIterator iter) throws ParseException {
        try {
            super.processOption(arg, iter)
        }
        catch (UnrecognizedOptionException specialE) {
            unknowns.push(arg);
        }
    }

}

def options = cli.parse(args)
//on error cli.usage()
if (!unknowns.isEmpty()) {
    println("had unknown options")
    unknowns.each {println it}
    cli.usage()
}

//cli.usage() is probably better and enough

答案 2 :(得分:0)

试试这个功能它会帮助你:

<?php
$obj = array(
        0 => (object)array(
                'exercise_name' => 'Hang Clean',
                'category_name' => 'Heavy,Pull,Core',
                ),
        1 => (object)array(
                'exercise_name' => 'Ice Skaters',
                'category_name' => 'Ground,Lower,Plyometrics',
                ),
        2 => (object)array(
                'exercise_name' => 'Ice Skaters',
                'category_name' => 'Basketball,Sport Specific',
                )

        );


/*-----------------------------------------------------------*/

        echo '<pre>';
        print_r(funcName($obj));

/*-----------------------------------------------------------*/



function funcName($obj) {   
        $results = array();
        foreach($obj as  $obj1){
            if(empty($results)){
                $results[] = (object)array(
                                'exercise_name' => $obj1->exercise_name,
                                'multiple_category_name' => array(
                                                        $obj1->category_name,
                                                    ),
                            );
            }else{
                $check = 0;
                foreach ($results as $key =>$result ){

                    if(trim($result->exercise_name) == trim($obj1->exercise_name)){
                        $check = 1;
                        $results[$key]->multiple_category_name[] = $obj1->category_name;
                        continue;
                    }
                }
                if($check == 0){
                    $results[] = (object)array(
                        'exercise_name' => $obj1->exercise_name,
                        'multiple_category_name' => array(
                                                $obj1->category_name,
                                            ),
                    );              
                }
            }       
        } 
        return $results;
}



?>