Given this array:
array(1) {
[0]=>
array(2) {
["Project"]=>
array(5) {
["id"]=>
string(1) "2"
["user_id"]=>
string(2) "21"
["customer_id"]=>
string(1) "4"
["name"]=>
string(15) "WordPress Theme"
["created"]=>
string(19) "2011-09-26 21:30:38"
}
["Track"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "7"
["user_id"]=>
string(2) "21"
["project"]=>
string(1) "2"
["customer"]=>
string(1) "4"
["title"]=>
string(7) "Backend"
["notes"]=>
string(0) ""
["created"]=>
string(19) "2011-09-28 22:21:22"
["Lapse"]=>
array(2) {
[0]=>
array(5) {
["id"]=>
string(1) "4"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:21"
["stop"]=>
string(19) "2011-09-28 22:22:30"
["created"]=>
string(19) "2011-09-28 22:22:21"
}
[1]=>
array(5) {
["id"]=>
string(1) "3"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:07"
["stop"]=>
string(19) "2011-09-28 22:22:12"
["created"]=>
string(19) "2011-09-28 22:22:07"
}
}
}
}
}
}
How would i group by Day in the Lapse Array with PHP? This may have been easier to do directly with MySQL but i'm using CakePHP's recursive function and i cant figure out how to use Group By with that!
$list = array();
function extractByDates($arr) {
foreach ($arr as $key => $v)
if (is_array($v))
function extractByDates($v);
else if ($key == 'created')
$list[$v] = $v;
}
extractByDates($yourGivenArray);
I not tested!
Related
I have three keys pointing to value of type array, in those array values I have duplicate values. How can I remove them?
array(3) {
["rock"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "4"
["title"]=>
string(9) "Blink-182"
["location"]=>
string(25) "Motorpoint Arena, Cardiff"
["event_date"]=>
string(10) "2017-07-03"
["event_time"]=>
string(8) "18:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.478937400000010"
["geo_long"]=>
string(17) "-3.17172289999996"
["tags"]=>
string(14) "rock,metal"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["alternative"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "6"
["title"]=>
string(6) "Sum 41"
["location"]=>
string(23) "O2 Academy Leeds, Leeds"
["event_date"]=>
string(10) "2017-02-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.802188400000000"
["geo_long"]=>
string(17) "-1.54713770000001"
["tags"]=>
string(21) "alternative,indie"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["sax"]=>
array(1) {
[0]=>
array(9) {
["id"]=>
string(1) "3"
["title"]=>
string(9) "Take That"
["location"]=>
string(22) "The SSE Hydro, Glasgow"
["event_date"]=>
string(10) "2017-05-11"
["event_time"]=>
string(8) "18:30:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "55.860156000000000"
["geo_long"]=>
string(17) "-4.28525800000000"
["tags"]=>
string(24) "pop,boy-band,sax"
}
}
}
I'm going to assume that if two bands have the same 'id' then they are identical
$unique_bands = array();
// if the master array is called bands
foreach($bands as $genre){
foreach($genre as $band){
$unqiue_bands[$band['id']] = $band;
}
}
Maybe put $unique_bands = array_values(array_filter($unique_bands)); at the end to reindex the array
You have a solution on the official documentation... A person made a simple function to do exactly what you want.
Example array:
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
?>
Function:
<?php
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
?>
You will have to fix it to work your for array dimension.
Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.
I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation
I want to get data from an array using PHP loop like FOR or FOREACH.
when I print the content of the array using var_dump it looks like this:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "query1"
["fql_result_set"]=>
array(13) {
[0]=>
array(3) {
["aid"]=>
string(19) "2944783819003364347"
["name"]=>
string(16) "Profile Pictures"
["photo_count"]=>
string(2) "37"
}
[1]=>
array(3) {
["aid"]=>
string(19) "2944783820076875780"
["name"]=>
string(7) "only me"
["photo_count"]=>
string(1) "2"
}
[2]=>
array(3) {
["aid"]=>
string(19) "2944783819003517141"
["name"]=>
string(35) "Ways To Tie Your Shoelaces. Nice :)"
["photo_count"]=>
string(1) "8"
}
[3]=>
array(3) {
["aid"]=>
string(19) "2944783819003490957"
["name"]=>
string(12) "Cover Photos"
["photo_count"]=>
string(2) "12"
}
[4]=>
array(3) {
["aid"]=>
string(19) "2944783819003481818"
["name"]=>
string(14) "Mobile Uploads"
["photo_count"]=>
string(2) "55"
}
}
}
what should I do to get: ["aid"] , ["name" ] and ["photo_count"]?
like this
foreach ($array[0]['fql_result_set'] as $record) {
echo "{$record['aid']}, {$record['name']}";
}
i have an array with size=1 and when i try to get the current array its returns false but the array has values.
$article=getArticle($id);
if(is_array($article))
{ $article=current($article); }
the getArticle returns multidimensional array and when var_dump before current i can see the array.
If var_dump after current i get false.
if i use the code above (because the array size=1)
if(is_array($article))
{
foreach($article as $k=>$v)
{
$article=$v;
}
}
It works without any problem
Whats wrong with current?
Please help
===============EDIT========================
This is the var_dump before current
I have remove the content and summary because are large texts
array(1) {
[529]=>
array(14) {
["articles_id"]=>
string(3) "529"
["issue"]=>
string(3) "161"
["membership_type"]=>
string(1) "1"
["el"]=>
array(9) {
["title"]=>
string(23) "AUTOBIANCHI A112 ABARTH"
["url"]=>
string(23) "autobianchi-a112-abarth"
["summary"]=>
string(397) " i have remove it
"
["content"]=>
string(11580) " i have remove it too large
"
["meta_keywords"]=>
string(23) "AUTOBIANCHI,A112,ABARTH"
["meta_description"]=>
string(402) " "
["created"]=>
string(10) "1362076380"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
["categories_id"]=>
string(2) "12"
["authors_id"]=>
string(2) "16"
["brands_id"]=>
string(2) "36"
["models_id"]=>
string(3) "206"
["engines_id"]=>
string(3) "174"
["o-categories"]=>
array(1) {
["values"]=>
array(1) {
[12]=>
array(15) {
["categories_id"]=>
string(2) "12"
["object_id"]=>
string(1) "1"
["parent_id"]=>
string(1) "0"
["path"]=>
string(2) "12"
["handler"]=>
string(0) ""
["icon"]=>
string(0) ""
["tpl"]=>
string(12) "articles.php"
["alias"]=>
string(1) "0"
["is_nav"]=>
string(1) "1"
["pos"]=>
string(2) "10"
["depth"]=>
string(1) "0"
["cts"]=>
string(0) ""
["mts"]=>
string(0) ""
["configuration"]=>
string(0) ""
["el"]=>
array(15) {
["categories_id"]=>
string(2) "12"
["lang"]=>
string(2) "el"
["category_name"]=>
string(13) "Classic"
["sub_title"]=>
string(0) ""
["summary"]=>
string(0) ""
["image"]=>
string(0) ""
["html_code"]=>
string(0) ""
["meta_title"]=>
string(0) ""
["url"]=>
string(13) "classic"
["url_path"]=>
string(13) "classic"
["meta_keywords"]=>
string(0) ""
["meta_description"]=>
string(0) ""
["created"]=>
string(10) "1355235888"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
["o-authors"]=>
array(1) {
["values"]=>
array(1) {
[16]=>
array(4) {
["authors_id"]=>
string(2) "16"
["object_id"]=>
string(2) "26"
["image"]=>
string(0) ""
["el"]=>
array(9) {
["authors_id"]=>
string(2) "16"
["lang"]=>
string(2) "el"
["name"]=>
string(31) "Last First"
["last_name"]=>
string(10) "Last"
["first_name"]=>
string(20) "First"
["nick_name"]=>
string(0) ""
["created"]=>
string(10) "1360567827"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
["o-brands"]=>
array(1) {
["values"]=>
array(1) {
[36]=>
array(6) {
["brands_id"]=>
string(2) "36"
["object_id"]=>
string(2) "17"
["brand_name"]=>
string(11) "AUTOBIANCHI"
["created"]=>
string(10) "1363179463"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
["o-models"]=>
array(1) {
["values"]=>
array(1) {
[206]=>
array(7) {
["models_id"]=>
string(3) "206"
["object_id"]=>
string(2) "18"
["brands_id"]=>
string(2) "36"
["model_name"]=>
string(11) "A112 ABARTH"
["created"]=>
string(10) "1363179480"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
["o-engines"]=>
array(1) {
["values"]=>
array(1) {
[174]=>
array(6) {
["engines_id"]=>
string(3) "174"
["object_id"]=>
string(2) "19"
["engine"]=>
string(5) "1.050"
["created"]=>
string(10) "1363179448"
["last_updated"]=>
string(1) "0"
["status"]=>
string(1) "1"
}
}
}
}
}
Can you try
$article=getArticle($id);
if (is_array($article)) {
$article = reset($article);
}
or
$article=getArticle($id);
if (is_array($article)) {
$article = array_shift($article);
}
You can look into the documentation about reset and the documentation about array_shift.
If you copy array in getArticle() function then its internal pointer is lost.
I think this is the reason, it returns false.