How do I create a multidimensional array from a single array?

AndyB

Well-known member
I have an Options field called Category. For example the Option field has the following:

Accessories,Cameras,Electronics

In my PHP code I convert this text field to a simple array.

What I would like to be able to do is is create a drop down in a template that allows me to select any of these three items:

(example of dropdown)
pic001.webp

My PHP code looks like this:

PHP:
		// get options from Admin CP -> Options -> Ebay -> Category
		$category = XenForo_Application::get('options')->ebayCategory;			
		
		// put into array
		$categoryArray = explode(',', $category); 

		$results = $categoryArray;

		// prepare viewParams
		$viewParams = array(
			'results' => $results
		);		
		
		// send to template
		return $this->responseView('Andy_Ebay_ViewPublic_Ebay', 'andy_ebay_add');

The above PHP code is incorrect. $results needs to be a multidimensional array. Normally this is easy to create because I would normally query a database table and use the fetchAll command which creates a multidimensional array. But I don't want to create a database table to store my categories, rather I want to use a simple Option and store the categories as a comma separated text.

How can I easily convert a simple array to a multidimensional array that is in the correct format to use in a xen:foreach loop in a template?

Thank you.
 
Hi Andy,

From your above example your $categoryArray looks like array(0 => 'Accessories', 1 => 'Cameras', 2 => 'Electronics')
In your template use <xen:foreach loop="$categoryArray" key="$key" value="$value">

If you need the key to match the value for the select options, then you could either do that in PHP first or simply lower case the value in the template select option itself.
 
Hi Syndol,

Thank you for your kind assistance.

I ended up doing this and it works.

PHP:
		// get options from Admin CP -> Options -> Ebay -> Category
		$category = XenForo_Application::get('options')->ebayCategory;
		
		// remove trailing comma if there is one
		$category = rtrim($category, ',');			
		
		// put into array
		$categoryArray = explode(',', $category); 

		// get count
		$count = count($categoryArray);
		
		// define variable
		$results = array();

		// create multidimensinal array
		for ($i=0; $i<$count; $i++)
		{
			$results[] = array(
			'category' => $categoryArray[$i]
			);
		}
		
		// prepare viewParams
		$viewParams = array(
			'results' => $results
		);		
		
		// send to template
		return $this->responseView('Andy_Ebay_ViewPublic_Ebay', 'andy_ebay_add', $viewParams);

and the template code is this:

Code:
<dl class="ctrlUnit">
    <dt><label>{xen:phrase ebay_category}:</label></dt>
    <dd>
        <ul>
            <li><select name="category" size="7" class="textCtrl">
                <xen:foreach loop="$results" value="$result">
                    <option value="{$result.category}">{$result.category}</option>
                </xen:foreach>
            </select></li>
        </ul>
    </dd>
</dl>
 
Also for a point of reference, the print_r looks like this:

PHP:
Array
(
    [0] => Array
        (
            [category] => Accessories
        )

    [1] => Array
        (
            [category] => Boots
        )
...
 
Still not sure why you can't just use your $categoryArray directly, or if you must have a multi array then why you need a count and a for loop when you could use a foreach, but glad you got it sorted out.
 
Thank you for the suggestion to use a foreach.

PHP:
		// get options from Admin CP -> Options -> Ebay -> Category
		$category = XenForo_Application::get('options')->ebayCategory;
		
		// remove trailing comma if there is one
		$category = rtrim($category, ',');			
		
		// put into array
		$categoryArray = explode(',', $category);
		
		// create multidimensional array
		foreach ($categoryArray AS $categoryName)
		{	
			$categoryOptions[] = array(
			'category' => $categoryName
			);
		}
		
		// prepare viewParams
		$viewParams = array(
			'categoryOptions' => $categoryOptions
		);
		
		// send to template
		return $this->responseView('Andy_Ebay_ViewPublic_Ebay', 'andy_ebay_add', $viewParams);
 
Hi Syndol,

Thank you so much for your continuing review of my code and noting that something is wrong. Indeed you're correct, the single function array works just fine. In post #1 you can see my PHP code was missing the important $viewParam in the "send to template" code. The error message fooled me into thinking a multidimensional array is required as most often that is what is being used.
 
This works great!

PHP:
        // get options from Admin CP -> Options -> Ebay -> Category
        $category = XenForo_Application::get('options')->ebayCategory;
       
        // remove trailing comma if there is one
        $category = rtrim($category, ',');           
       
        // put into array
        $categoryArray = explode(',', $category);
       
        // prepare viewParams
        $viewParams = array(
            'categoryArray' => $categoryArray
        );
       
        // send to template
        return $this->responseView('Andy_Ebay_ViewPublic_Ebay', 'andy_ebay_add', $viewParams);


and the template code

Code:
<dl class="ctrlUnit">
    <dt><label>{xen:phrase ebay_category}:</label></dt>
    <dd>
        <ul>
            <li><select name="category" size="7" class="textCtrl">
                <xen:foreach loop="$categoryArray" value="$option">
                    <option value="{$option}">{$option}</option>
                </xen:foreach>
            </select></li>
        </ul>
        <p class="explain">{xen:phrase ebay_category_of_feed}</p>
    </dd>
</dl>
 
Back
Top Bottom