0

I have some data coming into a page with multiple rows and columns. I am specifically looking to take 1 entire column and put it into another array.

I am using codeigniter and php. The var dump on my page shows that the data in its raw array is arriving just fine. But once I start to try and create a second array on the page, the array returns 0 results.

Here is the var_dump for the original array. For your sanity, ill only post the first sequence.

array(7) { [0]=> object(stdClass)#19 (7) { ["id"]=> string(1) "1"
["serial_number"]=> string(9) "battery 1" ["battery_type"]=> string(4) "Lipo" ["cell_count"]=> string(1) "4" ["capacity"]=> string(4) "16.8" ["date_submitted"]=> string(19) "0000-00-00 00:00:00" ["user_id_submitted"]=> string(0) "" } [1]=>

Here is the array_column function

<?php 
    $serialnumber = array_column($batteryDb, "serial_number");
    var_dump($serialnumber);
?>

and here is the result:

array(0) { }
3
  • Well, seems that $batteryDb has not the right structure. It has to be an array of arrays. Would you be so nice to add the output of var_dump($batteryDb); to your original post - maybe just two items. Commented Jul 7, 2016 at 17:34
  • @JustinSellers Edit your OP to include your sample array data. Commented Jul 7, 2016 at 17:36
  • 1
    I just updated it for you. Commented Jul 7, 2016 at 17:38

2 Answers 2

5

Ok, var_dump($batteryDb); shows this:

array(7) { 
 [0] => object(stdClass)#19 (7) {                     <----- object  !!
    ["id"]=> string(1) "1" 
    ["serial_number"]=> string(9) "battery 1" 
    ["battery_type"]=> string(4) "Lipo" 
    ["cell_count"]=> string(1) "4" 
    ["capacity"]=> string(4) "16.8" 
    ["date_submitted"]=> string(19) "0000-00-00 00:00:00" 
    ["user_id_submitted"]=> string(0) "" }
 [1] =>

The problem is that this array structure isn't working with array_column on PHP5.

You have an array of objects, but the function expects an array of arrays.

An array of objects on array_column is only supported by PHP7. Referencing: https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L626


The basic solution would be to turn your objects into arrays:

array(7) { 
     [0] => array (7) {                              <----- array !!
        ["id"]=> string(1) "1" 

You can do that using:

  • $array = (array) $object;
  • $array = get_object_vars($object);
  • if multi-dimensional: $array = json_decode(json_encode($object), true);

Give this a try:

$batteryDbArray = json_decode(json_encode($batteryDb), true);

$serial_numbers = array_column($batteryDbArray, 'serial_number');
var_dump($serial_numbers);
Sign up to request clarification or add additional context in comments.

2 Comments

ok, so I gave that a try. it rendered this array(7) { [0]=> string(9) "battery 1" [1]=> string(9) "battery 2" [2]=> string(9) "battery 3" [3]=> string(9) "battery 5" [4]=> string(9) "battery 6" [5]=> string(9) "battery 4" [6]=> string(9) "battery 7" }
Now to figure out how to put those into a drop down list. haha
1

I think array_column only added support for an array of Objects in 7.0, so you can try upgrading PHP or use a different method.

Try array_map:

$serial_numbers = array_map(function($e) {
  return is_object($e) ? $e->serial_number : $e['serial_number'];
}, $batteryDb);

There are other, worse, solutions, such as using json_encode(json_decode()) or iterating the array converting stdClass to array objects with get_object_vars or casting, but I think these add unnecessary overhead and are less obvious:

$serial_numbers = array_column(json_decode(json_encode($batteryDb), true), 'serial_number'));

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.