Link to home
Start Free TrialLog in
Avatar of freshwaterwest
freshwaterwestFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php array - implode only the second level if is_array

I have an array ... $fields

This contains variables of which some are single strings and some are arrays

I need to implode these second level arrays values with a || divider whilst still leaving them inside the $fields array

i.e.

	$fields = array(
		'contentid' => $resource->get('id'),
		'parent' => $resource->get('parent'),
		'var1' => $resource->get('tv1'),
		'var2' => $resource->get('tv2'),//if this is an array it needs to be imploded with ||
		'var3' => $resource->get('tv3'), //if this is an array it needs to be imploded with ||
	);

Open in new window

SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of freshwaterwest

ASKER

Hi thanks for the reply - I was thinking there might be a quicker way - perhaps a function or a foreach to do them all without individually listing - the actual $fields has quite a lot of keys/values - I've only shown a couple of examples here.

I wonder if I could do something like below but don't really know how without listing as you've done above:

	foreach($fields as $key => $value){
		if( is_array($value) ){
			$value = implode('||',$value);
		}
	}

Open in new window


cheers
Yes, certainly, if you don't know exactly which fields might be arrays, then just as you said.   Any kind of loop through the array $fields which checks each entry for is_array and then implodes it will work.
I'm not sure how exactly to get it to work though, my attempt above didn't seem to work so I must have the logic / syntax wrong - I'm not too sure how to work with $key / $value without spelling out each individual
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks Ray - works perfectly...
You can do this:

foreach ($fields as $key => $value)
{
    if (is_array($fields[$key]))
      $fields[$key] = implode("||", $fields[$key]);
}
Thanks for the points, and please feel free if you want to share some points with Yodercm.  I already have enough points to orbit Saturn.

Regarding this code snippet... It would make sense only if you wanted to implode all of the arrays.  If you wanted to be selective about it, you would need to tell PHP which arrays to choose or which arrays to exclude.

foreach ($fields as $key => $value)
{
    if (is_array($fields[$key]))
      $fields[$key] = implode("||", $fields[$key]);
} 

Open in new window

BUT... If you want to post a follow-on question about this design, I'll be glad to take a look at it.  Using a multi-dimensional array and implode() is an unusual code construct.  If we truly understand the problem you're trying to solve we may be able to suggest some easier ways to get a handle on the problem.
To anyone coming upon this answer in the future... Before you use this code:
https://www.experts-exchange.com/questions/28509381/php-array-implode-only-the-second-level-if-is-array.html?anchorAnswerId=40297280#a40297280

Please read this article to see what you need to change about that code!  String literal array indexes must be quoted.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
@Ray:  I'm surprised you don't know this, Ray.  This code



<?php
$fields = array(
      'contentid' => "x",
      'var2' => "d"
   );
$x = $fields[contentid];
$y = $fields[var2];
echo "$x,$y";
exit();



produces this output.  No quotes needed.

x,d
No quotes needed.
Right, but only under two circumstances, both of which depart a long way from best practices.

1. You have suppressed PHP Notice messages.  Many programmers do not know about Notice messages.  If you suppress these, and PHP does by default, you will never be told if your script relies on an undefined variable.  Most professionals raise the error reporting levels to get these messages.

2. There is not a constant contentid or var2.

This is all explained in the article.  Search the article text for Missing Quotation Marks Cause Time-Bombs.
https://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_12241-Quotation-Marks-in-PHP.html

Then go back and try your script with error_reporting(E_ALL) to see what happens.  
<?php
error_reporting(E_ALL);
$fields = array(
      'contentid' => "x",
      'var2' => "d"
   );
$x = $fields[contentid];
$y = $fields[var2];
echo "$x,$y";
exit();

Open in new window

After that, define a constant like this and rerun the script to see what happens.
<?php
define('contentid', 'ABC');
$fields = array(
      'contentid' => "x",
      'var2' => "d"
   );
$x = $fields[contentid];
$y = $fields[var2];
echo "$x,$y";
exit();

Open in new window

If you ever build something of any consequence in PHP you will likely work with other programmers and you will likely have a coding standards document that is your daily bible.  In all my years of working with PHP I have never seen a coding standard that allowed the use of unquoted string literals in array indexes.