CodeIgniter Forums
Query a associative array from Database CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Query a associative array from Database CI4 (/showthread.php?tid=81259)



Query a associative array from Database CI4 - samaidha - 02-10-2022

Hi, hope you guys are doing ok, 


I'm having trouble changing an array to an object in CI4, I have my working code in PHP which works fine, but I'm having difficulty writing it on CI4, below is my query for a "search" in my Database that retrieves a result and set it as a associative array(object) with a null value,


PHP Code:
if (isset($_GET['q'])) {
    $query mysqli_real_escape_string($conn$_GET['q']);
    $title = array();

    $sql "SELECT name FROM users WHERE name LIKE '%$query%'";
    $results mysqli_query($conn$sql);
    $names mysqli_num_rows($results);

    if ($names 0) {
        while ($row mysqli_fetch_assoc($results)) {
            $name[$row['name']] = null;
        }
    } else {
        exit();
    }
} else {
    exit();
}

echo 
json_encode($name); 

However in CI4. the closest thing I could muster up is below, which kind of work, but don't think it is right

Code:
  $user = new  UserModel();
     
  $term = $this->request->getVar('term');
  $data =  $user->like('name', $term, 'both')->findColumn('name');
     
  $object = (object)$data;

    foreach($data as $key => $val) {

        $object->$val = null;

  }

  return $this->response->setJSON($object);


How can I fix this code to get results as the raw PHP, which outputs the whole array converted to an object with its key(from array index) and value as null


RE: Query a associative array from Database CI4 - InsiteFX - 02-11-2022

You can place these in a helper and then use to convert.

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

if ( ! function_exists('objectToArray'))
{
    /**
    * objectToArray ()
    * -------------------------------------------------------------------
    *
    * @param  $data
    * @return array
    */
    function objectToArray(object $data) : array
    {
        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 ()
    * -------------------------------------------------------------------
    *
    * @param  $data
    * @return object
    */
    function arrayToObject(array $data) : object
    
{
        /**
        * Return array converted to object Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return (is_array($data)) ? (object) array_map(__FUNCTION__$data) : $data;
    }




RE: Query a associative array from Database CI4 - iRedds - 02-11-2022

return $this->response->setJSON(array_fill_keys($data, null));


RE: Query a associative array from Database CI4 - samaidha - 02-11-2022

Thank you Both @InsiteFX and @iRedds for taking the time to answer the question, The answer by @iRedds has solved the the problem (greatly appriciated), and thanks to @InsiteFX for providing the helper function.