I need to encode just the first column of an array with n rows.
Array (
[0] => Array
(
[FamilyName] => Dikkartan, Bartu
[Balance] => -2446.91
[Mobile] => 0444497
[HAddress] => 2/6 Tramsway Street
[HSuburb] => Rosebery
[HState] => NSW
[HPCode] =>
)
[1] => Array
(
[FamilyName] => King, Alan & Luka
[Balance] => -1676
[Mobile] => 0433 6090
[HAddress] => 46/12 Hayberry Street
[HSuburb] => Crows Nest
[HState] => NSW
[HPCode] =>
) ...
How can I make an encoded string from the FamilyName
column values.
Using array_map you can reduce the array to just that element.
$familyNames = array_map(function($object) {
return $object['FamilyName'];
}, $objects);
json_encode($familyNames);
This uses an anonymous function available in php 5.3 otherwise you need to use create_function or a reference to an existing function you've created. See the PHP callback documentation.
create_function('$object', 'return $object["FamilyName"];')
Alternatively you could use a simple foreach loop:
$familyNames = array();
foreach($objects as $object)
$familyNames[] = $object['FamilyName'];