php add array element to array of arrays - php

I use PHP 5.5, but my working knowledge is very limited - I am only just learning. I cant' get my code to work.
I have an array EP with 20 values. I shuffle these values for every new visitor of the site, so they are always in a random order.
$EP = array(30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220);
$EP = shuffle($EP);
I have an array of other arrays
$P[] = [81,102,74,157,93,106,0,0]
$P[] = [71,102,76,157,93,106,0,0]
$P[] = [91,102,74,7,93,106,0,0]
$P[] = [56,100,89,15,93,106,0,0]
20 in total
What I need is that every element of the EP array is used one time, and then a 0 added at the end, so it looks like this:
$P[] = [81,102,74,157,93,106,0,0,$EP(0),0];]
$P[] = [71,102,76,157,93,106,0,0,$EP(1),0];]
$P[] = [91,102,74,7,93,106,0,0,$EP(2),0];]
$P[] = [56,100,89,15,93,106,0,0,$EP(3),0];]
so that all 20 array elements of EP are used.
My code which worked, until I add the EP array and added EP(1) etc in my other arrays.:
<?php
$Return = array();
$P = array();
$S = array();
$F = array();
$EP = array(30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220);
$EP = shuffle($EP);
//ts1
$P[] = [81,102,74,157,93,106,$EP(0),0];
$S[] = [this series is not important fo rmy question];
//ts2
$P[] = [184,0,0,0,0,0,0,105,$EP(1),0];
$S[] = [..];
//ts3
$P[] = [0,0,0,0,0,$EP(2),0];
$S[] = [..];
//and so on till time series ts20
//for loop below worked until I added the EP series, so it must have something to do with how I call the EP elements from the array.
for($i=0; $i<count($P); $i++)
{
$Return[] = $P[$i];
$Return[] = $S[$i];
$Return[] = $F[$i];
}
die(json_encode($Return));
?>

I see two things going wrong:
shuffle() doesn't return an array. It returns a bool. When you do $a = shuffle($a);, $a will be true or false - not a shuffled array. Do instead: shuffle($a);.
When you want to refer to an index in an array, don't use $a(10). Use $a[10]. See php.net.
After changing that, it should work (tested).

You can push to the array
array_push($P, $EP);
This will add a new array element to $P;

Related

Divide randomly generated ids into equal groups

i'm using php and generating user's id like
$id = bin2hex(openssl_random_pseudo_bytes(16, $s));
And i would like to divide these ids into equals (or almost equals) groups. And in future i would like to know which group user belongs to.
Any ideas? What criteria can i choose for this?
I've found one variant of resolution. But maybe there is another, more pretty way...
$group = array_sum(str_split($id))%2;
OK this is a rough and ready answer; since the actual criteria you wish to sort on aren't given, I'm assuming the main goal is an even distribution of variables amongst your chosen container; I've used arrays here.
<?php
$id1 = 'abc';
$id2 = 'def';
$id3 = 'ghi';
$id4 = 'jk';
$id5 = 'lmn';
$id6 = 'opq';
$id7 = 'rst';
$id8 = 'uvx';
$id_array = array($id1, $id2, $id3, $id4, $id5, $id6, $id7, $id8);
$array1 = array();
$array2 = array();
$array3 = array();
$array4 = array();
$id_storage = array($array1, $array2, $array3, $array4);
$id_storage_size = sizeOf($id_storage);
foreach ($id_array as $indivId) {
$id_array_size = sizeOf($id_array);
$current_storage_array = $id_array_size % $id_storage_size;
$id_storage[$current_storage_array][] = $indivId;
array_shift($id_array);
}
//check them like so...
echo $id_storage[1][1];
?>
As for checking which array contains a given value:
<?php
$givenId = $id2;
foreach ($id_storage as $indiv_storage_array){
if (in_array($givenId, $indiv_storage_array)){
echo "Match found in $indiv_storage_array";
}
}
?>

Given an array of integers, what's the most efficient way to get the number of other integers in the array within n?

Given the following array:
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
And assuming $n = 2, what is the most efficient way to get a count of each value in the array within $n of each value?
For example, 6 has 3 other values within $n: 5,7,7.
Ultimately I'd like a corresponding array with simply the counts within $n, like so:
// 0,0,1,2,2,5,6,7,7,9,10,10 // $arr, so you can see it lined up
$count_arr = array(4,4,4,4,4,3,3,4,4,4, 2, 2);
Is a simple foreach loop the way to go? CodePad Link
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
$n = 2;
$count_arr = array();
foreach ($arr as $v) {
$range = range(($v-$n),($v+$n)); // simple range between lower and upper bound
$count = count(array_intersect($arr,$range)); // count intersect array
$count_arr[] = $count-1; // subtract 1 so you don't count itself
}
print_r($arr);
print_r($count_arr);
My last answer was written without fully groking the problem...
Try sorting the array, before processing it, and leverage that when you run through it. This has a better runtime complexity.
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
asort($arr);
$n = 2;
$cnt = count($arr);
$counts = array_pad(array(), $cnt, 0);
for ($x=0; $x<$cnt; $x++) {
$low = $x - 1;
$lower_range_bound = $arr[$x]-$n;
while($low >= 0 && ($arr[$low] >= $lower_range_bound)) {
$counts[$x]++;
$low--;
}
$high = $x + 1;
$upper_range_bound = $arr[$x]+$n;
while($high < $cnt && $arr[$high] <= $upper_range_bound) {
$counts[$x]++;
$high++;
}
}
print_r($arr);
print_r($counts);
Play with it here: http://codepad.org/JXlZNCxW

Creating a ranked list from multiple arrays

I have 3 arrays that return a url,title,snippet and score from 3 different search engines, the score starts at 100 for the element in the array, the second 99 and so on, I'm trying to combine all 3 into one array. If the urls match from the different arrays I want to add the scores together and then delete the duplicate url. If there is no match between the urls then I just want to put this element into the combined array.
The final combined list should contain all distinct urls with its score,title and snippet, here are my array structures
googleArray
$x=0;
$score=100;
foreach ($js->items as $item)
{
$googleArray[$x]['link'] = ($item->{'link'});
$googleArray[$x]['title'] = ($item->{'title'});
$googleArray[$x]['snippet'] = ($item->{'snippet'});
$googleArray[$x]['score'] = $score--;
$x++;
}
blekkoArray
$score = 100;
foreach ($js->RESULT as $item)
{
$blekkoArray[$i]['url'] = ($item->{'url'});
$blekkoArray[$i]['title'] = ($item->{'url_title'});
$blekkoArray[$i]['snippet'] = ($item->{'snippet'});
$blekkoArray[$i]['score'] = $score--; // assign the $score value here
$i++;
}
bingArray
foreach($jsonObj->d->results as $value)
{ $i = 0;
$bingArray[]['Url'] = ($value->{'Url'});
$bingArray[]['Title'] = ($value->{'Title'});
$bingArray[]['Description'] = ($value->{'Description'});
$bingArray[]['score'] = $score--;
$i++;
}
Any help would be great, thanks in advance
This solution depends on a couple of things to work. First, the url and score keys need to be the same, i.e. all lower case and none that are "link." Secondly, the URLs have to be normalized, because they serve as the key for the array. If there are any differences in the URLs, they will show up more than once in the final array.
$merged = array_merge($googleArray, $blekkoArray);
$merged = array_merge($merged, $bingArray);
$combined = array();
foreach ($merged as $key => $value){
$score = (isset($combined[$value['url']]['score'])) ? $value['score'] + $combined[$value['url']]['score'] : $value['score'];
$combined[$value['url']] = $value;
$combined[$value['url']]['score'] = $score;
}
If you don't want to keep the URLs as the key, add this line:
$combined = array_values($combined);
If you want to sort the array by score, you can use usort:
usort($combined, function ($a, $b){
return $b['score'] - $a['score'];
});
print_r($combined);

Nested loops and array formation

Suppose that I start with an array that looks like:
$array_1 = array(array(1,2,3), array(2,4,5), array(3,6,7));
For simplicity, assume that I have a rule that says: delete the first subarray and then delete the first elements of the remaining subarrays. This would yield the result:
$new_array = array(array(4,5), array(6,7))
Then assume I expand the problem to larger arrays like:
$array_2 = array(array(1,2,3,4), array(2,3,4,5), array(3,4,5,6), array(4,5,6,7));
I have the same rule here - delete first subarray and then delete first elements of the remaining subarrays. BUT this rule must be continued until the smallest subarray contains only two elements (as in the first example). So that in stage one of the process, my new array would look like:
$new_array_s1 = array(array(3,4,5), array(4,5,6), array(5,6,7));
But in the final stage, the completed array would look like:
$new_array_s2 = array(array(5,6), array(6,7));
For context, here is my code for the $array_1 example:
<?php
$array_1 = array(array(1,2,3), array(2,4,5), array(3,6,7));
$array_shell = $array_1;
unset($array_shell[0]);
$array_size = count($array_shell);
$i = 0;
$cofactor = array();
while($i < $array_size) {
$el_part_[$i] = $array_1[$i];
unset($el_part_[$i][0]);
$el_part_[$i] = array_values($el_part_[$i]);
array_push($cofactor, $el_part_[$i]);
++$i;
}
echo '<pre>',print_r($cofactor,1),'</pre>';
?>
My Question: How can I generalise this code to work for N sized arrays?
You don't need a complicated code .. Just loop and use array_shift
Example:
print_r(cleanUp($array_1));
Function
function cleanUp($array) {
array_shift($array);
foreach($array as $k => $var) {
is_array($var) && array_shift($array[$k]);
}
return $array;
}
See Live DEMO
$num = count($array_1);
for($i=0;$i<=$num;$i++)
{
if($i==0)
unset($array_1[$i]);
else unset($array_1[$i][0]);
}
Building off of Baba's answer, to work with N element arrays (assuming each array contains the same number of elements):
<?php
$array_1 = array(array(1,2,3,4), array(2,4,5,6), array(3,6,7,8));
$array = $array_1;
while(count($array[0]) > 2)
$array = cleanUp($array);
print_r($array);
function cleanUp($array) {
array_shift($array);
foreach($array as $k => $var) {
is_array($var) && array_shift($array[$k]);
}
return $array;
}
This will keep reducing until the sub-arrays have only 2 elements.
-Ken

Slice an array into 4 other arrays

I have an array which I want to slice in 4 other arrays because I want to display the content of the first array on four columns.
I have tried the code above, but what I get is N columns with 4 items.
$groups = array();
for ($i = 0; $i < count($menu); $i += 4) $groups[] = array_slice($menu, $i, 4);
Can this be modified in order to get exactly 4 columns and distribute the values so they fit?
Like Michael Berkowski suggested:
$groups = array_chunk($menu,4);
Should give you what you need. If you're more into "manual labour":
$groups = array();
while($groups[] = array_splice($menu,0,4))
{//no need for any code here ^^ chunks the array just fine
printf('This loop will run another %d times<br/>',(int)ceil(count($menu)/4));
}
Update:
I see I got this a bit wrong... want to chunk into 4 arrays, not into arrays of four:
$groups = array_chunk($menu,(int)ceil(count($menu)/4));
You can try
// Some Random array
$array = range(1, 20);
// Split it 4 Chuncks
$array = array_chunk($array, 4);
// Slice The first 4 Chunks
$array = array_slice($array, 0, 4);
// Output Result
foreach ( $array as $set ) {
printf("<li>%s</li>", implode(",", $set));
}

Categories