kirk112 Posted April 22, 2008 Share Posted April 22, 2008 Hi I have the following array Array ( [1] => Array ( [join_name] => join1 [join_field] => Array ( [0] => category_1 [1] => category_2 ) [join_word] => Array ( [2] => and ) ) [2] => Array ( [join_name] => join2 [join_field] => Array ( [2] => category_1 [3] => category_2 ) [join_word] => Array ( [5] => and ) ) ) I keep trying different way of looping through it so that I can get the following output Array ( [1] => CONCAT(category_1,' AND ', category_2) AS join1 [2] => CONCAT(category_1,' AND ', category_2) AS join2 ) But everytime I keep getting mixed up, please can somehelp help me arrange this Thanks for your help!!! Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 try <?php $arr = Array ( 1 => Array ( 'join_name' => 'join1', 'join_field' => Array ( '0' => 'category_1', '1' => 'category_2' ), 'join_word' => Array ( '2' => 'and' ) ), 2 => Array ( 'join_name' => 'join2', 'join_field' => Array ( '2' => 'category_1', '3' => 'category_2' ), 'join_word' => Array ( '5' => 'and' ) ) ); $result = array(); foreach ($arr as $k=>$data) { $join = $data['join_name']; list($jword) = array_values($data['join_word']); list($f1, $f2) = array_values($data['join_field']); $result[$k] = sprintf ("CONCAT(%s,' %s ', %s) AS %s", $f1, strtoupper($jword), $f2, $join); } echo '<pre>', print_r($result, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
kirk112 Posted April 22, 2008 Author Share Posted April 22, 2008 Thanks Barand!! Worked perfect for the example I provided, but there can be more than one join, so I have change it to... which worked prefect. if(count($joins > 0)) { $counter = 0; foreach($joins as $join => $join_fields) { $sql[$counter] = 'CONCAT('; $total_fields = count($join_fields['join_field']); $join_fields['join_word'] = array_pad($join_fields['join_word'],$total_fields,''); $joins = array_combine($join_fields['join_field'],$join_fields['join_word']); foreach($joins as $join_field => $join_word) { $sql[$counter] .= $join_field.",' ".$join_word." ',"; } $sql[$counter] = substr($sql[$counter],0,-6); $sql[$counter] .= ') AS '.$join_fields['join_name']; $counter++; } echo '<pre>'; print_r($sql); echo '</pre>'; $sql = implode(', ' ,$sql); echo $sql; } else { echo 'no joins selected'; } Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2008 Share Posted April 22, 2008 It's that old definition Test data : that data for which the program works Quote Link to comment Share on other sites More sharing options...
kirk112 Posted April 22, 2008 Author Share Posted April 22, 2008 Sorry, should have explained more. But the code that you provided help me solve the initial problem and helped me fix a few further problems. So THANK YOU!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.