I have an array that looks like this
array(3) {
[0]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(2) "2"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[1]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(1) "5"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[2]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(1) "9"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
}
As you can see it consists of sort, date, month and year. I want to add new element into the same array with value "day" +1.
If the 8th of august 2015 is in array the it should also include 9th of august 2015.
I need a way to extract values, calculate new date and put back in all dates.
The result should look like this:
array(3) {
[0]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(2) "2"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[1]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(1) "5"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[2]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(1) "9"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[3]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(2) "3"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[4]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(1) "6"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
[5]=>
array(4) {
["sort"]=>
string(0) ""
["day"]=>
string(1) "10"
["month"]=>
string(1) "8"
["year"]=>
string(4) "2015"
}
}
Ideally it should also exclude new dates, if allready identical date is in the array.
Create array with new items and merge it with old array
$temp = array();
foreach($array as $item)
$temp[] = array_replace($item, array("day" => $item["day"]+1));
$res = array_merge($array, $temp);
To exlude repeating day
$days = array_reduce($array,
function($a, $i) { $a[$i['day']] = 1; return $a; ; },
array());
$temp = array();
foreach($array as $item)
if (! isset($days[$item["day"]+1]))
$temp[] = array_replace($item, array("day" => $item["day"]+1));
$res = array_merge($array, $temp);
Related
I'm using array_chunk function:
<?php
$chunks = array_chunk($coupons, 2,true);
?>
#foreach($chunks as $k => $c )
#if(2 == sizeof($c))
$c[0]['tr']->coupon_code
but the blow code not works fine :
$c[0]['tr']->coupon_code
or
$c[0]['tr']['coupon_code']
the content of var_dump($c):
array(2) { [217517]=> array(3) { ["tr"]=> object(stdClass)#71 (30) { ["id"]=> string(6) "217517" ["price"]=> string(5) "14400" ["date"]=> string(19) "2016-01-16 11:13:13" ["user_id"]=> string(5) "16433" ["ip"]=> string(14) "46.225.196.181" ["code"]=> string(13) "5699f4917b9c3" ["succ"]=> string(1) "1" ["admin_seen"]=> string(1) "0" ["user_seen"]=> string(1) "0" ["coupon_id"]=> string(5) "15373" ["coupon_parent"]=> string(5) "15315" ["coupon_code"]=> string(20) "24_1117C1_4644(1453)" ["coupon_code_user"]=> string(4) "1453" ["coupon_code_partner"]=> string(14) "24_1117C1_4644" ["shop_id"]=> string(4) "1117" ["payment_type"]=> string(1) "7" ["merchent_type"]=> string(1) "3" ["merchent_id"]=> string(1) "0" ["cradit_start_date"]=> string(19) "2015-12-29 05:05:05" ["cradit_end_date"]=> string(19) "2016-02-19 05:05:05" ["expired"]=> string(1) "0" ["pay_data"]=> NULL ["seri"]=> string(1) "C" ["to_friend"]=> string(1) "0" ["finance_id"]=> string(1) "0" ["app"]=> string(3) "web" ["expire_date"]=> string(19) "0000-00-00 00:00:00" ["expire_app"]=> string(0) "" ["buy_id"]=> string(13) "5699f4913103e" ["coupon_property_id"]=> string(3) "195" } ["offer"]=> object(stdClass)#67 (46) { ["id"]=> string(5) "15373" ["title"]=> string(21) "طرح دخترانه" ["slider_title"]=> string(0) "" ["short_title"]=> string(0) "" ["pic"]=> string(25) "Bracelets-off-Greek31.jpg" ["text"]=> string(0) "" ["attrs"]=> string(0) "" ["jest"]=> string(0) "" ["terms_of_use"]=> string(0) "" ["how_to_use"]=> string(0) "" ["city"]=> string(0) "" ["price"]=> string(5) "45000" ["off"]=> string(2) "68" ["min_users"]=> string(1) "0" ["max_users"]=> string(1) "0" ["current_users"]=> string(1) "2" ["expire"]=> string(1) "0" ["sell_start_date"]=> string(19) "2015-12-29 01:00:00" ["sell_end_date"]=> string(19) "2016-01-26 15:00:00" ["cradit_start_date"]=> string(19) "0000-00-00 00:00:00" ["cradit_end_date"]=> string(19) "0000-00-00 00:00:00" ["admin_expire_date"]=> string(19) "0000-00-00 00:00:00" ["showin_other"]=> string(1) "0" ["showin_slider"]=> string(1) "0" ["showin_slider_cat"]=> string(1) "0" ["moment"]=> string(1) "0" ["seri"]=> string(1) "C" ["shop_id"]=> string(4) "1117" ["tractions"]=> string(1) "0" ["status"]=> string(1) "0" ["url"]=> string(0) "" ["shadyab_profit"]=> string(4) "6400" ["fake_sell"]=> string(1) "3" ["confirm"]=> string(1) "0" ["adviser"]=> string(1) "0" ["parent"]=> string(5) "15315" ["description"]=> string(0) "" ["old"]=> string(1) "0" ["email_text"]=> string(0) "" ["zone"]=> string(0) "" ["views"]=> string(1) "0" ["max_buy"]=> string(1) "0" ["likes"]=> string(1) "0" ["alt"]=> string(70) "فروشگاه گیوا گالری, خرید اینترنتی کالا" ["sizes"]=> string(0) "" ["name"]=> string(11) "مدل ها" } ["details"]=> object(stdClass)#66 (22) { ["id"]=> string(6) "205585" ["tr_id"]=> string(6) "217517" ["title"]=> string(59) "دستبند چرم پلاک استیل طرح فانتزی" ["short_title"]=> string(0) "" ["pic"]=> string(17) "dastband_(2)1.jpg" ["text"]=> string(627) "
Problem here is that you preserve keys form original array. So, you don't have 0 or 1 key in a chunk $c.
Either you do not preserve keys $chunks = array_chunk($coupons, 2) as you have id in a tr object within id property and use $c[0]['tr']->coupon_code
Or you should get keys of a chunk $c with array_keys for example:
foreach($chunks as $k => $c) {
$keys = array_keys($c);
echo $c[$keys[0]]['tr']->coupon_code;
}
$c[0]['tr']['coupon_code'] is an object of type object(stdClass) .
I suppose you use json_decode before the array_chunk.
If you use json_decode($yourjson), you have to use json_decode($yourjson, true); to convert the json into array instead of stdClass.
EDIT:
Check too the keys of $c with vardump(array_keys($c)). I'm not sure $c[0] is defined.
I have an array containing dates (year) and position.
I need to build a string out of it, where:
Every position is separated by a / if it's in a different year ; and a - must appear if there was no result on a year. And if we have a -, then there is no need to use the / to separate the years ...
I'm struggling to build a logic and code for it.
Example:
array(7) {
[0]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "2" }
[1]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "4" }
[2]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "5" }
[3]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> string(1) "5" }
[4]=> array(2) {
["year"]=> string(4) "2015"
["hformpos"]=> int(0) }
[5]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "2" }
[6]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "3" } }
Should show: 32/05542
And
array(7) {
[0]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "2" }
[1]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "4" }
[2]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "5" }
[3]=> array(2) {
["year"]=> string(4) "2014"
["hformpos"]=> string(1) "5" }
[4]=> array(2) {
["year"]=> string(4) "2013"
["hformpos"]=> int(0) }
[5]=> array(2) {
["year"]=> string(4) "2011"
["hformpos"]=> string(1) "2" }
[6]=> array(2) {
["year"]=> string(4) "2011"
["hformpos"]=> string(1) "3" } }
32-5542-
Thank you!
I am assuming that the array is sorted in the descending order of the dates(year).
$year_flag = $arrays[0]["year"];
$result = "";
foreach( $arrays as $array )
{
if($year_flag == $array["year"]){
if( $array["hformpos"] !==0){
$result.= $array["hformpos"];
}else {
$result.= "-";
}
}elseif( $array["hformpos"] ===0){
$result.= "-";
}else{
$result.= "/";
if( $array["hformpos"] !==0){
$result.= $array["hformpos"];
}else {
$result.= "-";
}
}
$year_flag = $array["year"];
}
I thought to access the value of '1' here, I would need to the following
$data['category'][0];
["category"]=> array(1) { [0]=> string(1) "1" }
But it doesn't return anything. How would I access the string(1) section to get the value 1?
Here is the full var dump of the variable $data array
array(18) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_id_48"]=> string(37) "{filedir_1}Tharp_BIO_Pic300x20038.jpg" ["field_ft_48"]=> string(4) "none" ["revision_post"]=> array(26) { ["RET"]=> string(65) "/account/add-submission" ["URI"]=> string(22) "account/add-submission" ["XID"]=> string(40) "3ee1766dfdbe4684831021c99a9197beaede03be" ["return_url"]=> string(36) "account/submission-complete/ENTRY_ID" ["author_id"]=> string(1) "1" ["channel_id"]=> string(1) "4" ["entry_id"]=> string(1) "0" ["ACT"]=> string(1) "4" ["meta"]=> string(556) "pdKeUQVJTA6FeLnmeqtK0gGu2C1S2gKOvRrMDjjKMou7JAp2HVA48Gn+yXTjY4tKuBam5rlyszhe3rEF2eClOB5bRPEJ8NYeh/qPBSkDuhuk0j+XYrQ0R7dJhaHZPIr1b5sge8/kqmWj2qvrpO5pE/iC6X4scIO2HmOPjWb4Sea2VgGwgQ70j7Qr1QmHlQAIZ95DXMp3YkietUWLWaFKvr8XwSx+vUhKEaueVoAbP0Le3fu0rMqz2LuZIScGpwn4yPJbenkc0P5ME/nM9CsfnzYPmM1cwTHO1Xe/wtJ3HGbNcglfn+A9ubz1GBNULgUvxYAW6eFrhqfAJ2omfiwSzpQkISJaDZvZofjOCHjiS7VaUIDgWOrznOm7oWR3m5Ut4TOxmsX2jeKpUAvLJQppc1+1hormnRSA0mambV0uodflDaZEZbPKmjWxsZD3doNJzmIG29bQtBV+UWdQ4xkxyM6fhyMmUkKGAgE+Xegkp/zK7+AXc4s8bEBENPAa1UbCkh0XEq4IYIqWRzYL/T2bfyySCPzvrbFBErIwj3jUF+w=" ["return"]=> string(36) "account/submission-complete/ENTRY_ID" ["site_id"]=> string(1) "1" ["title"]=> string(3) "asd" ["submission_file_hidden_file"]=> string(0) "" ["submission_file_hidden_dir"]=> string(1) "1" ["type"]=> string(3) "1.1" ["category"]=> array(1) { [0]=> string(1) "1" } ["snap_FxlgTgCET"]=> string(13) "vyHdSjitmEUoV" ["submission_file"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_id_48_hidden_file"]=> string(0) "" ["field_id_48_hidden_dir"]=> string(1) "1" ["field_id_48"]=> string(24) "Tharp_BIO_Pic300x200.jpg" ["field_ft_48"]=> string(4) "none" ["allow_comments"]=> string(1) "y" ["entry_date"]=> string(10) "1380114180" ["status"]=> string(4) "open" ["previous_status"]=> bool(false) } }
To access that data you have to use $data['revision_post']['category'][0] because category array is in revision_post array.
The value of $data['category'][0] is an array. So you would access like this:
$data['category'][0] = array('1');
echo $data['category'][0][0];
Try it for yourself by copying and pasting here http://writecodeonline.com/php/
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.
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!