array_combine function is not displaying repeated values [duplicate] - php

This question already has answers here:
How can I loop through two arrays at once? [duplicate]
(2 answers)
Closed 6 years ago.
These are the values to be displayed.
print_r(array_values($price));
print_r(array_values($mec_id));
Array ( [0] => 3100 [1] => 1600 [2] => 1600 [3] => 3100 [4] => 7500 [5] => 3500 )
Array ( [0] => 47 [1] => 41 [2] => 42 [3] => 45 [4] => 46 [5] => 48 )
I need to use two arrays at a time in the foreach loop.
$combined_array = array_combine($price, $mec_id);
foreach($combined_array as $price=>$mec_id)
{
echo '<br>'.$mec_id.'-';
echo $price.'<br>';
}
But, after using array_combined method, it is combining the repeated values too. I think it is parsing the array from ending while combining.
45-3100
42-1600
46-7500
48-3500

You need to change order in array_combine so:
$combined_array = array_combine($mec_id, $price);
because you can not create the same index twice

Related

Looking for duplicated values in an array for a certain key [duplicate]

This question already has answers here:
Get the keys for duplicate values in an array
(11 answers)
Closed 4 years ago.
I have an array:
Array
(
[0] => Array
(
[sku_code_part_id] => 1
[part_label] => blue
[part_value] => BLU
)
[1] => Array
(
[sku_code_part_id] => 2
[part_label] => Orange
[part_value] => ORG
)
[2] => Array
(
[sku_code_part_id] => 3
[part_label] => Orange
[part_value] => ORG
)
[3] => Array
(
[sku_code_part_id] => 4
[part_label] => Orange
[part_value] => ORG
)
[4] => Array
(
[sku_code_part_id] => 5
[part_label] => Green
[part_value] => GRE
)
[5] => Array
(
[sku_code_part_id] => 6
[part_label] => Red
[part_value] => RED
)
)
I'm wanting a simple way of checking the array $this->request->post['custom_parts'] for the any duplicated values assigned to the part_value keys.
So I can flag an error that 'ORG' is duplicated numerous times.
I've tried various methods such as removing duplications and comparing before and after. However I'm running into a number of issues with this.
Any ideas?
You may be able to use "array_key_exists"
http://php.net/manual/en/function.array-key-exists.php
You may want to write a function but here is a solution using foreach.
$part_values = [];
$part_values_duplicates = [];
foreach($this->request->post['custom_parts'] as $customPart){
if(!in_array($customPart['part_value'], $part_values)){
$part_values[] = $customPart['part_value'];
}
else {
$part_values_duplicates[] = $customPart['part_value'];
}
}
var_dump($part_values_duplicates);

Is it possible to divide the contents from an array into equal part/variable? [duplicate]

This question already has answers here:
Variable variables: when useful? [duplicate]
(3 answers)
what is "$$" in PHP
(3 answers)
How do I dynamically create the variable name in a PHP loop?
(1 answer)
How do I append a variable to create a new variable name
(2 answers)
Closed 4 years ago.
I am using print_r on a variable which gives me the following output
Array
(
[0] => 95.2
[1] => 94.7
[2] => 95
[3] => 33.6
)
Array
(
[0] => 100
[1] => 95
[2] => 91.90000000000001
[3] => 33.6
)
I want to split them into two variable dynamically. Like $var1 and $var2. If there were three iterations, i would want it to be three variables. Is it possible in php? In the current scenario i want
$var1= (
[0] => 95.2,
[1] => 94.7,
[2] => 95,
[3] => 33.6
)
and
$var2= (
[0] => 100,
[1] => 95,
[2] => 91.90000000000001,
[3] => 33.6
)
and i want to both to be generated dynamically and if possible i want the count of the iterations as well. Thank you in advance
<?php
$data[]=array(1,2,3);
$data[]=array(5,6,7);
$data[]=array(7,8,9);
//you can add many more here and it will still works fine
foreach ($data as $key=>$value){
$variable_name='var_'.$key;
$$variable_name=$value;
}
print_r($var_0);
print_r($var_1);
print_r($var_2);
Sure it is possible.

How to make array intersect in php? [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(27 answers)
Closed last year.
This is my two arrays:
$head_office = array(600,400,534,678,601,90);
$Sb_office = array(600,400,530,678,600,90,84);
My desire output:
array(600,400,678,90);
I am trying:
print_r(array_intersect($Sb_office,$head_office));
Output:
Array ( [0] => 600 [1] => 400 [3] => 678 [4] => 600 [5] => 90 )
How can I avoid the value 600 ?
NB: I am not removing duplicate values. I want to get matched values between 2 arrays.
Use array_unique().
$head_office = array(600,400,534,678,601,90);
$Sb_office = array(600,400,530,678,600,90,84);
print_r(array_unique(array_intersect($Sb_office,$head_office)));
Output
Array ( [0] => 600 [1] => 400 [3] => 678 [5] => 90 )
It seams that you want to avoid duplicate value 600. you can do it using array_unique() like below
print_r(arry_unique(array_intersect($Sb_office,$head_office)));
Use array_unique
Additionally you can use array_values to alter the keys of new array.
$head_office = array(600,400,534,678,601,90);
$Sb_office = array(600,400,530,678,600,90,84);
print_r(array_values(array_unique(array_intersect($Sb_office,$head_office))));
OUTPUT
Array ( [0] => 600 [1] => 400 [2] => 678 [3] => 90 )
Swap $head_office and $Sb_office
<?php
$head_office = array(600,400,534,678,601,90);
$Sb_office = array(600,400,530,678,600,90,84);
print_r( array_intersect($head_office, $Sb_office) );
Output:
Array
(
[0] => 600
[1] => 400
[3] => 678
[5] => 90
)
I have gotten a partial answer:
print_r( array_intersect_assoc($head_office, $Sb_office) );

PHP - Sorting an array of objects by object property [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Sort array of objects by one property
(23 answers)
Closed 8 years ago.
I trust you are well.
I would like to try and sort this array by the score property of the object. Below is an example of the data (print_r).
Array
(
[0] => stdClass Object
(
[device] => 352454530452548
[reg] => MAM 432A
[distance] => 823.36
[ha_points] => 1
[hb_points] => 235
[hc_points] => 7.5
[idling_points] => 111.5
[speeding_points] => 168
[total] => 523
[score] => 68.239895064127
)
[1] => stdClass Object
(
[device] => 3518020541565265
[reg] => SM** ***
[distance] => 851.07
[ha_points] => 14
[hb_points] => 136
[hc_points] => 6
[idling_points] => 50
[speeding_points] => 336
[total] => 542
[score] => 68.957730856451
)
The score can be anything from 0 to 100 and I would like to sort them into descending order (biggest first?). To make things more complicated, although the chances are very slim it is possible to have two identical scores in which case it doesn't matter which one is first.
Any ideas?
Thanks in advance,
Paul
A simple usort will do the job.
$arrData = array(/* ... */);
usort($arrData, function($a, $b) {
return $a->score < $b->score ? 1 : -1;
});

strip array with only one item in it without looping thru it and not knowing its index [duplicate]

This question already has answers here:
Reset PHP Array Index
(4 answers)
Closed 8 years ago.
Array
(
[2] => stdClass Object
(
[name] => test-song-poll-03
[description] => test-song-poll-03
[created_at] => 2014-05-02T23:19:06Z
[count] => stdClass Object
(
[approved] => 26638
[pending] => 0
[rejected] => 36923
[total] => 63561
)
[tpm] => 47
[approved_tpm] => 9
[pct] => 2
)
)
I have a function that uses array_filter and it returns what you see above. It will only return one object within the array. I do not know what the array index will be, but I know there will only be one item in the array. Is there an array function that strips down the array and just returns the content of it, since I don't need an array with just one item in it.
You should use:
$x = array_values($yourArrayName);
echo $x[0];
You can also use:
echo current($yourArrayName);

Categories