views:

148

answers:

1

// given following array:

$data = array(
  0=>array(
    "data"=>"object1",
    "col"=>array(
      0=>array(
        "data"=>"object2", 
        "col"=>array(
          0=>array(
            "data"=>"object3",
          ),
          1=>array(
            "data"=>"object4",
          ),
        )
      ),
      1=>array(
        "data"=>"object5", 
        "col"=>array()
      ),
    )
  )
);

// A new object can be added or existing one replaced given a position of any length // array(0), array(1,0), array(1,0,0), etc...

$data = add($data, array(0,0,1), "new_object");

function add($data, $position, $object) {
  // this has to be produced based the location:
  $data[0]["col"][0]["col"][1]["data"] = "new_object";
  return $data;
}

//Is there a sane way to do it in php5 besides creating $data."[0][\"col\"][0][\"col\"][1][\"data\"]"?

//Thanks for any hints!

+1  A: 

There must be a better data structure than that tangled array. 6 dimensions is too many in most cases. It seems like there's some repeated structure in there. Perhaps you could capitalize on that? There's no "sane" way, as you put it, to access an insane array.

nullpointer
thanks, i will have to rethink the whole thing.
lmonkey