Solved

Adding if statement in an array

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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