I am having difficulty with an multidimensional array. Here is a portion of the array. It's dynamic in that the top level array can have any number of elements and the child arrays, can have any number of elements as well. But each child array will contain the same amount of elements. If one child array has 16 elements, they will all have 16 elements, and if one has 20 elements, they will all have 20 elements.
[1] => Array
(
[1] => 0
[2] => 25
[3] => 38
[4] => 50
[5] => 63
[6] => 75
[7] => 85
[8] => 88
[9] => 100
[10] => 113
[11] => 125
[12] => 138
[13] => 150
[14] => 163
[15] => 175
[16] => 188
)
[2] => Array
(
[1] => 48
[2] => 37.22
[3] => 52.56
[4] => 63.17
[5] => 74.45
[6] => 87.98
[7] => 96.11
[8] => 98.36
[9] => 111.67
[10] => 132.20
[11] => 146.87
[12] => 160.85
[13] => 174.39
[14] => 187.70
[15] => 203.04
[16] => 215.90
)
What I am trying to do is to extract the data in a format like:
ProductCode: [2][1][1][2], Width: [2][1], Height: [1][2], Price: [2][2].
Can someone point me in the right direction of how I can loop over this to extract the data like I need to?
I ended up creating a stored procedure in SQL 2008 to handle this instead of using PHP.
Related
Hi I have a collection of numbers and I want to find the most occurring number and then the 2nd most occurring and then the third most occurring till 10 and store the result in a array.
The collection looks like this
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => 12
[1] => 194
[2] => 241
[3] => 4
[4] => 29
[5] => 4
[6] => 12
[7] => 15
[8] => 21
[9] => 31
[10] => 281
[11] => 4
[12] => 6
[13] => 4
[14] => 2
[15] => 6
[16] => 4
[17] => 4
[18] => 4
[19] => 15
[20] => 4
[21] => 4
[22] => 13
[23] => 10
[24] => 8
[25] => 8
[26] => 2
[27] => 2
[28] => 2
[29] => 17
[30] => 4
[31] => 20
[32] => 2
[33] => 4
[34] => 20
[35] => 6
)
)
So I want to find the most occurring one and so on till 10th most occurring number.
use array_count_values, here is your reference link http://www.w3schools.com/php/func_array_count_values.asp
<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>
Output
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
So i have 3 different ( yet they share some similarities ) and i would like to combine these into one to perform some calculations
Array
(
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
[1] => Array
(
[0] => Amber
[1] => 16
[3] => 2.0%
[4] => 0.2
[5] => 28.6
[6] => 1.8
[7] => 1861.1
[8] => 116.3
[9] => 0.0
[10] => 0.0
[11] => 234862.2
[12] => 3403.8
[13] => 69.0
[14] => 0.8%
[15] => 10
[16] => 6
[17] => 62.5%
[18] => 37.5%
)
[2] => Array
(
[0] => Amie
[1] => 106
[3] => 13.5%
[4] => 3.0
[5] => 721.7
[6] => 6.8
[7] => 12268.0
[8] => 115.7
[9] => 0.0
[10] => 0.0
[11] => 127011.0
[12] => 6350.5
[13] => 20.0
[14] => 9.7%
[15] => 54
[16] => 52
[17] => 50.9%
[18] => 49.1%
)
)
Array 2
Array
(
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
)
Array 3
[0] => Array
(
[0] => Agent
[1] => Answered
[2] => Missed
[3] => Contribution
[4] => Per Hour
[5] => Total Ring Time
[6] => Mean Ring Time
[7] => Total Talk Time
[8] => Mean Talk Time
[9] => Total Wrap Time
[10] => Mean Wrap Time
[11] => Total Session Time
[12] => Mean Session Time
[13] => Number of Sessions
[14] => % Util
[15] => Agent Hang-Ups
[16] => Caller Hang-Ups
[17] => Agent Hang-Ups Percent
[18] => Caller Hang-Ups Percent
)
[1] => Array
(
[0] => (7312
[1] => 1
[3] => 0.0%
[4] => 459.8
[5] => 0.0
[6] => 0.0
[7] => 0.4
[8] => 0.4
[9] => 0.0
[10] => 0.0
[11] => 7.8
[12] => 2.6
[13] => 3.0
[14] => 5.5%
[15] => 1
[17] => 100.0%
[18] => 0.0%
)
[2] => Array
(
[0] => Amber
[1] => 414
[2] => 9
[3] => 9.3%
[4] => 6.3
[5] => 1880.1
[6] => 4.4
[7] => 65209.8
[8] => 157.5
[9] => 240.4
[10] => 0.6
[11] => 234862.2
[12] => 3403.8
[13] => 69.0
[14] => 27.8%
[15] => 290
[16] => 124
[17] => 70.0%
[18] => 30.0%
)
So in this example i have 3 arrays, all have the same first initial array. But these arrays can differ in size and they may not share the same people in in.
People can also be in different places so i.e.
Array 1 : Common -> Amber -> Amie
Array 2 : Common ->
Array 3 : Common -> 7312 -> Amber ->Amie
Now the final array should look like
Array 4 : Common -> 7312 -> Amber -> Amie
All the values from all arrays should also be combined so i.e:
Amber 1st array / 1st value : 16
Amber 2nd array / 2nd value : 414
Amber final array value : 414 + 16 = 430
Hopefully i have made question clear.
Basically, you sum up all the info you got, remove the headers. Then rebuild a new array with the info consolidation.
//merge all arrays, remove the first element, it is headers.
$my_total_array = array_merge(
array_splice($array1,1),
array_splice($array2,1),
array_splice($array3,1)
);
$myagents = array();
foreach($my_total_array as $agent){
if(!isset($myagents[$agent[0]])){
//Never met the agent, add them.
$myagents[$agent[0]] = $agent;
}else{
//We already seen the agent, do maths.
$myagents[$agent[0]][1]+=$agent[1];
//TODO: Apply consolidation rules for other fields.
}
}
//Put the headings back at the beggining.
array_unshift($myagents,$array1[0]);
print_r($myagents);
I hope this helps.
EDIT: From PHP Docs
Merges the elements of one or more arrays together so that the values
of one are appended to the end of the previous one. It returns the
resulting array.
If the input arrays have the same string keys, then the later value
for that key will overwrite the previous one. If, however, the arrays
contain numeric keys, the later value will not overwrite the original
value, but will be appended.
I have a array like this :
Array
(
[0] => 395
[1] => 0
[2] => 395
[3] => 395
[4] => 39
[5] => 17
[6] => 11
[7] => 35
[8] => 21
[9] => 11
[10] => 11
[11] => 0
[12] => 0
[13] => 0
[14] => 0
[15] => 0
[16] => 0
[17] => 375
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 22
[23] => 215
[24] => 215
[25] => 42
[26] => 163
[27] => 163
[28] => 61
[29] => 61
[30] => 134
[31] => 134
)
Now, i get the maximum Value of that array with this code :
echo max($similar);
For the array i said, the output will be : 395 that is in the array[0] and array[2] and array[3].
Now, i want to know How can i give this number (395) and get the location of that in the array ?
For example,
I need a function like this :
echo_value_from_num(395); // Output :: 0
The output is zero, mean first time 395 appeard in the [0] of array.
How we can get that number in the array with that Values ?
The function array_serach (http://no2.php.net/array_search) will return the key for a certain element.
In your example, it would be something like this:
$max = max($similar);
$key = array_search($max,$similar);
This will give you the locations (keys) with the highest values,
$max = array_keys($array, max($array));
I have the following array structure. with the variable $sortie
Array
(
[0] => 480
[1] => 483
[2] => 497
[3] => 481
[4] => 478
[5] => 475
[6] => 476
[7] => 477
[8] => 498
[9] => 496
[10] => 502
[11] => 499
[12] => 494
[13] => 503
[14] => 493
[15] => 500
[16] => 484
[17] => 501
[18] => 495
[19] => 485
[20] => 489
[21] => 490
[22] => 488
[23] => 487
[24] => 486
)
I'm trying to achieve something by assigning them on a new array using the following code.
$release = array();
foreach ($sortie as $key_true => $value_true) {
$release[$key_true] = $value_true;
echo $key_true.'---'.$value_true.'<br>';
}
So far the echo results are going as expected with the correct order based on $sortie
however the $release array is not following the same ordering not assigning the $key_true to the $release array. Would appreciate any help why it's doing this.
EDIT
Sorry almost forgot, result of $release the values are the original key and original value
Array
(
[0] => 0---480
[1] => 1---483
[2] => 10---502
[3] => 11---499
[4] => 12---494
[5] => 13---503
[6] => 14---493
[7] => 15---500
[8] => 16---484
[9] => 17---501
[10] => 18---495
[11] => 19---485
[12] => 2---497
[13] => 20---489
[14] => 21---490
[15] => 22---488
[16] => 23---487
[17] => 24---486
[18] => 3---481
[19] => 4---478
[20] => 5---475
[21] => 6---476
[22] => 7---477
[23] => 8---498
[24] => 9---496
)
The best practice is change array to this structure:
$sortie = [480, 483, ...];
$release = [];
foreach($sortie as $val) {
$release[$val] = true;
}
And then you can use php array sorting functions
Try putting in $release == array();, it may work around for you as far as assigning is concerned
Im trying to get all the 4 digit numbers from this website, However there are some numbers that are less than 4 digits for example #20 in the array is value 20.
Array ( [0] => 6280 [1] => 6279 [2] => 6278 [3] => 6277 [4] => 6276 [5] => 6275 [6] => 6274 [7] => 6273 [8] => 6272 [9] => 6271 [10] => 6270 [11] => 6269 [12] => 6268 [13] => 6267 [14] => 6266 [15] => 6265 [16] => 6264 [17] => 6263 [18] => 6262 [19] => 6261 [20] => 20 [21] => 6320 )
This is the script im using:
$pattern = '#<b>([0-9]+)</b>#';
preg_match_all($pattern,$website,$match_number);
Is it possible to only get it if there are 4 digits no less no more.
Thanks
Change the pattern to:
$pattern = '#<b>([0-9]{4})</b>#';