I have an issue with the code I am working on and realised I need some help with it as I can not find out what I am doing wrong.
The following code is just at part of a bigger code (that works fine) and I would be thankful if someone could point me in the right directions with this one.

This part works good

$xt_pieces = 100;

$arrts = array();
    foreach (range(10, $xt_pieces, 2) as $number) 
    {
        $arrts[$number/2] = $number. ' pieces';
    }
print_r($arrts);

But when I try to use the array inside another array it doesn´t work at all

'amount' => array(
    'FriendlyName' => 'pieces',
    'Type' => 'dropdown',
    'Options' => $arrts,
    )) : false))); // <-- error for this line

ERROR MESSAGE
syntax error, unexpected ':', expecting ')'

I tried google and searched various forums about this, but I just can´t find out how to solve this.

Recommended Answers

All 3 Replies

Why are you talking about "ternary operator"? You are NOT using ternary operator at all. You use ternary operator when you want to replace if-else statement with in-line statement.

//i.e.
$somevalue = 10;
if ($value==1) { $somvalue-=1; }
else { $somevalue-=10; }

// then you could change it to
$somevalue -= $value==1 ? 1 : 10;

What you are doing is also bad programming concept. You are NOT supposed to assign a different datatype into a variable because it is extremely risky if you try to use the same variable later in your script. Besides, it is difficult to maintain your script later on.

So what do you really want to do with the array? What values are you planning to assign?

As i can see , your array amount is not in proper array format. if i removed that **: false)))** , your array become like below and it will work so i don't know how that array is generating by your long code.

$output=array('amount' => array(
    'FriendlyName' => 'pieces',
    'Type' => 'dropdown',
    'Options' => $arrts,
    ));

Thank for the responses, as I pointed out, I am new to php and haven´t figured everything out yet. I have tried to adjust the code with your suggestions and I think it made progress a bit, still not there yet. Here is the complete code and I hope the code speaks for itself and what I want out from it.
When I try to print this it complains about the semicolon for the arrMap array, tried to remove those (even if that felt wierd) and then I got the error: syntax error, unexpected 'print_r' (T_STRING) instead for the print_r($arrMap); .

Removing the : false as you suggested mangel.murti worked. As I now can use this without any problems, but when I try to put it all together I must have missed something, because as I stated before, it just wont work.

$xt_pieces = 100;

$arrts = array();
    foreach (range(10, $xt_pieces, 2) as $number) 
    {
        $arrts[$number/2] = $number. ' pieces';
    }

   $output=array('amount' => array(
    'FriendlyName' => 'pieces',
    'Type' => 'dropdown',
    'Options' => $arrts,
    ));

print_r($output);

Below is the complete section of code I am trying to get to work. I did put back that semicolon after the $arrMap array in the code.

$WAStatus = $TSStatus = $OIStatus = true;

$wa_pieces = 50;    // Populate the arrwa, with this amount
$xt_pieces = 100;   // Populate the arrts, with this amount
$oi_pieces = 1000;  // Populate the arroi, with this amount


$arrwa = array();
    foreach (range(2, $wa_pieces, 2) as $number) 
    {
        $arrwa[$number/2] = $number. ' pieces';
    }


$arrts = array();
    foreach (range(10, $xt_pieces, 2) as $number) 
    {
        $arrts[$number/2] = $number. ' pieces';
    }


$arroi = array();
    foreach (range(10, $oi_pieces, 2) as $number) 
    {
        $arroi[$number/2] = $number. ' pieces';
    }


$arrMap = array(
            'section1' => array(
            ($WAStatus ? array('WA' => array('amountOfHours' => array(
            'FriendlyName' => 'Hours',
            'Type' => 'dropdown',
            'Options' => array(
                            'option1' => '1 hour',
                            'option2' => '2 hours',
                            'option3' => '3 hours',
                            'option4' => '4 hours',
                            'option5' => '5 hours',
                            'option6' => '6 hours',
                            'option7' => '7 hours',
                            'option8' => '8 hours',
                            'option9' => '9 hours',
                            'option10' => '10 hours',
                            'option11' => '11 hours',
                            'option12' => '12 hours',
                            ),
        'Description' => 'Choose time',
        'Default' => '4'),
        'amountOfPiecesForWA' => array(
                                    'FriendlyName' => 'Pieces',
                                    'Type' => 'dropdown',
                                    'Options' => $arrwa,
                                    'Description' => 'How many pieces to use',
                                    'Default' => '12',
                                    )),
            ($TSStatus ? array('TS' => array('amountOfHours' => array(
            'FriendlyName' => 'Hours',
            'Type' => 'dropdown',
            'Options' => array(
                            'option1' => '1 hour',
                            'option2' => '2 hours',
                            'option3' => '3 hours',
                            'option4' => '4 hours',
                            'option5' => '5 hours',
                            'option6' => '6 hours',
                            ),
        'Description' => 'Choose time',
        'Default' => '2',
        'amountOfPiecesForTS' => array(
                                    'FriendlyName' => 'Pieces',
                                    'Type' => 'dropdown',
                                    'Options' => $arrts,
                                    )),
            ($OIStatus ? array('OI' => array('amountOfHours' => array(
            'FriendlyName' => 'Hours',
            'Type' => 'dropdown',
            'Options' => array(
                            'option1' => '1 hour',
                            'option2' => '2 hours',
                            'option3' => '3 hours',
                            'option4' => '4 hours',
                            'option5' => '5 hours',
                            'option6' => '6 hours',
                            'option7' => '7 hours',
                            'option8' => '8 hours',
                            'option9' => '9 hours',
                            ),
        'Description' => 'Choose time',
        'Default' => '3',
        'amountOfPiecesForOI' => array(
                                    'FriendlyName' => 'Slots',
                                    'Type' => 'dropdown',
                                    'Options' => $arroi,
                                    ))));    


print_r($arrMap);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.