Welcome Guest, Not a member yet? Register   Sign In
Array helper - more function required
#1

I have gone through the ArrayHelper file and found only one function.

There are multiple functions which we use in our development, which we wish to have in core helpers

Can we suggest those as well to be a part of core functionality instead of custom?

This might help all developers to get the one-stop solution

Suggested functions can be,

  1. array_sort_by_column
  2. array_remove_empty_elements
  3. array_search_by_key
  4. array_search_by_value
  5. array_change_key_case_recursive
  6. array_trim
Reply
#2

Here are a couple to get started with, I'll work on the others tomorrow.

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

if ( ! function_exists('objectToArray'))
{
    
/**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * @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;
    }


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

if ( ! function_exists('arrayToObject'))
{
    
/**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * @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;
    }

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

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

I have the implementation of all these functions. I want to include itnin core codeigniter code base.

But I don't know how to do this contribution.

Please provide some video tutorial from where I can understand, how to contribute.
Reply
#4

(This post was last modified: 02-10-2019, 06:43 AM by kilishan.)

Some of those functions look like good additions, but any that wold be accepted would have to be significant improvements in usability over core PHP functions. array_sort_by_column would fit, since I'm assuming it handles multi-dimensional array sorting which is always a bit of a pain to manually do. Things like array_remove_empty_elements doesn't sound any different than array_filter so wouldn't accept that one.

I don't have time to make a video on how to do contribution, but this should help: Contributing to CodeIgniter. Especially the link on Pull Requests.
Reply
#5

PHP has almost all of the sorting functions / methods already for key / value and multi.

That would be breaking kiss and dry.
What did you Try? What did you Get? What did you Expect?

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

There are tons of Core PHP Array functions, CI4 would just be wrapping them inside a helper - also I would imagine this would get insane: array_sort_by_column, array_sort_by_column_multi... for sorting based on an value that is nested.... hmmm
---
array_sort_by_column could be done by: array_map_assoc / array_walk
Reply




Theme © iAndrew 2016 - Forum software by © MyBB