Solved

Adding if statement in an array

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

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 109

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 count occurrences of each item in an array.

832 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