Solved

Adding if statement in an array

Posted on 2013-12-30
5
279 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 30

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress syntax error 1 29
Link SQL table to Webpage 9 39
delete database record with modal 21 34
update field on focusout 15 24
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

18 Experts available now in Live!

Get 1:1 Help Now