[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Adding if statement in an array

Posted on 2013-12-30
5
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

Accepted Solution

by:
Ray Paseur earned 800 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 111

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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