• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complex re-Creation of an unknown array

#1
[eluser]Xeoncross[/eluser]
I have an array that will contain optional sub-arrays in one of it's parts (I don't know which one). I need to break it into multiple single arrays.

Code:
<?php
array(3) {
  ["type"]=>
  array(1) {
    [0]=> string(10) "ROOFDRAINS"
  }
  ["wade"]=>
  array(2) {
    [0]=> string(4) "3000"
    [1]=> string(1) "3"
  }
  ["zurn"]=>
  array(1) {
    [0]=> string(7) "ZRB104*"
  }
}


//into

array(2) {
  [0]=>
  array(3) {
    ["type"]=> string(10) "ROOFDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB104*"
  }
  [1]=>
  array(3) {
    ["type"]=> string(10) "ROOFDRAINS"
    ["wade"]=> string(1) "3"
    ["zurn"]=> string(7) "ZRB104*"
  }
}

?>

Does anyone know how I can do this?

#2
[eluser]xwero[/eluser]
I don't think you can create the array you desire without adding some assumptions into your code. Lets say the type has 2 items and the wade has 3 which of the type items needs to be repeated, the first or the the second?

Code:
$nice_keys = array();
$count = 0;
foreach($ugly_array as $key => $array)
{
    if($count < count($array))
    {
       $count = count($array);
    }
    $nice_keys[] = $key;
}

$nice_array = array();
for($i=0;$i<$count;$i++)
{
   $temp = array();
   foreach($nice_keys as $key)
   {
      $temp[$key] = (isset($ugly_array[$i][$key])) ? $ugly_array[$i][$key] : $ugly_array[0][$key];
   }
   $nice_array[] = $temp;
}
This code assumes the first item is the fall through item and all key arrays have at least one item.

#3
[eluser]Xeoncross[/eluser]
[quote author="xwero" date="1229438060"]I don't think you can create the array you desire without adding some assumptions into your code... which of the type items needs to be repeated, the first or the the second?[/quote]

That is the fun of this challenge - I need all variations of the array created. The array may have 2+ children arrays of 1+ elements (max depth of all this is 2). So I want all of the type arrays created and all of the wade arrays created, and so forth.


Here is another example:

Code:
&lt;?php
array(3) {
  ["type"]=>
  array(1) {
    [0]=> string(10) "ROOFDRAINS"
    [0]=> string(11) "FLOORDRAINS"
  }
  ["wade"]=>
  array(2) {
    [0]=> string(4) "3000"
  }
  ["zurn"]=>
  array(1) {
    [0]=> string(7) "ZRB104"
    [0]=> string(7) "ZRB105"
    [0]=> string(7) "ZRB106"
  }
}

//INTO:

array(2) {
  [0]=>
  array(3) {
    ["type"]=> string(10) "ROOFDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB104"
  }
  [1]=>
  array(3) {
    ["type"]=> string(10) "ROOFDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB105"
  }
  [2]=>
  array(3) {
    ["type"]=> string(10) "ROOFDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB106"
  }
  [3]=>
  array(3) {
    ["type"]=> string(11) "FLOORDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB104"
  }
  [4]=>
  array(3) {
    ["type"]=> string(11) "FLOORDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB105"
  }
  [5]=>
  array(3) {
    ["type"]=> string(11) "FLOORDRAINS"
    ["wade"]=> string(4) "3000"
    ["zurn"]=> string(7) "ZRB106"
  }
}


?&gt;

Hey, thanks for your code. I can't try it now but I appreciate it.

#4
[eluser]sophistry[/eluser]
could you try putting this in sql tables and do a cartesian product join?

#5
[eluser]Xeoncross[/eluser]
Actually, that is why I am trying to get these into single arrays - so I can store them in MySQL as rows.

#6
[eluser]sophistry[/eluser]
right, so put the ones you have into the tables and the do the cartesian product on them. it's not used often, but i think it's called UNION


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.