PHP Array_intersect enters undefined offset - php

I got an array working like this:
$listaMaterias[x]['id_materia'] = (value with number and letters random)
$listaMaterias[x]['name_materia'] = (string)
$listaEditoriales[x]['id_editorial'] = (value with n. and l. random)
$listaEditoriales[x]['name_editorial'] = (string)
A 'materia' is a book's category. I made a foreach where I get all values from an xml right. Many editorials and materias, where some of them comes repeated.
And then, I make a method with an array_intersect to make remove repeated values, but I get an error :
$listaEdits_result = array(); // final results
$listaMats_result = array();
$listaEds_first_res = $listaEditoriales[0];
for ($j = 1 ; $j < count($listaEditoriales) ; $j++ ){
$listaEdits_result = array_intersect($listaEds_first_res, $listaEditoriales[$j]);
$listaEds_first_res = $listaEdits_result;
}
$listaMts_first_res = $listaMaterias[0];
for ($k = 1 ; $k < count($listaMaterias) ; $k++ ){
// Line 285, is this one above
$listaMats_result = array_intersect($listaMts_first_res, $listaMaterias[$j]);
$listaMts_first_res = $listaMats_result;
}
And finally, I get this error :
Notice: Undefined offset: 20 in [URL]/menu-librosnormales.php on line 285
Warning: array_intersect(): Argument #2 is not an array in [URL]/menu-librosnormales.php on line 285
Why access offset 20 if before I count this quantity in every array :
count($listaEditoriales) : 20
count($listaMaterias) : 14

Instead of $listaMaterias[$j] do $listaMaterias[$k] in second loop below line:-
$listaMats_result = array_intersect($listaMts_first_res, $listaMaterias[$j]);
Note:- If your aim is to remove duplicates from an array then you can use array_unique() easily.

In the second loop you use $listaMaterias[$j] but the loop is indexed by $k, not by $j.
The value of $j is count($listaEditoriales) because this was the last value of $j when the first loop ended. Since $listMaterias contains only 14 items, trying to access its 21st item triggers the notice you described.
If the purpose of each loop is to compute the intersection of the arrays stored in $listaEditoriales (and $listaMaterias) then you can do with a single call to array_intersect() using arguments unpacking (the so-called "splat operator"):
$listaEds_first_res = array_intersect(...$listaEditoriales);
$listaMts_first_res = array_intersect(...$listaMaterias);
The arguments unpacking operator is available since PHP 5.6. If you need to run the code on older PHP versions then you can use call_user_func_array() instead:
$listaEds_first_res = call_user_func_array('array_intersect', $listaEditoriales);
$listaMts_first_res = call_user_func_array('array_intersect', $listaMaterias);
The two lines of code above do the same thing as the entire block of code you posted in the question (faster and without errors).

Related

How to loop though an array and create an array, in the loop, containing values from the original array?

Hey everyone I have a question about arrays and loops in PHP.
For a game I'm making, I need to write a function that generates a stack of crystal_id's based on a given size and ratio.
The ratio is the ratio between black crystals and different colored crystal (so a ratio of 0,25 (1:4) and a stack of 50 would yield a stack with 40 black crystals and 10 colored crystals).
I have all the math to calculate the amount of crystals per color and stuff figured out, but I can't figure out how to create an array with the right amount of colored crystals where each color is represented equally.
For reference, the array the code gets to choose from is a variable called $crystals_array, which is an array filled with integers where each integer represents a different colored crystal (e.g. [2,3,4,5,6]).
In the above case we have 5 different colored crystals and we needed a total of 10 colored crystals where each crystal is represented equally. So I need to create an array that looks a little something like this:
[2,2,3,3,4,4,5,5,6,6].
The code I have so far is this:
for($i = 0; $i <= count($amount_crystals_color) - 1; $i++)
{
$array = array_fill(0, $amount_crystals_per_color_stack, $crystals_array[$i]);
$i++;
}
Using the above example $amount_crystals_per_color_stack is equal to 2 and amount_crystals_color is equal to 5.
When executing this code it outputs an array: [2,2] which is how many 2's we need, but I can't figure out how to add the remaining crystals to this array.
Can anyone help me out?
Your code has several problems and i will address each of them individually.
Using the for loop to iterate over an array
You are using a for loop in your code, that has the following loop head:
for($i = 0; $i <= count($crystals_array) - 1; $i++)
The loop had consists of three parts, each separated by a semicolon (;).
The first part $i = 0 is the initialization part. Here you initialize a variable, that is later used as an iterator, which shall change in each loop iteration. You could name this the start point as well.
The second part $i <= count($crystals_array) - 1 is the condition part. A condition is formed, that shall express for how long the loop shall iterate. As long as the expression evaluates as true, the loop will run again. Therefore this condition is evaluated at the start of each iteration. As soon as the condition evaluates as false the loop will end. Therefore this part can be named endpoint as well.
The third part $i++ is the step size. This is executed at the end of each iteration and determines how the iterator (the variable you defined in the first part) shall change. $i++ is in this context equal to $i = $i + 1 and represents a step size of 1. Therefore the variable $i gets increased by 1 for each run of the loop.
This said, you can improve and fix your code regarding the for loop with two changes:
Save functions, that are executed in your condition part into an variable, if they return a constant result for each iteration. You use the count() function, which will then count your array again for each iteration of the for loop. By saving it in a variable $count = count($crystals_array); before the for loop and changing the condition to $i < $count, the function is only called once and your code gets faster.
Do not change the iterator variable $i outside of your loop header. This is really bad code style. You added the line $i++; to the end of your loop, but that is already done in the step size part of the for header. Because that is executed at the and of each iteration as well you increased the step size to two, meaning that you only run the for loop with $i = 0, $i = 2 and $i = 4 instead of for each element.
For your code the usage of the $i iterator is only to address the elements of the initial array. Even though you should understand the for loop for the future, you should use a foreach loop for this case instead. The following code would be equivalent to your for loop.
//This code still contains another major bug and is jsut a partial improvvement
foreach($crystals_array as $crystal) {
$array = array_fill(0, $amount_crystals_per_color_stack, $crystal);
}
As you can see, you neither need to worry about counting the initial array, nor in which index the current value is. Instead the variable $crystal will automatically contain the next element for each iteration.
Appending elements to an array
You used the following line to save the newly generated elements in your array:
$array = array_fill(0, $amount_crystals_per_color_stack, $crystal);
If you look closely, you use a standard assignment with $array = at the beginning of your line. This means, that (like with each variable assignment) the previous value of the variable gets overwritten by the new value provided from the right side of the assignment. What you do not want yet is to overwrite the array, but to append something to it.
This can be done by adding two squared brackets to the end of the variable name: $array[] = .... Now, if the variable $array is really an array, what ever value is on the right side of the assignment will be appended to the array, instead of overwriting it.
Managing result types the right way
The following line still contains a major problem:
$array[] = array_fill(0, $amount_crystals_per_color_stack, $crystal);
The result type of array_fill() is an array itself. By appending it to the previous array, you would get the following structure:
$array = [
[2, 2],
[3, 3],
[4, 4],
[5, 5],
[6, 6],
];
As you can see, the code did exactly what it should but not what you wanted. Each result (array) was appended to the array. The result is therefore an array or arrays (or a multidimensional array). What you want instead, is that the values of the result are appended to the existing array.
PHP offers a function for that, named array_merge(). This function takes all elements for one (or more) array(s) and appends them to the end of the first array, that was given to the function. You can use it as followed:
$newCrystals = array_fill(0, $amount_crystals_per_color_stack, $crystal);
$array = array_merge($array, $newCrystals);
As you can see the latter line contains a normal assignment again. ($array =) This is because array_merge() does not modify the first array given to it, but creates a new array with the merged fields. Therefore the new array contains all values from the old one and it is safe to overwrite the old one with it.
The complete code would therefore be:
$array = [];
foreach($crystals_array as $crystal) {
$newCrystals = array_fill(0, $amount_crystals_per_color_stack, $crystal);
$array = array_merge($array, $newCrystals);
}
As I understood the problem
$crystals_array = [2,3,4,5,6];
$amount_crystals_per_color_stack = 2;
$res = [];
foreach($crystals_array as $v) {
// repeat each item from array $amount_crystals_per_color_stack times
$res = array_merge($res, array_fill(0, $amount_crystals_per_color_stack, $v));
}
print_r($res); // [2,2,3,3,4,4,5,5,6,6]
You need to merge your array at each iteration of the loop (repl online) or you lose the result each time.
Like:
$array = array();
for($i = 0; $i < count($amount_crystals_color); $i++)
{
$array = array_merge($array, array_fill(0, $amount_crystals_per_color_stack, $crystals_array[$i]);
}
Also you don't need the $i++ in the loop, because it iterate twice otherwise, and you don't need count(..)-1 if the condition is < instead of <=.
You could use simple foreach() to achieve this-
$amount_crystals_per_color_stack = 2;
$array = [2,3,4,5,6];
$result = array();
foreach($array as $a){
for($i=1;$i<=$amount_crystals_per_color_stack;$i++){
array_push($result, $a);
}
}
print_r($result);

Copy or Append first 13 index in Array of Strings to another and sort it - php

I am trying to work with array of string in php, its array of images and shuffle the array many times and i need to get the first 13 value from the huge array and place it in another array and then sort the second array of strings. but i am getting two errors :
Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 46
Warning: ksort() expects parameter 1 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs//index.php on line 50
Array30.png41.png24.png31.png25.png44.png2.png15.png14.png50.png36.png38.png32.png
here is my code :
$images = array("1.png","2.png","3.png","4.png","5.png","6.png","7.png","8.png",
"9.png","10.png","11.png","12.png","13.png","14.png","15.png","16.png","17.png","18.png",
"19.png","20.png","21.png","22.png","23.png","24.png","25.png","26.png","27.png","28.png",
"29.png","30.png","31.png","32.png","33.png","34.png","35.png","36.png","37.png","38.png",
"39.png","40.png","41.png","42.png","43.png","44.png","45.png","46.png","47.png","48.png",
"49.png","50.png","51.png","52.png"
);
shuffle($images);
shuffle($images);
shuffle($images);
shuffle($images);
shuffle($images);
$playerArraySorted = array();
for ($i = 0; $i < 13; $i++) {
$playerArraySorted .= $images[$i];
}
ksort($playerArraySorted,2);
echo "$playerArraySorted";
In order to push to an array in php, use brackets:
$playerArraySorted[] = $images[$i];
With .= you concatenate strings.
And instead of echo in order to check the array, use var_dump:
var_dump($playerArraySorted);
And it seems that your array has no keys, so ksort wont work, use sort or usort with a custom sorting function instead.

Undefined offset: 1 For Loop PHP

The array it is fetching has size 2 , so if we comment out $size it returns 2 , but the for loop prints only the value present at 0th position of table column and not the next at 2th giving the error:
Undefined offset: 1
Code:
$allunstitched="Select p_id from unstitchedproduct";
$resultAllUnstitched= mysqli_query($connection,$allunstitched);
$AllUnstitchedResult= mysqli_fetch_array($resultAllUnstitched);
//$size=count($AllUnstitchedResult);
for ($i=0, $count = count($AllUnstitchedResult); $i < $count; ++$i) {
print $AllUnstitchedResult[$i];
}
You're using mysqli_fetch_array. This function returns the result twice by default: indexed both by number and by column name. Here the result set has only one column, so you get its value twice in the array: first with index 0 and then with a string index. There is no index 1.
To get the results with numerical indexes only, pass MYSQLI_NUM as a second argument to mysqli_fetch_array:
mysqli_fetch_array($resultAllUnstitched, MYSQLI_NUM);
Um, i dont think you have a problem in your for-loop; since your query reads ONE field/column it should have 1 iteration (=0 index), so the =1 undefined is correct (in my opinion).
What you should do is a loop that calls repeatedly mysqli_fetch_array to get all rows, like this:
while($AllUnstitchedResult = mysqli_fetch_array($resultAllUnstitched,MYSQLI_NUM)) {
foreach ($AllUnstitchedResult as $field) {
print $field;
}
}
If you start at 0, if count = 5 you'll have 6 elements (0,1,2,3,4,5), beware of that. You need to loop (count-1) times:
$count = count($AllUnstitchedResult);
for ($i=0, $i < $count-1; $i++) {
print $AllUnstitchedResult[$i];
}

Undefined offset in 1 Error

i'm trying to exctract array with this code, but it gives that error. But if i remove while block from code and only give indice it works. Here is the code.
//This function gives error: Notice: Undefined offset: 1 in .......
//but if i delete while block and only write print $type[$i]; it works.
public function checkMimeType(){
echo '<pre>';
$i = 0;
$type = array();
foreach($this->_sourceFile as $key){
$type= $key['type'];
}
while($i <= count($type))
{
print $type[$i].'<br>';
$i++;
}
}
YOu're looping one time to often ;)
Index numbers start at zero. If there's one element in your array, the only index that is defined is therefore 0. count() will return 1.
If you loop 'till $i<= 1, it will stop at $i = 1. There is no element with the ID of 1.
So, instead, use while($i < count($type))
The count is not equal to the last index.
An array [x, y, z] has a count of 3, but the last index is 2.
So, in your while loop, you are not allowed to run until <= count, but only < count. When $ibecomes count the index is out of bounds already.
In your first loop you are not adding values to an array, you are overwriting the $type variable each time. Try this:
$type[] = $key['type'];
Edit: And also what #thst said

PHP Undefined offset error repeated

I'm trying to run a PHP script that finds all the numbers divisible by 3 or 5, dumps them into an array, and adds all the values together. However, When I try to run it, I get a number output (I don't know if it's correct or not) and several hundred errors. They start out with:
Notice: Undefined offset: 1 in G:\Computer Stuff\WampServer\wamp\www\findthreesandfives.php on line 18
Then the offset number increases by increments of 1-3 (randomly, I haven't seen a pattern yet). I can't figure out what's wrong. Here's my code:
<?php
function loop($x)
{
$a = array(); //array of values divisible by 3 or 5
$l = 0; //length of the array
$e = 0; //sum of all the values in the array
for ($i=0; $i<=$x; $i++){ //this for loop creates the array
$n3=$i%3;
$n5=$i%5;
if($n3 === 0 || $n5 === 0){
$a[$i]=$i;
$l++;
}
}
for ($v=0; $v<=$l; $v++){ //this loop adds each value of the array to the total value
$e=$e + $a[$v];
}
return $e;
}
echo loop(1000);
?>
Someone please help...
The problem in your code is the following line:
$a[$i]=$i;
Should be:
$a[count($a)] = $i;
This is because the value of $i is always increasing, so using $i as your pointer will create gaps in the array's indices. count($a) returns the total number of items in the given array, which also happens to be the next index.
EDIT: #pebbl suggested using $a[] = $i; as a simpler alternative providing the same functionality.
EDIT 2: Solving the subsequent problem the OP described in the comments:
The problem seems to be that $l is greater than the number of items in $a. Thus, using count($a) in the for loop should fix your subsequent error.
Try replacing:
for ($v=0; $v<=$l; $v++){
With:
for ($v=0; $v<=count($a); $v++){
I found the same problem as #zsnow said. There are gaps within $a. The if condition allowed the gaps making the assignment skip some indexes. You can also use this
foreach ($a as $v){ //this loop adds each value of the array to the total value
$e=$e + $a[$v];
}
should actually be
foreach ($a as $v){ //this loop adds each value of the array to the total value
$e=$e + $v;
}

Categories