PHP:合并具有相同元素的数组



我需要一个新的数组来合并所有的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的最好方法是什么?

解决方案吗?

这可能对你有帮助

 [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
                 )
         )
 )

试试这个,

$new = array();
foreach($obj as $key=>$val {
   if($key == "exercise_name") {
     $key1 = $val;
   }
   if($key == "category_name") {
     $new[$key1] = $val;
   }

}
print_r($new);

试试这个函数,它一定会帮到你的:

<?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;
}

?>

相关内容

  • 没有找到相关文章

最新更新