1

I need to create the following array from a query in codeigniter. So far what I'm producing is not what I'm looking for.

What I need...

array (size=4)
  1 => string '1414277999' (length=10)
  2 => string '1470411334' (length=10)
  3 => string '1456617599' (length=10)
  4 => string '1461538799' (length=10)

What I currently have..

array (size=4)
  0 => 
    object(stdClass)[35]
      public 'session' => string '1' (length=1)
      public 'end' => string '1477090799' (length=10)
  1 => 
    object(stdClass)[36]
      public 'session' => string '2' (length=1)
      public 'end' => string '1481932799' (length=10)
  2 => 
    object(stdClass)[37]
      public 'session' => string '3' (length=1)
      public 'end' => string '1488585599' (length=10)
  3 => 
    object(stdClass)[38]
      public 'session' => string '4' (length=1)
      public 'end' => string '1493420399' (length=10)

This is my query in codeigniter..

$bd = $this->db->select('session, end')
    ->from('session_dates')
    ->where('end >=', $now)
    ->get();
return $bd->result();

Can someone point me in the direction of how to build my query to create the array I'm looking for?

3
  • if you had searched on google you would have found the result_array @ first result with this question : codeigniter get array from query Commented Aug 5, 2016 at 15:44
  • I did google and result_array does not return the results I indicated. Commented Aug 5, 2016 at 15:46
  • You'r right, mybad, then : return array_map(function($val){ return $val['end']; }, $bd->array_result()); Commented Aug 5, 2016 at 16:12

3 Answers 3

1

Although this is probably not the best way to achieve your goal, I made an object to array conversion function which I can share with you:

function (throw this into a helper file or something)

function object_to_array_recursive(&$o)
{
    if(is_object($o))
    {
        $o = (array)$o;
    }

    if(is_array($o) && count($o) > 0)
    {
        foreach($o as $k=>&$v)
        {
            object_to_array_recursive($v);
        }
        unset($v);
    }
}

Use of function

$my_db_result = $bd->result();
object_to_array_recursive($my_db_result);

return $my_db_result;

Since it accepts the parameter by reference, you will have to pass it a variable name because object_to_array_recursive($bd->result()); will fail.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to extract all the data you need into a new array, and then return it.

You can do this like the following :

$getEnds = function($value){ return $value->end; };

return array_map($getEnds, $bd->result());

The array_map looping the given results, and apply on each value of it the function in first parameter. This function just return the end param from current object.

You can also compress your code :

return array_map(function($value){ return $value->end; }, $bd->result());

Comments

0

Try this code:

$bd = $this->db->select('session, end')
    ->from('session_dates')
    ->where('end >=', $now)
    ->get();
$result_array = array();
foreach ($bd->result() as $row){
    $result_array[$row->session] = $row->end;
}
return $result_array;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.