0

I have an array as follows:

$sql_commands_to_run=    Array
    (
        [0] => ALTER TABLE `live`.`master_table3`]} {[ADD COLUMN `con` varchar(25) NULL;
        [1] => ALTER TABLE `live`.`master_table_2`]} {[MODIFY COLUMN `mobile2` varchar(15), ADD UNIQUE(`mobile2`);
        [2] => ALTER TABLE `live`.`master_table_2`]} {[MODIFY `type2` varchar(10)  NOT NULL;
        [3] => ALTER TABLE `live`.`master_table_2`]} {[MODIFY COLUMN `type2` varchar(10);
        [4] => ALTER TABLE `live`.`master_table_2`]} {[ADD COLUMN `email2` varchar(255);
    )

I want to merge the multiple alter queries into one query per table

Array
(
    [0] => ALTER TABLE `live`.`master_table3` ADD COLUMN `con` varchar(25) NULL;
    [1] => ALTER TABLE `live`.`master_table_2` MODIFY COLUMN `mobile2` varchar(15), ADD UNIQUE(`mobile2`),MODIFY `type2` varchar(10)  NOT NULL,MODIFY COLUMN `type2` varchar(10),ADD COLUMN `email2` varchar(255);
)

for that to happen I am trying to separate the table names with the modifications

Array(
['ALTER TABLE `live`.`master_table3`'] => Array(
                                            [0] => 'ADD COLUMN `con` varchar(25) NULL'
                                            ),
['ALTER TABLE `live`.`master_table_2`'] => Array(
                                                [0] => 'MODIFY COLUMN `mobile2` varchar(15)',
                                                [1] => 'ADD UNIQUE(`mobile2`)',
                                                [2] => 'MODIFY `type2` varchar(10)  NOT NULL',
                                                [3] => 'MODIFY COLUMN `type2` varchar(10)',
                                                [4] => 'ADD COLUMN `email2` varchar(255)'
                                                )                                       
)

here is what I am trying :

$merger = array();
foreach($sql_commands_to_run as $sql_command){            
            $temp_arr = explode("]} {[",$sql_command);            
            array_push($merger["'".$temp_arr[0]."'"],$temp_arr[1]);
        }

but I'm not good at arrays. Please, any help would be appreciated.

4
  • 1
    $merger[$temp_arr[0]][] = $temp_arr[1]; instead array_push Commented Nov 3, 2017 at 9:15
  • The source array that you have is a real mess, I can see roughly what you are trying to do but the data held in $sql_commands_to_run is far from ideal. Where does $sql_commands_to_run come from? Show us the code please Commented Nov 3, 2017 at 9:17
  • Maybe you should write that as an answer @splash58 Commented Nov 3, 2017 at 9:17
  • Why not just str_replace(']} {[', ' ', $sql_commands_to_run)? Oh I see, you want 1 query per table. Commented Nov 3, 2017 at 9:18

2 Answers 2

2

Change line with array_push with

$merger[$temp_arr[0]][] = $temp_arr[1];

demo

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help. Good luck!
1

Don't use array_push() just set the value....

$merger = array();
foreach($sql_commands_to_run as $sql_command){            
        $temp_arr = explode("]} {[",$sql_command);            
        $merger[$temp_arr[0]][]=$temp_arr[1];
}

Comments

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.