Jump to content

[SOLVED] Array Help


kirk112

Recommended Posts

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!!!

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.