ramsey / array_column Goto Github PK
View Code? Open in Web Editor NEWProvides functionality for array_column() to projects using PHP earlier than version 5.5.
License: MIT License
Provides functionality for array_column() to projects using PHP earlier than version 5.5.
License: MIT License
I'm trying to parse the following array **:
Array
(
[data] => Array
(
[0] => Array
(
[id] => 78987967867
[created_time] => 2013-06-16T02:30:15+0000
)
[1] => Array
(
[id] => 45645645645
[created_time] => 2013-04-20T17:43:46+0000
)
[2] => Array
(
[id] => 5675676756756
[created_time] => 2013-04-08T20:52:57+0000
)
[3] => Array
(
[id] => 5767567657
[created_time] => 2013-02-15T23:08:10+0000
)
[4] => Array
(
[id] => 6556765756
[created_time] => 2013-02-15T23:08:02+0000
)
[5] => Array
(
[id] => 345345345
[created_time] => 2010-01-25T06:44:05+0000
)
[6] => Array
(
[id] => 345345656
[created_time] => 2008-12-06T02:12:25+0000
)
[7] => Array
(
[id] => 2453453453
[created_time] => 2008-02-16T02:36:45+0000
)
[8] => Array
(
[id] => 1232434234
[created_time] => 2007-07-08T01:56:34+0000
)
[9] => Array
(
[id] => 6512005646
[created_time] => 2007-07-08T01:55:14+0000
)
[10] => Array
(
[id] => 6512010646
[created_time] => 2007-03-13T20:44:19+0000
)
[11] => Array
(
[id] => 6512015646
[created_time] => 2007-02-16T15:12:42+0000
)
)
[paging] => Array
(
[cursors] => Array
(
[before] => sdkj898hnnksdffsdf=
[after] => moiusdujnekwero==
)
)
)
This array is contained in $data
. Calling array_column($data,'id')
returns an empty array:
Array
(
)
There are no errors, no warnings, no notices. Unless I'm doing something wrong, it would seem that there is some sort of limit on array depth?
Ideas? Particularly how to debug this?
** this is a standard datastructure from the Facebook API, turn into a PHP array by using json_decode($json, TRUE)
this is my array and i want to fetch only the value of deviceToken ...
Array ( [0] => Parse\ParseObject Object ( [serverData:protected] => Array ( [Public] => 1 [SavedToProfile] => [deviceToken] => 2677d45739c22e2e4b06debd47d7c018095897429f496cbffc736d47675e7d5d [starCount] => 80 [userid] => Parse\ParseUser Object ( [_sessionToken:protected] => [serverData:protected] => Array ( ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( ) [dataAvailability:Parse\ParseObject:private] => Array ( ) [className:Parse\ParseObject:private] => _User [objectId:Parse\ParseObject:private] => PnqfQ5o1eL [createdAt:Parse\ParseObject:private] => [updatedAt:Parse\ParseObject:private] => [hasBeenFetched:Parse\ParseObject:private] => ) [video] => Parse\ParseFile Object ( [name:Parse\ParseFile:private] => tfss-11c9283e-8b16-46d2-9dcd-095a32398393-video.mov [url:Parse\ParseFile:private] => http://files.parsetfss.com/0445aec8-2c25-4ddf-998d-d5d3f4137a98/tfss-11c9283e-8b16-46d2-9dcd-095a32398393-video.mov [data:Parse\ParseFile:private] => [mimeType:Parse\ParseFile:private] => ) [videoDescription] => Akki [videoThumbnil] => Parse\ParseFile Object ( [name:Parse\ParseFile:private] => tfss-5844ea36-c656-42f4-a592-05173ae730d3-img [url:Parse\ParseFile:private] => http://files.parsetfss.com/0445aec8-2c25-4ddf-998d-d5d3f4137a98/tfss-5844ea36-c656-42f4-a592-05173ae730d3-img [data:Parse\ParseFile:private] => [mimeType:Parse\ParseFile:private] => ) [videoTitle] => new video [videoTotalDuretion] => 8.93 [videoUploadedOnMonthAndYear] => May 2015 ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( [Public] => 1 [SavedToProfile] => [deviceToken] => 2677d45739c22e2e4b06debd47d7c018095897429f496cbffc736d47675e7d5d [starCount] => 80 [userid] => Parse\ParseUser Object ( [_sessionToken:protected] => [serverData:protected] => Array ( ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( ) [dataAvailability:Parse\ParseObject:private] => Array ( ) [className:Parse\ParseObject:private] => _User [objectId:Parse\ParseObject:private] => PnqfQ5o1eL [createdAt:Parse\ParseObject:private] => [updatedAt:Parse\ParseObject:private] => [hasBeenFetched:Parse\ParseObject:private] => ) [video] => Parse\ParseFile Object ( [name:Parse\ParseFile:private] => tfss-11c9283e-8b16-46d2-9dcd-095a32398393-video.mov [url:Parse\ParseFile:private] => http://files.parsetfss.com/0445aec8-2c25-4ddf-998d-d5d3f4137a98/tfss-11c9283e-8b16-46d2-9dcd-095a32398393-video.mov [data:Parse\ParseFile:private] => [mimeType:Parse\ParseFile:private] => ) [videoDescription] => Akki [videoThumbnil] => Parse\ParseFile Object ( [name:Parse\ParseFile:private] => tfss-5844ea36-c656-42f4-a592-05173ae730d3-img [url:Parse\ParseFile:private] => http://files.parsetfss.com/0445aec8-2c25-4ddf-998d-d5d3f4137a98/tfss-5844ea36-c656-42f4-a592-05173ae730d3-img [data:Parse\ParseFile:private] => [mimeType:Parse\ParseFile:private] => ) [videoTitle] => new video [videoTotalDuretion] => 8.93 [videoUploadedOnMonthAndYear] => May 2015 ) [dataAvailability:Parse\ParseObject:private] => Array ( [Public] => 1 [SavedToProfile] => 1 [deviceToken] => 1 [starCount] => 1 [userid] => 1 [video] => 1 [videoDescription] => 1 [videoThumbnil] => 1 [videoTitle] => 1 [videoTotalDuretion] => 1 [videoUploadedOnMonthAndYear] => 1 ) [className:Parse\ParseObject:private] => videoListTable [objectId:Parse\ParseObject:private] => I6eEdNLVQN [createdAt:Parse\ParseObject:private] => DateTime Object ( [date] => 2015-05-28 10:43:25 [timezone_type] => 2 [timezone] => Z ) [updatedAt:Parse\ParseObject:private] => DateTime Object ( [date] => 2015-05-29 04:52:28 [timezone_type] => 2 [timezone] => Z ) [hasBeenFetched:Parse\ParseObject:private] => 1 ) )
First of all - great job! ๐
Why do you need to check the count of the arguments at all? As I know, PHP will throw an error, if there are less parameters given than required, just don't allow the arguments be optional (as in the manual they are not)! This can be written like this:
<?php
// function array_column($input = null, $columnKey = null, $indexKey = null)
function array_column(array $input, $columnKey, $indexKey = null)
{
// ...
// !!! Now you can get rid of this code too:
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return null;
}
if (!is_array($params[0])) {
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
return null;
}
// ...
}
Ald also the
if (!is_array($params[0]))
will throw an error, if an empty array was given. In that case there should be an extra isset()
checking.
It would be great if the function could return the original index of the row.
Example:
$array = array(
'key' => array(
'element' => 'value'
)
);
// Returns array('key' => 'value')
array_column($array, 'element', true);
I know it only mimics the core function, but still would be a great feature.
I am currently working a on a project which is used on multiple servers with multiple PHP versions.
Until now I used put backport packages (like this) into require-dev (for testing) and support section of composer.json.
However recently I found out that I could even require it in all cases which has two effects:
A third option is to install it globally on older servers, but I don't like that either.
Does anybody have a good answer for this? What is the best practice?
Your array_column()
is giving different results from PHP's built-in array_column()
if the 3rd parameter is not null
and the element value is not a string. For example:
$records = array(
array('id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe'),
array('id' => true, 'first_name' => 'Sally', 'last_name' => 'Smith'),
array('id' => null, 'first_name' => 'Jane', 'last_name' => 'Jones'),
array('id' => 56.23, 'first_name' => 'Peter', 'last_name' => 'Doe'),
);
PHP 5.6.3 array_column()
gives:
Array
(
[2135] => Doe
[2136] => Smith
[2137] => Jones
[2138] => Doe
)
Your array_column()
gives:
Array
(
[2135] => Doe
[1] => Smith
[] => Jones
[56.23] => Doe
)
Why not simply checking the parameters?
It would be great if you could pass in an array of objects, and the index key provided merely accessed one of the properties to be used when re-indexing the array of objects.
I know that you can implement the ArrayAccess interface, but it's a bit tedious to do this for every object that you just want to be able to reindex an array of.
A simple check to see if $row
is an object would be good. A more robust implementation might allow you to define a method call against a class as the index key e.g. array('Person', 'getID')
.
Of course this would make it inconsistent with the actual PHP implementation, but it would make this more useful. Having to implement ArrayAccess marginally defeats the purpose of using the terse syntax of array_column()
over the slightly more verbose array_map()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.