I got the following array:
array(4) {
[0] => array(2) {
[1] => string(1)
"2" ["month"] => string(2)
"01"
}[1] => array(2) {
[1] => string(2)
"74" ["month"] => string(2)
"02"
}[2] => array(2) {
[1] => string(3)
"233" ["month"] => string(2)
"03"
}[3] => array(2) {
[1] => string(2)
"21" ["month"] => string(2)
"05"
}
}
As you can see the value for April is missing. I parse it as follows:
$delivbymonth = $query->getResult();//A DQL Query which returns the above array
$MyArray = array();
for($i=0;$i<=11;++$i)
{
if(isset($delivbymonth[$i]['month']) && $delivbymonth[$i]['month']==$i+1)
{
$MyArray[$i] = $delivbymonth[$i][1];
}else{$MyArray[$i]=0;}
}
So I want a final array of 12 numbers (NOT a key=>value pairs). If there is no number (like for April here) I want the number in array set to zero. Everything works fine untill the for loop get to the omitted month. It sets the value for the 4th number in array to zero as it is supposed to, but after that ALL remaining elements in array are set to zero not regarding the fact that the value for May actually exists. The data for first 3 month is parsed correctly, i.e. the element in my final array are NOT zero (2, 74, 233). Does anybody know the reason for that?
Thank You
$myArray = array_fill(0, 12, 0);
foreach ($delivbymonth as $deliv) {
$myArray[intval($deliv['month'] - 1)] = $deliv[1];
}
var_dump($myArray);
OUTPUT:
array(12) {
[0] =>
string(1) "2"
[1] =>
string(2) "74"
[2] =>
string(3) "233"
[3] =>
int(0)
[4] =>
string(2) "21"
[5] =>
int(0)
[6] =>
int(0)
[7] =>
int(0)
[8] =>
int(0)
[9] =>
int(0)
[10] =>
int(0)
[11] =>
int(0)
}
Explanation:
The condition below in your original code was being met by the first 3 elements.
if(isset($delivbymonth[$i]['month']) && $delivbymonth[$i]['month']==$i+1)
$i = 0 => isset($delivbymonth[0]['month']) && ("01" == 1) -> true
$i = 1 => isset($delivbymonth[1]['month']) && ("02" == 2) -> true
$i = 2 => isset($delivbymonth[2]['month']) && ("03" == 3) -> true
$i = 3 => isset($delivbymonth[3]['month']) && ("05" == 4) -> FALSE
However on the fourth element (when $i = 3) the part of the condition
$delivbymonth[$i]['month']==$i+1
evaluates to false (5 == 4), then from this point on every loop will end up on the else clause, causing all subsequent elements to be 0.
Related
My code contains an array that has 7 elements and each element has a total number of different characters. I want, when the number of characters meets the criteria (<= 6) then create a new array.
The output is expected in the form of a two dimension array,
// My Variables, $value & $count
$value=array('as','fix','fine','is','port','none','hi','of');
for ($i=0; $i <count($value) ; $i++) {
$count[]=strlen($value[$i]);
}
Then have output like a,
// $value,
Array
(
[0] => as
[1] => fix
[2] => fine
[3] => is
[4] => port
[5] => none
[6] => hi
[7] => of
)
// Count the length $value and store in Variable $count,
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 2
[4] => 4
[5] => 4
[6] => 2
[7] => 2
)
and then I hope my code can produce output like this:
(Explode element where length <= 6)
// If length value in the variable $count,
Array
(
[0] => 2
[1] => 3
Total Length(5)
[2] => 4
[3] => 2
Total Length(6)
[4] => 4
Total Length(4)
[5] => 4
Total Length(4)
[6] => 2
[7] => 2
Total Length(4)
)
This is my question point:
// $Values RESULT
Array
(
[0] => Array
(
[0] => as
[1] => fix
)
[1] => Array
(
[0] => fine
[1] => is
)
[1] => Array
(
[0] => port
)
[1] => Array
(
[0] => none
)
[1] => Array
(
[0] => hi
[1] => of
)
)
Your examples were a bit hard to follow but here's what I've got:
<?php
$value=array('as','fix','fine','is','port','none','hi','of');
$final = [];
$accumulator = 0;
$final[0] = [];
$x = 0;
for ($i=0; $i <count($value) ; $i++) {
var_dump($accumulator);
if($accumulator + strlen($value[$i]) > 6) {
echo "adding ".$value[$i] ." to new\n\n";
$x++;
$final[$x] = [];
array_push($final[$x], $value[$i]);
$accumulator = strlen($value[$i]);
}else{
echo "adding ".$value[$i] . " to existing\n\n";
array_push($final[$x], $value[$i]);
$accumulator += strlen($value[$i]);
}
}
var_dump($final);
Yields
int(0)
adding as to existing
int(2)
adding fix to existing
int(5)
adding fine to new
int(4)
adding is to existing
int(6)
adding port to new
int(4)
adding none to new
int(4)
adding hi to existing
int(6)
adding of to new
array(5) {
[0]=>
array(2) {
[0]=>
string(2) "as"
[1]=>
string(3) "fix"
}
[1]=>
array(2) {
[0]=>
string(4) "fine"
[1]=>
string(2) "is"
}
[2]=>
array(1) {
[0]=>
string(4) "port"
}
[3]=>
array(2) {
[0]=>
string(4) "none"
[1]=>
string(2) "hi"
}
[4]=>
array(1) {
[0]=>
string(2) "of"
}
}
http://sandbox.onlinephpfunctions.com/code/20a63b83ad5524c5cd77e111bc15e197bf8bfba2
Sorry if this question is worded incorrectly or doesn't make any sense. What I am trying to do is write an if statement that checks if:
array(6) {
[5]=>
string(17) "Quality Assurance"
[6]=>
string(7) "Analyst"
[7]=>
string(19) "Developer/Front end"
[8]=>
string(18) "Developer/Back end"
[9]=>
string(4) "Test"
[10]=>
string(2) "hi"
}
Any of those keys, in this case, 5, 6, 7, 8, 9, 10 is in:
array(4) {
[0]=>
object(stdClass)#195 (2) {
["labour_type_id"]=>
int(5)
["required_labour_type_hours"]=>
int(40)
}
[1]=>
object(stdClass)#193 (2) {
["labour_type_id"]=>
int(6)
["required_labour_type_hours"]=>
int(80)
}
}
This second arrays "labour_type_id".
In this example, 5 and 6 would match.
I am trying to use the in_array() function but I am not sure how to access the labour_type_id of the second array.
My best attempt at the moment:
#foreach($labourTypes as $id => $name)
#if(in_array($id, $reqLabourTypes->labour_type_id))
Where labourTypes is the first array, and reqLabourTypes is the 2nd array.
Thanks.
I've cleaned up this little search for you to try and find it as you require:
$new = array_filter(array_map(function(&$item) use($requiredLabour, $labourTypes){
$key = array_search($item, $labourTypes);
foreach($requiredLabour as $elem){
if($elem['labour_type_id'] == $key) {
return array(
$key => $item,
'options' => $elem
);
}
}
}, $labourTypes));
Everything will be accessible in $new if found. It returns:
Array
(
[5] => Array
(
[5] => Quality Assurance
[options] => Array
(
[labour_type_id] => 5
[required_labour_hours] => 40
)
)
[6] => Array
(
[6] => Analyst
[options] => Array
(
[labour_type_id] => 6
[required_labour_hours] => 40
)
)
)
The above is just the output, you can change it to whatever you need it to be by simply editing the return array(..... inside to whatever you require.
Example/Demo
I have an array
dump($data);
*************************************
array(10) {
["12-male"] => string(1) "2"
["11-male"] => string(1) "2"
["10-female"] => string(1) "2"
["16-female"] => string(1) "2"
["9-male"] => string(1) "2"
["17-male"] => string(1) "4"
["14-male"] => string(1) "4"
["15-female"] => string(1) "4"
["13-female"] => string(1) "5"
["18-female"] => string(1) "6"
}
******************************************
I am DYNAMICALLY getting like sub arrays out of the array above
$rooms = array();
foreach ($data as $key => $value) {
$rooms['room'.$value][] = $key;
$rooms['room'.$value]['count'] = sizeof($rooms['room'.$value]);
}
dump($rooms);
******************************************
I get this result
Dump => array(4) {
["room2"] => array(6) { //array size=6
[0] => string(7) "12-male"
["count"] => int(6) //count of array size=6
[1] => string(7) "11-male"
[2] => string(9) "10-female"
[3] => string(9) "16-female"
[4] => string(6) "9-male"
}
["room4"] => array(4) { //array size=4
[0] => string(7) "17-male"
["count"] => int(4) //count of array size=4
[1] => string(7) "14-male"
[2] => string(9) "15-female"
}
["room5"] => array(2) { //array size=2
[0] => string(9) "13-female"
["count"] => int(1) //count of array size=1 (the problem here)
}
["room6"] => array(2) { //array size=2
[0] => string(9) "18-female"
["count"] => int(1) //count of array size=1 (the problem here)
}
}
My issue is that, the count is returned correctly after first 2 iterations, after that the count is always showing 1, no matter the size of array.
I tried count() as well but the result is the same.
You could do like below:
$rooms = array();
foreach ($data as $key => $value) {
if (!isset($rooms['room'.$value])) {
$rooms['room'.$value] = array('count' => 0);
}
$rooms['room'.$value][] = $key;
$rooms['room'.$value]['count']++;
}
But you don't need to add the count into your array.
The reason the count is doing that is that from room2 and room4 you are inserting 'count' on the first iteration, then on subsequent iterations 'count' is included in the sizeof() request. For room 5 and room6 as they are iterated only once sizeof() is only called once, before 'count' is inserted into the array, so it's not the index of 'count' not included in the result of sizeof for those items.
I have a PHP array when I used var_dump() this is the result I get:
array(1) { ["GetVehicleConfigurationByVehicleIdResult"]=> array(9) { ["Id"]=> string(1) "2" ["VIN"]=> NULL ["Year"]=> array(2) { ["Id"]=> string(4) "2006" ["Value"]=> string(4) "2006" } ["Make"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(5) "Acura" } ["Model"]=> array(2) { ["Id"]=> string(1) "2" ["Value"]=> string(2) "TL" } ["Trim"]=> array(2) { ["Id"]=> string(6) "268650" ["Value"]=> string(12) "3.2 Sedan 4D" } ["Mileage"]=> string(6) "100000" ["OptionalEquipment"]=> array(1) { ["EquipmentOption"]=> array(35) { [0]=> array(13) { ["DisplayName"]=> string(19) "V6, VTEC, 3.2 Liter" ["VehicleOptionId"]=> string(3) "204" ["IsSelected"]=> string(4) "true" ["OptionTypeDisplayName"]=> string(6) "Engine" ["OptionGroupName"]=> string(3) "N/A" ["DisplayNameAdditionalData"]=> string(3) "N/A" ["ManufactureCode"]=> string(0) "" ["OptionAvailabilityDisplayName"]=> string(3) "N/A" ["IsDefaultConfiguration"]=> string(4) "true" ["DetailName"]=> string(3) "N/A" ["NonBoldName"]=> string(3) "N/A" ["Footer"]=> string(3) "N/A" ["SortOrder"]=> string(4) "1000" }
How I can get the elements from this array?
Some of the elements are complex, like they are array inside array.
That is the formatted print out of the array to understand better:
Array
(
[GetVehicleConfigurationByVehicleIdResult] => Array
(
[Id] => 2
[VIN] =>
[Year] => Array
(
[Id] => 2006
[Value] => 2006
)
[Make] => Array
(
[Id] => 2
[Value] => Acura
)
[Model] => Array
(
[Id] => 2
[Value] => TL
)
[Trim] => Array
(
[Id] => 268650
[Value] => 3.2 Sedan 4D
)
[Mileage] => 100000
[OptionalEquipment] => Array
(
[EquipmentOption] => Array
(
[0] => Array
(
[DisplayName] => V6, VTEC, 3.2 Liter
[VehicleOptionId] => 204
[IsSelected] => true
[OptionTypeDisplayName] => Engine
[OptionGroupName] => N/A
[DisplayNameAdditionalData] => N/A
[ManufactureCode] =>
[OptionAvailabilityDisplayName] => N/A
[IsDefaultConfiguration] => true
[DetailName] => N/A
[NonBoldName] => N/A
[Footer] => N/A
[SortOrder] => 1000
)
I want to get: Id, VIN, Year, Make, Model, Trim,Mileage and OptionalEquipment and pass them as 1 single parameter to another method.
It solved:
$Id = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Id'];
$Year = $resultVehicleId['GetVehicleConfigurationByVehicleIdResult']['Year']['Value'];
You are correct. There are arrays inside of arrays here. And var_dump shows it very nicely so you can perfectly know how to navigate the levels of this multi-dimensional array.
If you want VIN just get $array['GetVehicleConfigurationByVehicleIdResult']['VIN']
For Year you need to get $array['GetVehicleConfigurationByVehicleIdResult']['Year']['Value']
I think you can guess the others now.
They are just array elements no matter how deep you go so you can just reference them by name like below:
$id = $that_array['GetVehicleConfigurationByVehicleIdResult']['Id'];
$id = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Id'];;
$vin = $array_name["GetVehicleConfigurationByVehicleIdResult"]['VIN'];
$year = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Year']
$make = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Make'];
$model = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Model'];
$trim = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Trim']['Value'];
$mileage = $array_name["GetVehicleConfigurationByVehicleIdResult"]['Mileage'];
$optional_equipment = $array_name["GetVehicleConfigurationByVehicleIdResult"]['OptionalEquipment']['EquipmentOption'][0]['DisplayName'];
For simplicity's sake I'm assuming this array is saved as $array.
You can access the data from the array like this:
$vin = $array['GetVehicleConfigurationByVehicleIdResult']['VIN'];
But you stated that you want to pass them all as a single parameter, so to do that you would probably want to just pass an array.
someFuntion($array['GetVehicleConfigurationByVehicleIdResult']);
I have had success merging two arrays by difference using the following code:
$a=array("2013-08-22"=>"12","2013-08-25"=>"5","2013-08-27"=>"10");
$b=array("2013-08-22"=>"1","2013-08-23"=>"3","2013-08-25"=>"5","2013-08-27"=>"10","2013-08-29"=>"5");
foreach ($b as $key => $value){
if(!array_key_exists($key, $a)){
$a[$key]=0;
}
}
This will return:
Array
(
[2013-08-22] => 0
[2013-08-23] => 0
[2013-08-25] => 5
[2013-08-27] => 10
[2013-08-29] => 0
[2013-12-22] => 12
)
The idea is for a to additionally hold the elements from b that are not present in a.
I am having issues now doing the same thing for the following array format:
$a=array(array("2013-12-22","12"),array("2013-08-25","5"),array("2013-08-27","10"));
$b=array(array("2013-08-22","1"),array("2013-08-23","3"),array("2013-08-25","5"),array("2013-08-27","10"),array("2013-08-29","5"));
I went to try this:
foreach ($b as $key => $value){
if(!array_key_exists($key, $a)){
$a[$key]=array($value[0], 0);
}
}
But the returned result is far from what I need:
Array
(
[0] => Array
(
[0] => 2013-12-22
[1] => 12
)
[1] => Array
(
[0] => 2013-08-25
[1] => 5
)
[2] => Array
(
[0] => 2013-08-27
[1] => 10
)
[3] => Array
(
[0] => 2013-08-27
[1] => 0
)
[4] => Array
(
[0] => 2013-08-29
[1] => 0
)
)
I understand they keys are no longer the dates, but how should I go about checking each array and making sure I don't get double entries?
$a = array(
array("2013-12-22","12"),
array("2013-08-25","5"),
array("2013-08-27","10"));
$b = array(
array("2013-08-22","1"),
array("2013-08-23","3"),
array("2013-08-25","5"),
array("2013-08-27","10"),
array("2013-08-29","5"));
$exists = array();
foreach ($a as $data) {
$exists[$data[0]] = 1;
}
foreach ($b as $data) {
if (array_key_exists($data[0], $exists)) {
continue;
}
$a[] = array($data[0], $data[1]);
}
$a now contains:
array(6) {
[0]=>
array(2) {
[0]=>
string(10) "2013-12-22"
[1]=>
string(2) "12"
}
[1]=>
array(2) {
[0]=>
string(10) "2013-08-25"
[1]=>
string(1) "5"
}
[2]=>
array(2) {
[0]=>
string(10) "2013-08-27"
[1]=>
string(2) "10"
}
[3]=>
array(2) {
[0]=>
string(10) "2013-08-22"
[1]=>
string(1) "1"
}
[4]=>
array(2) {
[0]=>
string(10) "2013-08-23"
[1]=>
string(1) "3"
}
[5]=>
array(2) {
[0]=>
string(10) "2013-08-29"
[1]=>
string(1) "5"
}
}