PHP array and string - php

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"];
}

Related

Add elements to existing array

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);

Data mismatch while dumping array in csv file

I have 7 array fetched by mysql and its working correctly each array have exactly 9 rows which are city_names but one of the array has only 7 rows since two values on the results where null . This causes confunsion while dumping file in csv. I use the below code. Is there any way to check it by city name without using foreach for eact array ?
for($i=0 ; $i <= $count-2 ; $i++)
{
$data[] = $all_restaurants_opr_no_temp_off[$i]['city_name'];
$data[] = $all_restaurants_opr[$i]['total'];
$data[] = $all_restaurants_opr_no_temp_off[$i]['total'];
$data[] = $restaurants_opr_temp_off[$i]['total'];
$data[] = $restaurants_opr_operations_closed[$i]['total'];
$data[] = $restaurants_opr_automated[$i]['total'];
$data[] = $restaurants_opr_automated_working[$i]['total'];
$data[] = $restaurants_opr_online_payment[$i]['total'];
$data[] = $restaurants_opr_online_payment_with_tempoff[$i]['total'];
$data[] = $restaurants_opr_atleast_one_order[$i]['total'];
fputcsv($fp, $data,",");
unset($data);
}
Array 1:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "687"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "568"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "139"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "232"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "57"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "113"
}
}
Array 2:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "674"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "547"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "135"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "202"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "56"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "111"
}
}
Array 3:(Problem is here)
array(6) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(2) "13"
}
[1]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(2) "21"
}
[2]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(1) "4"
}
[3]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(2) "30"
}
[4]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(1) "1"
}
[5]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(1) "2"
}
}
I'm not sure I understand the question 100%, but perhaps you can test each value if it is null and put a default value there if it is, for example, for this row, you could use this above your for loop:
foreach($badArray as $b){
$reformattedArray[$b['city_name']] = $b['total'];
}
then replace the bad array line with this in your for loop
data[] = is_null($reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']]) ? 'Default' : $reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']];

Get specific value from PHP array using foreach key value

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

Secondary Drop Down with Preloaded options

I have Sub-Categories that are related to a Main Category. My sub cat table has the main category id as a foreign key. I'd like to preload all subcategories using javascript and avoid an extra php file or db query if possible.
Both dropdowns are listing all the records but...
I'd like my second dropdown list of sub-categories to change dynamically onChange (select) of the first dropdown based on the first dropdown's value which is the main cat id.
Example,
$data = $this->Category->MainCategory->findall();
$this->set('data',$data);
$data2 = $this->Category->SubCategory->findall();
$this->set('data2',$data2);
My arrays look like this:
var_dump ($data) =
array(5) {
[0]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(10) "Accounting" ["doctype"]=> string(1) "2" } }
[1]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(15) "Human Resources" ["doctype"]=> string(1) "2" } }
[2]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(5) "Clubs" ["doctype"]=> string(1) "2" } }
[3]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(16) "Service" ["doctype"]=> string(1) "2" } }
[4]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(9) "Safety" ["doctype"]=> string(1) "2" } } }
var_dump($data2) =
array(20) {
[0]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(17) "Application Forms" ["main_category_id"]=> string(1) "2" } }
[1]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(19) "Benefit Claim Forms" ["main_category_id"]=> string(1) "2" } }
[2]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "3" ["name"]=> string(22) "Evaluations" ["main_category_id"]=> string(1) "2" } }
[3]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(11) "Leave Forms" ["main_category_id"]=> string(1) "2" } }
[4]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "5" ["name"]=> string(13) "Payroll" ["main_category_id"]=> string(1) "2" } }
[5]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "6" ["name"]=> string(17) "Recruitment" ["main_category_id"]=> string(1) "2" } }
[6]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "7" ["name"]=> string(24) "Training" ["main_category_id"]=> string(1) "2" } }
[7]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(10) "Accounting" ["main_category_id"]=> string(1) "1" } }
[8]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "9" ["name"]=> string(13) "Staff" ["main_category_id"]=> string(1) "2" } }
[9]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(14) "Codes" ["main_category_id"]=> string(2) "3" } }
[10]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "11" ["name"]=> string(28) "Reports" ["main_category_id"]=> string(2) "3" }
[11]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "12" ["name"]=> string(14) "Plan" ["main_category_id"]=> string(2) "4" } }
[12]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "13" ["name"]=> string(21) "Charts" ["main_category_id"]=> string(2) "4" } }
[13]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "14" ["name"]=> string(11) "Travel" ["main_category_id"]=> string(1) "4" } }
[14]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "15" ["name"]=> string(15) "Financials" ["main_category_id"]=> string(1) "4" } }
[15]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "16" ["name"]=> string(19) "Event Planning" ["main_category_id"]=> string(1) "4" } }
[16]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "17" ["name"]=> string(14) "Resources" ["main_category_id"]=> string(1) "4" } }
[17]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "18" ["name"]=> string(11) "Basics" ["main_category_id"]=> string(1) "4" } }
[18]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "19" ["name"]=> string(9) "News" ["main_category_id"]=> string(1) "4" } }
[19]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "20" ["name"]=> string(12) “Funding" ["main_category_id"]=> string(1) "4" } } }
--html form ….
<td>Category: </td>
<td><select id="main_cat">
<option value=""> </option>
<?php foreach($data as $row){
echo "<option value=".$row['MainCategory']['id'].">" .$row['MainCategory']['name']. "</option>";
} ?>
</select></td>
<td>Sub Category: </td>
<td><select id="sub_cat">
<option value=""> </option>
<?php foreach($data2 as $row){
echo "<option value=".$row[‘SubCategory']['id'].">" .$row['SubCategory']['name']. "</option>";
} ?>
</select></td>
…..
Create a two dimensional javascript array loaded with the sub-categories by category id, then reload the second drop down when the first changes. Highly simplified jsfiddle example.
For the php, you'll only need to worry about generating the subCats array. Something along the lines of:
<script type="text/javascript>
var subCats = [
<?php foreach($data2 as $row){
echo "[";
foreach($row as $subcat) {
echo "\"" . $subcat . "\",";
}
echo "]";
} ?>
</script>
just.another.programmer got me on the right track and with some other examples from Jonathan Kuhn, this is the result http://jsfiddle.net/wprLD/9/
<script type="text/javascript">
var subCats =
<?php
$js = array();
foreach($data2 as $sub){
//get the parent id (main_category_id)
$parent = $sub['SubCategory']['main_category_id'];
//if the parent doesn't exist, add it
if(!isset($js[$parent])){
//add array with name and id
$js[$parent] = array(array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']));
//parent does exist
} else {
//append this entry name and id
$js[$parent][] = array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']);
}
}
$js = array_values($js);
echo json_encode($js);
?>
;
function makeSubCatHtml(catId) {
var subCatHtml = "",
i;
//check if the subcats object has this cat Id.
if(subCats.hasOwnProperty(catId)){
for (i in subCats[catId]) {
subCatHtml += "<option value='"+subCats[catId][i].id+"'>" + subCats[catId][i].name + "</option>";
}
}
return subCatHtml;
}
$(document).ready(function () {
$("#MainCategory").bind("change", function () {
$("#SubCategory").html(
makeSubCatHtml(this.selectedIndex));
});
});
</script>

PHP Group By Day

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!

Categories