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
Related
This question already has answers here:
How to filter a two dimensional array by value
(6 answers)
Closed 4 years ago.
I have an array $arrItems['items'] in which 5 more arrays (associate array) and each array contain 5 element (with the keys: f_name, l_name, contact, address, seller_id).
I want to get all those arrays (from $arrItems['items']) in which element of seller_id is 1 like "seller_id"=>1 Code given below.
Please guide me how use foreach loop or else...
array(5)
{
[0] =>
array(5)
{
["f_name"] =>
string(3) "abc"
["l_name"] =>
string(3) "xyz"
["contact"] =>
string(5) "12345"
["address"] =>
string(3) "xyz"
["seller_id"] =>
string(1) => "1"
}
[1]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"1"
}
[2]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"5"
}
[3]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"1"
}
[4]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"1"
}
You can use array-filter:
array_filter — Filters elements of an array using a callback function
In your case:
$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
return $sellerId == $e["seller_id"]; });
You can use array_filter to filter the items of array
$arr = array_filter($arrItems['items'], function($arr) {
return $e["seller_id"] == 1;
});
foreach ($arrItems['items'] as $subarray) {
if ($subarray[seller_id] === 1) {
$result[] = $subarray;
}
}
is this what you need?
Here you can find simple solution using foreach.
$arr = [];
foreach ($arrItems['items'] as $i => $row) {
if ($row['seller_id'] != 1) {
// Ignore row if seller_id is not 1
continue;
}
// here you got only elements with seller_id = 1
// so you can add them to a new array
$arr[] = $row;
}
// After the loop (foreach) in $row you get only elements with seller_id 1.
// If they must be in $arrItems['items'] use it
$arrItems['items'] = $arr;
echo '<pre>';
print_r($arrItems['items']);
echo '</pre>';
Result:
Array
(
[f_name] => abc
[l_name] => xyz
[contact] => 12345
[address] => xyz
[seller_id] => 1
)
Array
(
[f_name] => abc
[l_name] => xyz
[contact] => 12345
[address] => xyz
[seller_id] => 1
)
Array
(
[f_name] => abc
[l_name] => xyz
[contact] => 12345
[address] => xyz
[seller_id] => 1
)
For simplest way to get it just two line of code.
$key = array_search(1, array_column($arrayItems, 'seller_id'));
print_r($arrayItems[$key]);
This method use binary search technics, when number 100000303 foreach loop will take a lot of time to finished it.
This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
Referring to this question & this, i have a little different problem here.
I have an array like this:
Array
(
[0] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1201"
)
[1] => Array
(
[name] => "Size"
[value] => "Small"
[id] => "1203"
)
[2] => Array
(
[name] => "Size"
[value] => "Medium"
[id] => "1204"
)
[3] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1205"
)
[4] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1206"
)
[5] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1207"
)
)
Above array have repetition of Large Three times, i want to identify unique on key based value. and remove that index (0,1,2,3,4,5) from that array.
Mentioned questions contains problems like this, but not the exact problem i am facing.
I am trying like this:
array_map("unserialize", array_unique(array_map("serialize", $input)));
but not working.
Since you have not answered my question yet I assume "id" is irrelevant.
By using array_column to make the array associative on "value" and it will delete any duplicates, then array_values will reset the keys to indexed.
This way you don't have to loop at all.
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1207"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
}
https://3v4l.org/aOhVS
If you want to keep the lowest "id", and the "id" is higher the further down in the array you go (as in your example), then you can use rsort($arr); before the code.
rsort($arr);
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1201"
}
}
https://3v4l.org/VtgAM
You could try make a foreach creating another array like you need
$arrayOrdenado = array();
foreach($array as $a){
$arrayOrdenado[$a["value"]][] = $a;
}
I have two arrays:
One:
array(4) {
[0]=> array(2) {
[0]=> string(19) "Ford"
[1]=> string(1) "1"
}
[1]=> array(2) {
[0]=> string(15) "Chevrolet"
[1]=> string(1) "1"
}
[2]=> array(2) {
[0]=> string(7) "VW"
[1]=> string(1) "1"
}
[3]=> array(2) {
[0]=> string(4) "Fiat"
[1]=> string(1) "3"
}
}
Two:
array(6) {
[0]=> string(7) "#581845"
[1]=> string(7) "#900C3F"
[2]=> string(7) "#C70039"
[3]=> string(7) "#FF5733"
[4]=> string(7) "#FFC300"
[5]=> string(7) "#DAF7A6"
}
Now, I need the first array combining with the second, excluding the elements of the second array that are not used by the first. At the end, I want to receive an array like:
[0]=> {
[0]=> "Ford",
[1]=> "1",
[2]=>"#581845"
}
[1]=>
...
}
Provided you always have more colors than auto makes, you can do this:
$makes = [
[
"Ford",
"1"
],
[
"Chevrolet",
"1"
],
[
"VW",
"1"
],
[
"Fiat",
"3"
]
];
$colors = [
"#581845",
"#900C3F",
"#C70039",
"#FF5733",
"#FFC300",
"#DAF7A6"
];
foreach($makes as &$currMakeTuple)
{
$currMakeTuple[] = array_shift($colors);
}
print_r($makes);
Array
(
[0] => Array
(
[0] => Ford
[1] => 1
[2] => #581845
)
[1] => Array
(
[0] => Chevrolet
[1] => 1
[2] => #900C3F
)
[2] => Array
(
[0] => VW
[1] => 1
[2] => #C70039
)
[3] => Array
(
[0] => Fiat
[1] => 3
[2] => #FF5733
)
)
You should probably check that condition and have a contingency for it.
Another one solution. Independent of the number of colors. Will add to brands only those colors which keys are equal to the brands' keys.
$brands = [
['Ford', 1],
['Chevrolet', 1],
['VW', 1],
['Fiat', 3],
];
$colors = [
'#581845',
'#900C3F',
'#C70039',
'#FF5733',
'#FFC300',
'#DAF7A6',
];
array_walk($brands, function(&$value, $key, $colors) {
if (isset($colors[$key])) {
$value = array_merge($value, [$colors[$key]]);
}
}, $colors);
print_r($brands);
I'm not saying you should do it this way, but I thought it was worth showing an alternative that used array functions instead:
var_dump(array_map(function($make, $color) {
$make[] = $color;
return $make;
}, $makes, array_slice($colors, 0, count($makes))));
I used:
array_slice to reduce the $colors array, should it be too long.
array_map with a lambda (anonymous function) that will apply the lambda to each element and return the final array upon completion.
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.
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']);