1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'XenForo Development Discussions' started by AndyB, Jun 10, 2014.

  1. AndyB

    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.jpg

    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.
     
  2. Syndol

    Syndol Guest

    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.
     
    Daniel Hood, wickedstangs and AndyB like this.
  3. AndyB

    AndyB Well-Known Member

    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>
    
     
  4. AndyB

    AndyB Well-Known Member

    Also for a point of reference, the print_r looks like this:

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

        [
    1] => Array
            (
                [
    category] => Boots
            
    )
    ...
     
  5. Syndol

    Syndol Guest

    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.
     
  6. AndyB

    AndyB Well-Known Member

    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);
     
  7. AndyB

    AndyB Well-Known Member

    The xen:foreach loop in the template requires a multidimensional array.
     
  8. Syndol

    Syndol Guest

    I don't think so, but what do I know.
     
    Daniel Hood likes this.
  9. AndyB

    AndyB Well-Known Member

    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.
     
    Syndol likes this.
  10. AndyB

    AndyB Well-Known Member

    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>
    
     
  11. AndyB

    AndyB Well-Known Member

    The final product. :)

    pic001.jpg
     

Share This Page