PHP get array keys where input user dynamical values [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I have array with data:
$data = [
Array (
[0] => Array ( [lcd] => 14, [cpu] => Intel, [vga] => Nvidia )
[1] => Array ( [lcd] => 14, [cpu] => AMD, [vga] => Nvidia )
[2] => Array ( [lcd] => 15, [cpu] => Intel, [vga] => Integrated )
[3] => Array ( [lcd] => 15, [cpu] => AMD, [vga] => AMD )
[4] => Array ( [lcd] => 16, [cpu] => Intel, [vga] => Іntegrated )
[5] => Array ( [lcd] => 16, [cpu] => AMD, [vga] => Nvidia )
);
AND user form:
<form>
<p>LCD</p>
<input name="lcd[]" value="14">
<input name="lcd[]" value="15">
<input name="lcd[]" value="16">
<p>CPU</p>
<input name="cpu[]" value="Intel">
<input name="cpu[]" value="AMD">
<p>VGA</p>
<input name="vga[]" value="Nvidia">
<input name="vga[]" value="AMD">
<input name="vga[]" value="Integrated">
</form>
User pick some inputs and I need to get keys from $data array:
For example user click lcd = 14, lcd = 15 and cpu = Intel
I get array from user:
Array ( [lcd] => Array ( [0] => 14 [1] => 15 ) [cpu] => Array ( [0] => Intel ) )
I need keys from $data array where lcd = 14 OR 15 AND cpu = Intel
It should be dynamically changed depending on which value the user chooses.
I can get data when I know values:
$result = array_filter($data, function($data) {
return $data['cpu'] == 'Intel' && in_array($data['lcd'], ['14', '15']);
});
But I can't do this with user values.

Related

Order multidimensional array according to a second array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I would like to order $ArrayToOrder according to column SecondArrayField2 in $SecondArray, where the link between the two arrays are Field_3 (in $ArrayToOrder) and SecondArrayField1 in $SecondArray.
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3
)
[1] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2
)
[2] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11
)
)
$SecondArray=Array
(
[0] => Array
(
[SecondArrayField1] => 11
[SecondArrayField2] => Bruce
)
[1] => Array
(
[SecondArrayField1] => 3
[SecondArrayField2] => Arthur
)
[2] => Array
(
[SecondArrayField1] => 2
[SecondArrayField2] => Mary
)
)
Desired result as follows:
$ArrayToOrder=Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 //(Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 //(Mary)
)
)
Here is snippet you can use,
// first fetching key as SecondArrayField2 and SecondArrayField1 as value
$Field_3 = array_column($SecondArray, "SecondArrayField1","SecondArrayField2");
// sort by key alphabetically
ksort($Field_3);
//
$sorted = array_values(array_map(function($v) use ($ArrayToOrder) {
// first fetched Field_3 matching with current value of $Field_3 in the order
// get the index of matching Field_3
// save it to sorted array its sub array
return $ArrayToOrder[array_search($v, array_column($ArrayToOrder,'Field_3'))];
}, $Field_3));
print_r($sorted);die;
Demo.
Output:
Array
(
[0] => Array
(
[Field_1] => 13
[Field_2] => 15
[Field_3] => 3 //(Arthur)
)
[1] => Array
(
[Field_1] => 121
[Field_2] => 20
[Field_3] => 11 // (Bruce)
)
[2] => Array
(
[Field_1] => 25
[Field_2] => 17
[Field_3] => 2 // (Mary)
)
)

Combine multi array to one array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a PHP array like the following one and I would like to combine a multi array to one array:
Array
(
[0] => ADB_DW2017
[1] => LM9
[2] => MS_OF2013
)
Array
(
[0] => NK
[1] => PV
[2] => NK
)
Array
(
[0] => 15
[1] => 25
[2] => 10
)
Array
(
[0] => 250
[1] => 111
[2] => 150
)
Array
(
[0] => 450
[1] => 123
[2] => 250
)
Array
(
[0] => 0
[1] => Mien thue
[2] => 5
)
Array
(
[0] => 200
[1] => 12
[2] => 100
)
Array
(
[0] => 6.750
[1] => 3.075
[2] => 2.500
)
I want to combine like this, with the highest performance :
Array
(
[ADB_DW2017]=>array([suppiler]=>NK
[min_pro]=>15
[max_pro]=>250
[avg_pro]=>450
[tax]=>0
[com]=>200
[sum]=>6.750
)
)
My question is : How to combine a multi array to one array in PHP with the highest performance ?
I assume array names are $first_array,$second_array,...... (so change varabile names accordingly).
Do like below:-
$final_array = array();
foreach ($first_array as $key=> $arr){
$final_array[$arr] =array(
'suppiler'=>(isset($second_array[$key]))? $second_array[$key]: '',
'min_pro'=>(isset($third_array[$key]))? $third_array[$key]: 0,
'max_pro'=>(isset($fourth_array[$key]))? $fourth_array[$key]: 0,
'avg_pro'=>(isset($fifth_array[$key]))? $fifth_array[$key]: 0,
'tax'=>(isset($sixth_array[$key]))? $sixth_array[$key]: '',
'com'=>(isset($seventh_array[$key]))? $seventh_array[$key]: 0,
'sum'=>(isset($eigth_array[$key]))? $eigth_array[$key]: 0
);
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/829938

Convert object/array to array with comma and bracket [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Our Result:
Array (
[0] => Array ( [0] => Province [1] => Party [2] => Age [3] => Name [4] => Gender )
[1] => Array ( [0] => Quebec [1] => NDP [2] => 22 [3] => Liu, Laurin [4] => Female )
[2] => Array ( [0] => Quebec [1] => Bloc Quebecois [2] => 22 [3] => Mourani, Maria [4] => Female )
)
I want a result looking like this: How to convert like this?
array(
['Province'=>'Quebec','Party'=>'NDP','Age'=>22,'Name'=>'Liu, Laurin','Gender'=>'Female'],
['Province'=>'Quebec','Party'=>'Bloc Quebecois','Age'=>43,'Name'=>'Mourani, Maria','Gender'=>'Female']
)
OR
array(
['Province', 'Party', 'Age', 'Name', 'Gender'],
['Quebec', 'NDP', 22, 'Liu, Laurin', 'Female'],
['Quebec', 'Bloc Quebecois', 43, 'Mourani, Maria', 'Female']
)
Pretty simple using an array_combine() to set the headers for each row, and just walking the array setting those headers:
$headers = array_shift($myArray);
array_walk(
$myArray,
function(&$row) use ($headers) {
$row = array_combine($headers, $row);
}
);

create new array php multidimensional [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have array
Array
(
[0] => Array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
)
how to create new array become
Array
(
[Personal Impact] => Array
(
[Position] => array
(
[0] => array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
)
)
)
Is it possible?
Yes it's possible you can just foreach(your values as key => value) {} it or something like this:
Sample Fiddle
<?php
$new_values = array();
foreach($old_values as $key => $values) {
$new_values[$values['cluster_name']][$values['driver_display_name']][] = array(
'answer1' => $values['answer1'],
'answer2' => $values['answer2'],
'PercentEVQ' => $values['PercentEVQ'],
'PercentEVQ2' => $values['PercentEVQ2'],
'driver_display_name' => $values['driver_display_name'],
'cluster_name' => $values['cluster_name'],
);
}
Are you defining a new array?
$new_array['Personal Impact']['Position'] = $old_array;

how to make the value of an array as the keys of the values of another array in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merging PHP array, one as Keys the other as Values?
i have an array of keys, and i have an array of values..how to make the values of arrray A, as the "keys" of the values of array B ?
array A
[0] => 224
[1] => 77
[2] => 78
[3] => 79
[4] => 80
[5] => 81
[6] => 82
[7] => 76
)
1
array B
Array
(
[0] => Men Shoes
[1] => Fashion Accessories
[2] => Men Apparels
[3] => Shoes & Belts
[4] => Watches & Clocks
[5] => Women Apparels
[6] => Others
[7] => Bags
what i want to happen is make it like this
array(
[224] => Men Shoes
[77] => Fashion Accessories
[78] => Men Apparells
[79] => Shoes & Belts
[80] => Watches & clocks
[81] => Women Apparels
[82] => Others
[76] => Bags
)
You can use array_combine()
Creates an array by using the values from the keys array as keys and
the values from the values array as the corresponding values.
You can use array_combine()
Sample code for you.
<?PHP
$array_a = array
(
0 => 224,
1 => 77,
2 => 78,
3 => 79,
4 => 80,
5 => 81,
6 => 82,
7 => 76
);
$array_b = array
(
0 => "Men Shoes",
1 => "Fashion Accessories",
2 => "Men Apparels",
3 => "Shoes & Belts",
4 => "Watches & Clocks",
5 => "Women Apparels",
6 => "Others",
7 => "Bags"
);
$new_array = array_combine($array_a,$array_b);
print_r($new_array);
?>

Categories