0

I have the nested array of data below which comes from the Wordpress Formidable Pro plugin form entries database table. I would like to reformat it so I can use it in a Wordpress WP_list_table but I can't work out how to alter the innermost nested array. I need each row to be formatted like 'product_id' => '4080',

        Array (
            [30] => Array (
                [user_id] => 2
                [product_id] => 4080
            )
            [31] => Array (
                [user_id] => 5
                [product_id] => 2942
            )
            [32] => Array (
                [user_id] => 4
                [product_id] => 9630
            )
            [33] => Array (
                [user_id] => 3
                [product_id] => 2542
            )
            [34] => Array (
                [user_id] => 7
                [product_id] => 1234
            )
        )

The code used to produce the array:

        global $wpdb;

        //Retrieve the bids from the database.      
        $form_entries = $wpdb->get_results('SELECT * FROM '. $wpdb->prefix .'frm_item_metas WHERE field_id in (145,147)');

        $data = array();

        foreach ( $form_entries as $meta ) {

            if ( ! isset($data[$meta->item_id])){

                $data[$meta->item_id] = array();

            }

            $data[$meta->item_id][] = $meta->meta_value;

        }

        //rename the array keys
        foreach( $data as &$new_values ) {
          $new_values['user_id'] = $new_values[0]; unset( $new_values[0] );
          $new_values['product_id'] = $new_values[1]; unset( $new_values[1] );
        }
        unset($new_values);

    }

I have tried messing around with strReplace and implode() but I don't really know what I'm doing. I'd be grateful if anyone could help.

Desired array:

        Array (
            [30] => Array (
                'user_id' => '2',
                'product_id' => '4080',
            )
            [31] => Array (
                'user_id' => '5',
                'product_id' => '2942',
            )...
  • 1
    Is that your code producing the array so you can manipulate it easily? Or is it from the plugin? Where are you trying to manipulate the array? What you are looking for seems to be type casting. You can read more about it here: php.net/manual/en/language.types.type-juggling.php – hampusn Jul 3 '15 at 12:15
  • The $wpdb query of the plugin's database entries initially produces an array of stdClass Objects. The code then picks the values I need from each stdClass Object and adds them to a new array $data. Type casting looks possible but I'm just not sure where/how I would go about integrating that to produce what I need. – theflyingant Jul 3 '15 at 12:28
0

A quick solution would be to change this:

    //rename the array keys
    foreach( $data as &$new_values ) {
      $new_values['user_id'] = $new_values[0]; unset( $new_values[0] );
      $new_values['product_id'] = $new_values[1]; unset( $new_values[1] );
    }

Into this:

    //rename the array keys
    foreach( $data as &$new_values ) {
      $new_values['user_id'] = (string) $new_values[0]; unset( $new_values[0] );
      $new_values['product_id'] = (string) $new_values[1]; unset( $new_values[1] );
    }

However, I think you can shorten the work a bit by only looping once. This is based on the your code so I'm just guessing on the structure on the data retrieved from the database.

$wpdb->get_results() has a second parameter which you can use to choose how the retrieved data should be structured. In my code below I use an associative array instead of the default object. This is just a preference for me and does not really matter in this case. But if you would not need your resulting data array to be indexed by the item_id you wouldn't have to convert your retrieved data from an objects to associative arrays with this parameter.

    global $wpdb;

    //Retrieve the form entries from the database.      
    $form_entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}frm_item_metas WHERE field_id in (145,147)", ARRAY_A );

    $data = array();
    // Loop through all form entries from query and add them to the data array.
    foreach ( $form_entries as $form_entry ) {
        // Make sure the item_id exist and some kind of meta value too.
        if ( ! empty( $form_entry[ 'item_id' ] ) && ! empty( $form_entry[ 'meta_value' ] ) ) {
            $item_id = $form_entry[ 'item_id' ];
            $entry_value = $form_entry[ 'meta_value' ];

            // Begin row in data array with the item_id as the numeric key.
            $data[ $item_id ] = array();
            // Add user_id if it exist.
            if ( $entry_value[ 'user_id' ] ) {
                $data[ $item_id ][ 'user_id' ] (string) $entry_value[ 'user_id' ];
            }
            // Add product_id if it exist.
            if ( $entry_value[ 'product_id' ] ) {
                $data[ $item_id ][ 'product_id' ] (string) $entry_value[ 'product_id' ];
            }
        }
    }

    // $data should now hold the structure as you described

    /*
    //rename the array keys
    foreach( $data as &$new_values ) {
      $new_values['user_id'] = $new_values[0]; unset( $new_values[0] );
      $new_values['product_id'] = $new_values[1]; unset( $new_values[1] );
    }
    unset($new_values);
    */

You might want to read up a bit about protecting queries against SQL injection attacks. Unless those two field_ids are static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.