PHP -modify value of last accessed element in multidimensional associative array

87 views Asked by At

I am reading a GEDCOM-formatted family tree flat file, and producing an array from the data for staging into table. If I encounter the values CONC <some value>, then, instead of adding an element, I need to append <some value> to the value of the last element that was just inserted (regardless of dimension depth).

I tried with current(...) etc but does this work for a multidimensional associative array?

please consider following element in an array:

[@N163@] => Array ( [INDI] => Array ( [TEXT] => Some data of this person) ) 

if the next line reads "1 CONC including his profession"

instead of adding a line as such

[@N163@] => Array (  
                [INDI] => Array ( [TEXT] => Some data of this person)  
                [INDI] => Array ( [CONC] => including his profession) )  

I would like the array to look as follows:

[@N163@] => Array ( 
                [INDI] => Array ( [TEXT] => Some data of this person including his profession) )

What I have researched thus far:

end($theArray) to set pointer to last inserted element followed by $theArray[key($theArray)] = .... to update this element.

But I did not get this method to work for multidimensional arrays and/or it became really messy.

And:

merging two arrays using e.g. += notation, but this only seems to overwrite a new element, not affect the last one, if keys are same

And:

examples with foreach calls, which does not help in my case.

Hope somebody can shed some light... many thanks!

1

There are 1 answers

3
splash58 On

When you adding $array[@N163@][INDI][TEXT] = 'smtng'; you can save position

$pos = &$array[@N163@][INDI][TEXT];

And if you need concatenate, write

$pos .= "concate line";