Tagged: 

Viewing 2 reply threads
  • Author
    Posts
    • #2006
      Jason
      Keymaster

      Greetings!

      If you’re like me, you think Piklist is awesome and the add_more array results are confusing. If, for example, you have an add_more on a question & answer group, you’d get something like this:

      array = (
        [0] => array(
          'questions' => array(
            [0] => 'Question #1',
            [1] => 'Question #2'
          ),
          'answers' => array(
            [0] => 'Answer #1'
            [0] => 'Answer #2'
          )
        )
      )

      Now it’s formatted like this because of the awesome complexity of the piklist fields, and they even provide a cool way of looping this still using Piklist.

      But more than once I’ve had a situation where I really wanted to manipulate the data myself, and found the array structure cumbersome. I wanted the data in this form:

      array = (
        [0] => array(
          'answer'   => 'Answer #1',
          'question' => 'Question #1'
          )
        [1] => array(
          'answer'   => 'Answer #2',
          'question' => 'Question #2'
          )
        )
      )

      To get this, I put together a generic function for converting the array structure, and figured I’d share it here in case someone else found it useful:

      function parse_piklist_array($array) {
        if ( empty($array) )
          return array();
      
        $keys = array_keys($array[0]);
        if ( empty($keys) )
          return array();
      
        $results = $values = array();
        $count = count($array[0][$keys[0]]);
        for ($index = 0; $index < $count; $index++) {
          foreach($keys as $key_index => $key)
            $values[$key] = $array[0][$key][$index];
      
          $results[] = $values;
        }
      
        return $results;
      }
      

      As a note: This function does not work on nested add_more groups. It wouldn’t be too much work to make a recursive form of this function, but I haven’t needed that yet. If that changes, I’ll update the function here.

    • #2010
      Jason
      Keymaster

      I added support for the uploads field and nested groups. The resulting element will inherit the array of image ids if it’s an image, otherwise it will recursively work through the groups.

      function parse_piklist_array($array) {
        if ( empty($array) )
          return array();
      
        $keys = array_keys($array[0]);
        if ( empty($keys) )
          return array();
      
        $results = $values = array();
        $count = count($array[0][$keys[0]]);
        for ($index = 0; $index < $count; $index++) {
          foreach($keys as $key_index => $key) {
            $value = $array[0][$key][$index];
            if ( is_array($value) && !isset($value[0][0]) )
              $values[$key] = parse_piklist_array($value);
            else
              $values[$key] = $value;
          }
      
          $results[] = $values;
        }
      
        return $results;
      }
      
    • #2011
      Jason
      Keymaster

      Fixed a bug for the case of an empty image field and made a gist for this for easier revisions: https://gist.github.com/JasonTheAdams/d40351ecf5bca8a7e7f4#file-parse_piklist_array

Viewing 2 reply threads
  • The topic ‘Reordering the Piklist Add_More Array’ is closed to new replies.