[Solution] How to fetch data from arraylist in PHP

2
495
How to fetch data from arraylist in PHP
[Solution] How to fetch data from arraylist in PHP : complete clarification and solution to get value from the arraylist in simple way.

Many user get stuck regarding how one can get value from the array list.

There many different way to figure out the solution regarding getting value from array list.

With help two approach you can get value easily.

Best 2 [Solution] How to fetch data from arraylist in PHP

Solution 1 : foreach ?

Suppose we have arraylist assigned to single variable which get from the database.

$records  = Array
(
    [0] => Array
        (
            [public_keyname] => Toyota
        )

    [1] => Array
        (
            [public_keyname] => Color
        )

    [2] => Array
        (
            [public_keyname] => Speed
        )

) ;

So from above code i just want to get value toyota, color, and speed. So how to get this value from this array list.

To Get Value we can use below code.

foreach($array as $key=>$val){
  echo $val["public_keyname"]."<br />";
}

Solution 2 : array_column() function

The array_column function is inbuilt function in PHP and used to get value from single column in an input array.

Below is syntax for array column.

$arrayvar = array_column($input_array, $column_number, $index_key);

The first two parameter are mandatory and third one is optional.

first parameter as $input_array : It is a array from which we want to extract all value or specific value of particular column.

second parameter as $column_number : Column of values that is need to be returned.

Third one is $index_key parameter : column to use as the index or keys for returning the array output.

To use this function then, PHP 5 >= 5.5.0.

$public_keyname = array_column($records, 'public_keyname');
print_r($public_keyname);

Please refer above code for $records variable value.

2 COMMENTS

  1. syntax error, unexpected ‘=>’ (T_DOUBLE_ARROW), expecting ‘)’ in C:\xampp\htdocs\test11\test.php on line 6

Comments are closed.