1
\$\begingroup\$

i do have two dimentional array of data in php.

I do want to add another column with constant to that array for passing tabular data to next program.

i am iterating and adding manually. What can be alternate code?

my code:

            if( is_array( $arrSource )){
                foreach( $arrSource as &$dataRow){
                    foreach( $arrColumn as $k=>$v)
                        $dataRow[$k] = $v;
                }                
                unset( $dataRow );
            }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I see nothing wrong with what you already have. Assuming of course that you meant to append identical data to each $arrSource element. You are already doing the shorthand by using the reference operator. The only other way would be to write it all out manually, which would be pointless, unless you are looking for more novice readable code. I would hesitate to add, however, that unset should be called tithin the first foreach loop. Though I could not confirm 100% because I don't use the reference operator to know its quirks :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.