Welcome Guest, Not a member yet? Register   Sign In
Query a associative array from Database CI4
#1

(This post was last modified: 02-10-2022, 06:13 PM by samaidha.)

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
Reply
#2

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;
    }

What did you Try? What did you Get? What did you Expect?

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB