Title: Send array as shortcode argument for a plugin
Last modified: August 21, 2016

---

# Send array as shortcode argument for a plugin

 *  [Craig Lambie](https://wordpress.org/support/users/cclambie/)
 * (@cclambie)
 * [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/)
 * Hi fellow WordPress programmers,
 * I am developing embed-wistia-video plugin and they have implemented “chapters”
   for video, which is great.
    So the chapters take 2 inputs, title and time. I 
   am thinking the best way to do this via shortcode is with an array, but I can’t
   seem to access the array correctly.
 * Some help with formatting I think please:
    my shortcode: `[wistiavid id=ro4t5bq4y0
   postrolltext="this is a test" postrollurl=#anchor chapters="'title' => 'test 
   title', 'tme' => '05'" ]` The print_r of the £args array:
 *     ```
       Array
       (
           [id] => ro4t5bq4y0
           [postrolltext] => this is a test
           [postrollurl] => #anchor
           [chapters] => 'title' => 'test title', 'tme' => '05'
       )
       ```
   
 * the print_r of the $chapters array:
 *     ```
       Array
       (
           [0] => 'title' => 'test title', 'tme' => '05'
       )
       ```
   
 * my function to get the sub array out:
 *     ```
       function ChaptersArrayRet($chapters)
       {
       	$chaparray[] = array($chapters);
       	$ret = "";
       	$chapNo = 1;
       	$comma = "";
   
       	print_r($chaparray);
   
       	foreach($chaparray as $chap)
       	{
       		$ret .= "ch_".$chapNo."_title: \"".$chap[0][title]."\",";
       		$ret .= "ch_".$chapNo."_time: \"".$chap[0][tme]."\"".$comma;
       		$chapNo ++;
       		$comma = ",";
       	}
   
       	return $ret;
       }
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/#post-3797691)
 * I think you just need to use `$chap[0]->title` and `$chap[0]->tme`
 *  Thread Starter [Craig Lambie](https://wordpress.org/support/users/cclambie/)
 * (@cclambie)
 * [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/#post-3797702)
 * Hmmm, I see where you are coming from, but no cigar 🙁
 * I have changed the code a little, and I think the issue is related to this:
 *     ```
       function ChaptersArrayRet($chapters)
       {
       	$chaparray[] = array($chapters);
       	$ret = "";
       	$chapNo = 1;
       	$comma = "";
   
       	print_r1($chaparray);
   
       	foreach($chaparray as $chap)
       	{
       		print_r1($chap[0]);
       		$ret .= "ch_".$chapNo."_title: \"".$chap[0][3]."\",";
       		$ret .= "ch_".$chapNo."_time: \"".$chap[0]->tme."\"".$comma;
       		$chapNo ++;
       		$comma = ",";
       	}
   
       	return $ret;
       }
       ```
   
 * This actually outputs title: t
    Which suggests to me that the “array” is each
   individual letter, instead of an associative array, that I was hoping for. Any
   suggestions?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/#post-3797722)
 * I was going for an easy fix without making much effort to understand what you’re
   really doing. Nice try huh?
 * It may be $chap is a simple string and the time string will be in $chap during
   the next loop iteration. What does print_r($chaparray) output?
 * Now that I’ve taken the time to look closer at your code, I question `$chaparray[]
   = array($chapters);`. This is useful inside a loop to accumulate data into a 
   single array. Outside a loop, it adds unnecessary array layers. Why not just 
   build your $ret string directly from $chapters?
 * For that matter, I really question building an array in shortcode parameters.
   It is prone to typographic errors. I would honestly just pass the title and time
   as separate parameters, keeping array levels to a minimum.
 * Still, it’s informative to be able to parse complex arrays. It can come down 
   to methodically printing progressive array elements until you arrive at the correct
   structure needed to get at the desired element. Also, I’ve found using var_dump()
   instead of print_r() gives a slightly more understandable array structure, plus
   it indicates variable types, which can unveil hard to find errors in some cases.
   OTOH, the extra information can confuse the essence of the data. Each function
   has its place.
 *  Thread Starter [Craig Lambie](https://wordpress.org/support/users/cclambie/)
 * (@cclambie)
 * [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/#post-3797759)
 * Thanks for the tips.
    I ended up using var_dump, and will in future to work this
   out.
 * I simple couldn’t convert it to an associative array at all, only ever a string.
 * So I dumped the idea, and made it into a explode function using just commas… 
   will be simpler anyway I think.
 *     ```
       function ChaptersArrayRet($chapters)
       {
       	$chaparray[] = explode(",",$chapters);
       	$ret = "";
       	$comma = "";
       	$chapNo = 1;
       	var_dump($chaparray);
       	$i = 0;
   
       	while($i < count($chaparray[0]) )
       	{
       		$ret .= $comma."ch_". $chapNo ."_title: \"".$chaparray[0][$i]."\",";
       		$ret .= "ch_". $chapNo ."_time: \"".$chaparray[0][$i+1]."\"";
       		echo1($ret);
       		$comma = ",";
       		$chapNo++;
       		$i++;
       		$i++;
       	}
   
       	return $ret;
       }
       ```
   
 * Thanks for your advice.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/#post-3797765)
 * I agree, a better approach. I’m pleased you solved your problem and was happy
   to help.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Send array as shortcode argument for a plugin’ is closed to new replies.

## Tags

 * [argument](https://wordpress.org/support/topic-tag/argument/)
 * [array](https://wordpress.org/support/topic-tag/array/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/send-array-as-shortcode-argument-for-a-plugin/#post-3797765)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
