Solved

Adding if statement in an array

Posted on 2013-12-30
5
285 Views
Last Modified: 2013-12-31
Hey All

I have this array code:

register_field_group(array (
		'id' => 'acf_page-options',
		'title' => 'Page Options',
		'fields' => array (
			array (
				'key' => 'field_1',
				'label' => 'Type1',
				'name' => '',
				'type' => 'text',
			),
                        array (
				'key' => 'field_2',
				'label' => 'Type2',
				'name' => '',
				'type' => 'text',
			),
		   ),
              );

Open in new window


But I want to add in a if statement like this:

register_field_group(array (
		'id' => 'acf_page-options',
		'title' => 'Page Options',
		'fields' => array (
			array (
				'key' => 'field_1',
				'label' => 'Type1',
				'name' => '',
				'type' => 'text',
			),
                        if($something=0){     //<---
                        array (
				'key' => 'field_2',
				'label' => 'Type2',
				'name' => '',
				'type' => 'text',
			),
                        }  // end if
		   ),
              );

Open in new window


Is that possible?
0
Comment
Question by:Sam Cohen
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39747836
No, it isn't possible. You have to build two array and use one of them accordingly to the condition; or you can put the condition in the code which builds the array.

$array = array();
$array['id'] = 'acf_page-options';
$array['title'] = 'Page Options';
if ($something = 0) {
    $array['fields'] = array....
}else{
    $array['fields'] = array...
}

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 39748102
register_field_group(array (...
This notation appears to be calling a PHP function.  The name of the function is register_field_group() and the argument being passed to the function is a multidimensional array.

PHP functions are described here:
http://www.php.net/manual/en/language.functions.php

The notation is troublesome because the data being passed to the function needs to be modified, however the data (the multidimensional array) is being created inside the function statement, where neither modification nor visualization is possible.  This is AntiPractice #9a and it prevents you from inserting the if() statement you want.

The solution is to create a separate variable and pass the separate variable to the function.  The separate variable can be created with conditional statements to define different data structures.  Obviously this is untested code, because we do not have your test data set, but it's probably pretty close to right.

// CREATE THE BASIC FIELDS ARRAY
$fields[] = array  
( 'key'   => 'field_1'
, 'label' => 'Type1'
, 'name'  => ''
, 'type'  => 'text'
)
;

// IF SOMETHING SIGNALS US, ADD ANOTHER ELEMENT TO THE FIELDS ARRAY
if ($something == 0)
{
    $fields[] = array  
    ( 'key'   => 'field_2'
    , 'label' => 'Type2'
    , 'name'  => ''
    , 'type'  => 'text'
    )
    ;
}
 
// CREATE THE MULTIDIMENSIONAL ARRAY FOR THE FUNCTION CALL
$multi_array = array 
( 'id'     => 'acf_page-options'
, 'title'  => 'Page Options'
, 'fields' => $fields 
)
;

// CALL THE FUNCTION USING THE VARIABLES WE CREATED
register_field_group($multi_array);

Open in new window

HTH, ~Ray
0
 
LVL 8

Author Closing Comment

by:Sam Cohen
ID: 39748775
Thanks Ray!!!

I knew you would always give a very detailed answer each time.
Thank you for that!!
You Rock!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39749033
Thanks for the points and thanks for using EE.  Happy New Year and best of luck with the project, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now