Welcome Guest, Not a member yet? Register   Sign In
populating an associative array from an sql return stryct
#1

Trying to move data from mysql over to a structure.

$data['email'] = $row->email;
$data['year'] = $row->year;


But when I look at $data, the fields are null even though the $row is populated. What is wrong with this? I need to get the data into an associative array. 
proof that an old dog can learn new tricks
Reply
#2

(This post was last modified: 08-25-2018, 07:30 AM by richb201.)

I changed it to this:

         $data = array(
           'email' => $row->email,
           'year' => $row->year,
           'campaign' => $row->campaign,
        'data_time' => uniqid(),
           'hours' => $row->hours,
           'week' => $row->week,
           'business_component' => $row->business_comp,
           'project' => $row->project,
          'activity' => $row->activity);

But it is still not working. The elements of $data are still null.
proof that an old dog can learn new tricks
Reply
#3

Place these two methods in a helper file and load it.

PHP Code:
// -----------------------------------------------------------------------

if ( ! function_exists('objectToArray'))
{
    
/**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * Usage:
     * 
     * $data = objectToArray($object);
     * 
     * @param  $data
     * @return array
     */
    
function objectToArray($data)
    {
        if (
is_object($data))
        {
            
/**
             * Gets the properties of the given object
             * with get_object_vars function
             */
            
$data get_object_vars($data);
        }

        
/**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        
return (is_array($data))
            ? 
array_map(__FUNCTION__$data)
            : 
$data;
    }
}

// -----------------------------------------------------------------------

if ( ! function_exists('arrayToObject'))
{
    
/**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * Usage:
     * 
     * $data = arrayToObject($array);
     * 
     * @param  $data
     * @return object
     */
    
function arrayToObject($data)
    {
        
/**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        
return (is_array($data))
            ? (object) 
array_map(__FUNCTION__$data)
            : 
$data;
    }


These two methods will convert (array to object and object to array).
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 08-25-2018, 02:58 PM by richb201.)

I create MY_array_helper and put it in the helpers directory. I load it with:

     $this->load->helper('string','MY_array_helper');

And then I try calling it with

$data=objectToArray($result);

The $result struct is from my call to mySQL Query(). I get Message: Call to undefined function objectToArray(). Is the result from the mysql query an object? Perhaps I want to do $data2=objectToArray($result) instead, and then have:

          $data = array(
           'email' => $data2->email,
           'year' => $data2->year,
           'campaign' => $data2->campaign,
           'data_time' => uniqid(),
           'hours' => $data2->hours,
           'week' => $data2->week,
           'business_component' => $data2->business_comp,
           'project' => $data2->project,
          'activity' => $data2->activity);
proof that an old dog can learn new tricks
Reply
#5

Got it. This is the solution:

$data = array(
'email' => $row['email'],
'year' => $row['year'],
'campaign' => $row['campaign'],
'data_time' => uniqid(),
'hours' => $row['hours'],
'week' => $row['week'],
'business_component' => $row['business_comp'],
'project' => $row['project'],
'activity' => $row['activity']);
proof that an old dog can learn new tricks
Reply
#6

Not sure where you got $result from but it looks like you should have passed $row to the method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(08-26-2018, 04:28 AM)InsiteFX Wrote: Not sure where you got $result from but it looks like you should have passed $row to the method.

https://www.codeigniter.com/user_guide/d...sults.html
proof that an old dog can learn new tricks
Reply
#8

I know all about those results.

In your first post you mention $row but then you are passing $result to the

$data = objectToArray($result);

This should be passed the result from the database.

$data = objectToArray($row);
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB