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
https://forums.phpfreaks.com/topic/102291-solved-array-help/
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
https://forums.phpfreaks.com/topic/102291-solved-array-help/#findComment-523817
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
https://forums.phpfreaks.com/topic/102291-solved-array-help/#findComment-523832
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.