array_combine(); not producing values properly - php

EDIT: Alright, my original post was kinda confusing, I've also tried something new. Here's my code:
if($_POST['submit']=='Save'){
$_SESSION['quantity'] = $_POST['quantity'];
$_SESSION['order'] = array_combine($_SESSION['parts-order'], $_SESSION['quantity']);
header("Location: shopping.php");
exit;
}
Both $_SESSION arrays are 430 long, both containing strings. When I print_r() on both $_SESSION arrays separately, they return their expected values. Basically, when I combine the arrays, I expect this output:
Array
(
[1 inch ratchet tie down] => 13
[24" White Ring Buoy w/ Reflective Tape] => 4
[50' Throw Line w/bag] => 5
[Classic Shirt Large] => 0
...
)
Y'know, or whatever values are in $_SESSION['quantity']. I, instead, get this:
Array
(
[1 inch ratchet tie down] => 0
[24" White Ring Buoy w/ Reflective Tape] => 0
[50' Throw Line w/bag] => 0
[Classic Shirt Large] => 0
...
)
Unless each of the $_SESSION['quantity'] values is the same, it produces 0. How do the values get changed to 0 in the process of array_combine?
Thanks.

Related

PHP array has the same index for every element

I'm working on a program that prints the qualities of every individual in a team.
I have 3 tables:
The Teams table:
TeamID
1
2
The TeamPlayers table :
PlayerID
Team
100
1
269
1
357
2
The program works like this: There is a textbox in the HTML page where the user is typing the ID of the team and the program outputs the members. The problem is that each player is stored in an array. and all have the same index:
$fetchPlayersSQL = "SELECT Player_ID
FROM TeamPlayers
WHERE Team = $TeamID;";
$fetchPlayers = ibase_query($dbConnection, $fetchPlayersSQL);
while ($row2 = ibase_fetch_object($fetchPlayers)) {
$fetchPlayersArray = get_object_vars($row2);
print_r($fetchPlayersArray);
}
Keep in mind $TeamID is the value introduced by the user in the HTML textbox.
Now, this program outputs this:
Array ( [Player_ID] => 2157 )
Array ( [Player_ID] => 734 )
Array ( [Player_ID] => 2160 )
Array ( [Player_ID] => 3744 )
Array ( [Player_ID] => 2166 )
(Keep in mind, 2157, 734, 2160, 3744 and 2166 are other players, there are a lot of them, but I listed only a few)
The problem is that I need every player to have their own index in the array, because I have to print their qualities
I really can't find where the problem comes from. Maybe I am using an incorrect method to select since there are more players in a team.
The 3rd table is just the qualities of every player
PlayerID
Height
Weight
HairColor
100
187
80
black
357
167
67
grey
269
182
95
brown
And the expected output is something like this:
The user enters 1 in the HTML textbox, Team 1 players are 100 and 269 so they should see:
Array ( [0] => 2157 )
Array ( [1] => 734 )
echo $row2->Player_ID; just prints their IDs, we have player 100 and 269 in the Team 1, and this prints 100269
It might not be the most elegant solution, but you can always build that array yourself in a loop:
$a = array();
while ($row2 = ibase_fetch_object($fetchPlayers)) {
$a[] = $row2->Player_ID;
}
print_r($a);
See if that works for you, I am not able to code at the moment.

Is this normal behaviour in php arrays? Array size get shortened when using numbered indexes out of order

So I'm learning Php, so as I was messing around with arrays to see how they work, I stumbled into this when I made two arrays.
$TestArray1 = array( 1 => 1, "string" => "string", 24, "other", 2 => 6, 8);
$TestArray2 = array( 6 => 1, "string" => "string", 24, "other", 1 => 6, 8);
But when I print them out with print_r() this is what I get (this also happens with var_dump by the way)
Array ( [1] => 1 [string] => string [2] => 6 [3] => other [4] => 8 )
Array ( [6] => 1 [string] => string [7] => 24 [8] => other [1] => 6 [9] => 8 )
As far as I can tell, by putting the two in the second array it overwrites the next possible spot with no key and then keeps going, shortening the array. So I thought that meant that if I use a 1 it would put it at the start but that does not happen either.
Is this normal or is there something wrong with my php installation?
Im using Ampps in windows 10 with php 7.3.
Thanks in advance
Good question.
What's happening is that when determining automatic numeric indexes, PHP will look to the largest numeric index added and increment it (or use 0 if there are none).
The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.
What's happening with your first array is that as it is evaluated left-to-right, 24 is inserted at index 2 because the last numeric index was 1 => 1.
Then when it gets to 2 => 6, it overwrites the previous value at index 2. This is why 24 is missing from your first array.
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Here's a breakdown
$TestArray1 = [1 => 6]; // Array( [1] => 6 )
// no index, so use last numeric + 1
$TestArray1[] = 24; // Array( [1] => 6, [2] => 24 )
$TestArray1[2] = 6; // Array( [1] => 6, [2] => 6 )
When you manually add numeric indexes that are lower than previous ones (ie $TestArray2), they will be added as provided but their position will be later.
This is because PHP arrays are really maps that just pretend to be indexed arrays sometimes, depending on what's in them.
References are from the PHP manual page for Arrays

Looping : PhP Array Loops through Database query to output result

I have an array called $helloArray which looks like
[peach] => 1
[banana] => 1
[apple] => 1
[pineapple] => 1
[grapefruit] => 2
[tomatoe] => 2
[giger] => 1
[watermelon] => 1
Database columns look like
City peach banana apple pineapple grapfruit tomatoe ginger watermelon
Tokyo 0 0 0 500 0 0 0 0
DC 50 55 0 0 0 0 0 0
NY 0 0 0 0 0 500 0 0
Rome 0 0 0 0 90 0 0 0
SQL Statment that I used
$sql = "SELECT peach,banana,apple,pineapple,grapfruit,tomatoe,ginger,watermelon";
$sql .= " FROM TestTable2";
$sql .= " WHERE city ='NY'";
Question:
How do I loop through the array and then the variable names (aka column names) names so that
so that we get NY and Rome values of 2 and 1 for the rest. Below is my code that I have tried, not to mention it not working
foreach ($helloArray as $key =>$value){
for($i=0;$i<=odbc_num_fields($connection);$i++)
{ if (odbc_result($connection,$i) > 0) {
echo $value; }
}
}
OKay, not sure if I completely understand your question without an output sample, but I'm guessing you want:
Tokyo => 1
DC => 1
NY => 2
Rome => 2
If so, your approach is wrong. Don't pull data and then massage it, construct your SQL to return the data you want.
SELECT City, CASE WHEN grapefruit > 0 THEN 2 WHEN tomatoe > 0 THEN 2 WHEN peach > 0 THEN 1 WHEN banana > 0 THEN 1 WHEN ...
If it has to be data driven then sort your hello array by descending values and then use it to generate the SQL case conditions and return vals
-- edit --
Okay, but the code sample above, you are looping through your array as key=>value pairs. So first pass, you're on peach=>1, then you loop through every column in the return result (peach, watermelon, etc) and check it's value is greater than 0 and if it is then you output value from your array. (peach = 1)
Because there is no comparison between the fieldname and the key of the array, you are simply going to output 1 for each column while on peach, 1 for each column while on banana, etc. While I am assuming you are wanting to check if the field NAMED peach is greater than zero and if so, then output 1, and if the field named watermelon is greater than zero then output 2, you need to modify your loop. Drop the odbc_num_field and use the odbc_result syntax to specify the column name you want to retrieve. odbc_result($connection, $key) > 0.
But going by your original question it sounds like that isn't really what you are looking for, but instead want a city assigned a certain value based on the largest number returned by those field comparisons described above (NY = 2) in which case my original answer stands and you just need to use SQL to return that value for each city.

PHP - Certain array indexes not accepting variables

I have a bizarre issue that I cannot for the life of me seem to resolve.
I am generating an array ($tags) from a mysql query, it looks something like this:
Array(
[1] => Safety Toe
[2] => Waterproof
)
Then I have another array ($link) I generate in a loop:
Array(
[1] => Array(
[0] => 1
[1] => 2
)
[2] => Array(
[0] => 1
[1] => 2
)
)
Also, I have 2 predefined variables that in this case are as follows:
$max == 2;
$title_count == 3;
Later on I have 2 for loops, 1 is nested:
for($y=0;$y<$max;$y++){
for($x=1;$x<=$title_count;$x++){
if($x==1){
echo "<tr><td>".$tags[$link[$x][$y]]."</td>";
}elseif($x<$title_count){
echo "<td>".$tags[$link[$x][$y]]."</td>";
}else{
echo "<td>".$tags[$link[$x][$y]]."</td></tr>";
}
}
}
This should produce something along the lines of:
Safety Toe Safety Toe Safety Toe
Waterproof Waterproof Waterproof
The problem is this is what I get:
Safety Toe Safety Toe Safety Toe
This made me curious, so I tried manually inputting $tags[2]. That worked and produced:
Waterproof Waterproof Waterproof
Waterproof Waterproof Waterproof
However, if I manually set them all to $tags[$link[1][1]] ($link[1][1] == 2) I get an empty result. If I set a variable, such as $test = $link[1][1]; (which echoes as 2), and then try $tags[$test], I get nothing. However if I set $test = 2; and do $tags[$test] I get Waterproof.
I am beyond bewildered here, if there is anything I'm missing, or any ideas as to why this would be the way it is, please let me know.
Thanks!
I figured out my problem, and it would not have been determinable from what I posted.
I tested
$tags[intval($links[1][1])]
and it worked.The $links array is being generated by exploding a string (1, 2). I was axploding on "," not ", " so the value of the second entry was " 2" instead of "2", hence the intval.
The string needs to be adjusted to "1,2" or the explode needs to be adjusted to ", ". Either way fixes the problem.

Get the real difference between two arrays in php

I'm trying to get the difference between two arrays, but with array_diff, array_diff_assoc, or array_diff_key I can't get what I want..
Array 1 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 421001,
5 => 421002,
Array 2 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 431447,
5 => 421001,
6 => 421002,
array_diff = array ()
// empty
jarray_diff_assoc = array (
4 => 431447,
5 => 421001,
6 => 421002,
)
// OK but too much :)
array_diff_key = array(
6 => 421002
)
// nope i don't want that :(
I want 431447, cause it's only one time in the first array and twice in the second.
Regards, Tony
Is that exactly what you want? Only those that occur one time in the first, and two times in the second?
You can basically write your own function for that. Search through the second array, get a list of values that occur two times (or more than once, depending on what it is that you actually want), and then search for those in the first one (this you can do using a built-in PHP function array_intersect).

Categories