Hey,

I've been working with some multi-dimensional arrays and need to use the array_merge() function on them. The problem with the current php array_merge() function is that ignores more then one dimension. Any array inside an array is just replaced instead of merged...

Halp?
I don't have much exp with PHP, but I'll try and help you out here with logic Rolling Eyes

Perhaps there is a way to move one dimension of the array to a second, one dimensional array; do this for each dimension then merge them.
Yeah. I was just thinking of making another function that used the array_merge function, but it would be really complicated and I'd have to implement a bunch of ifs or a switch...

It needs to check if the array exists inside both parent arrays, and if so then it needs to run itself again on that child array to see if it has any more dimensions. If not, then it just does an array merge.

Hmmm, lemmy see if I can explain this easier (for my sake as well as everyone elses).

function blah(array1,array2)
if keyA exists in array1 and keyA exists in array2
then blah(array1[keyA],array2[keyA])
else (if it doesn't exist in both arrays)
then array_merge(array1,array2)

The problem is finding the deapest dimension and going backwards from there to keep it from overwriting data...

I might write up a function and see if I can't get it to work. The logic is confusing me though, lol. That and I'm tired 0x5


[... Hours Go By ...]


Nope, I'm stumped... (head hurts too, I should go to bed... its already 0300 Rolling Eyes )

EDIT:
Heres what I wrote, for those who want to check it out and try and work with it.

Don't ask questions about it though, because I have no clue how half of this function works... I just let my brain do it, lol, while I day dreamed (happens allot! Razz Thats where most of my good code comes from 0x5 )


Code:
<?php

   session_start();

   print "<pre>";

   function array_merge_md($array1,$array2,$cleanup=true) {
      $orig_array1 = $array1;
      foreach($array1 as $k => $v) {
         if($array1[$k]!=$array2[$k]) {
            if(!empty($array1[$k])&&!empty($array2[$k])) {
               if(is_array($array1[$k])&&is_array($array1[$k]))
                  $array1[$k] = array_merge_md($array1[$k],$array2[$k],false);
               else
                  $array1[$k] = $array2[$k];
            } elseif(empty($array1[$k])&&!empty($array2[$k]))
               $array1[$k] = $array2[$k];
         }
      }
      if($cleanup===true) $array1 = array_merge_md($array2,$array1,false);
      return $array1;
   }

    $Array1 = array("SESSION"=>array("PUSH"=>array("STAGE"=>1,"AID"=>123456)));
    $Array2 = array("SESSION"=>array("Error_ID"=>"","PUSH"=>array(0=>"PUSHED")));

   print_r($Array1); print "<br /><br />";
   print_r($Array2); print "<br /><br />";

   $Array3 = array_merge_md($Array1,$Array2);

   print_r($Array3); print "<br /><br />";

   print "</pre>";

?>


The cleanup=true, I assume, means that it runs the function again except backwards and array1 has its new values.

array_merge_md == Multi-Dimensional

EDIT (again... teehee):
I wrote yet another function that did not work. I cleaned this one up a tad bit and I feel a little more concious (probably not, but eh Razz )...

Code:
   function array_merge_md($array1,$array2,$first_time=true) {
      foreach($array1 as $k => $v) {
         if(array_key_exists($k,$array2)) {
            if($array2[$k]!=$v) {
               if(is_array($array2[$k])&&is_array($array1[$k]))
                  $array1 = array_merge_md($array1[$k],$array2[$k],false);
               else
                  $array1[$k] = $array2[$k];
            }
         }
      }
      if($first_time===true) $array1 = array_merge_md($array2,$array1,false);
      return $array1;
   }
That looks pretty good; that's basically what I was going to suggest when I first saw the topic. It might not be totally optimized, but it will work.
Both functions I have tested and neither work Sad

Heres a good example for what I would like to happen:

Array1:
Code:
Array
(
    [SESSION] => Array
        (
            [PUSH] => Array
                (
                    [STAGE] => 1
                    [AID] => 399897
                )

        )

)


Array2:

Code:
Array
(
    [SESSION] => Array
        (
            [Error_ID] =>
            [PUSH] => Array
                (
                    [0] => PUSHED
                )

        )

)


Returned Array:

Code:
Array
(
    [SESSION] => Array
        (
            [Error_ID] =>
            [PUSH] => Array
                (
                    [0] => PUSHED
                    [STAGE] => 1
                    [AID] => 399897
                )

        )

)



EDIT:
Okay, I finally got wrote the function. This works:

Code:
   function array_merge_md($array1,$array2,$first_time=true) {
      foreach($array1 as $k=>$v) {
         if($array2[$k]!=$v) {
            if(is_array($array2[$k])&&is_array($v))
               $array1[$k] = array_merge_md($array1[$k],$array2[$k],false);
            elseif(is_array($array2)&&is_array($array1))
               $array1 = array_merge($array1,$array2);
            else
               $array1[$k] = $array2[$k];
         }
      }
      return $array1;
   }


EDIT:
This is still being very pesky...
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement