array_push to non-associative array? - php

The code below generates an associative array (key=>value), how can I have a non-associative array (e.g. just keys)? e.g. array('1','2','3','4','5');
$join_have = array();
$join_need = array();
array_push($join_have,$row2['id']);
array_push($join_need,$row3['id']);
I'm trying to construct two arrays to compare the database autoincrement id's for a JOIN table. The JOIN table is one of three (it allows unlimited number of associations instead of statically adding columns in the tables).
The point is to determine what JOIN table id's I have to keep, what I will DELETE and what I will INSERT for id's.

Arrays in php always have keys and values since it's very definition is an ordered map.
For example
$array = array(1, 2, 3, 4, 5);
Would have the following key, value pairs.
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
I think what you're asking is how do you set the keys in an array instead of the value.
You would simply do.
$join_have = array();
$join_have [ $row2['id'] ] = '';
This would still give you a key, value pair but you would be setting the key.

Related

How to combine two arrays without overwriting the keys

I am having two arrays with the same key . How can I combine those two arrays without overwriting the keys of first array.
I tried using array_merge() function , + operation
These are the two arrays:
Array ( [1] => 1 ) Array ( [1] => 0 )
I want the output to be like
array( [1] => 1 , [1] => 0)
You can't have duplicate keys in an array.
What you can do is build an array of object with different keys: 0,1,2,3,4,etc. but with sub objects which will make access to the same object property.
// for istance in this case you end up having an array
// with 5 objects with a property named "key" with a value
$arrayOfObjects = [];
$arrayOfObjects[] = (object)["key"=>"value0"];
$arrayOfObjects[] = (object)["key"=>"value1"];
$arrayOfObjects[] = (object)["key"=>"value2"];
$arrayOfObjects[] = (object)["key"=>"value3"];
$arrayOfObjects[] = (object)["key"=>"value4"];
// to return the value $arrayOfObjects[4]->key

Using values in an PHP array

I have an array split in half, but now i want to use the values in the array.
The array is split like this
$teams = array_chunk($lista, count($lista)/2);
Output
Array ( [0] => Array ( [0] => 4 [1] => 3 ) [1] => Array ( [0] => 4 [1] => 4 ) )
the numbers represent user id's.
How can i use these numbers?
Let's say that i want to select all id's in the first array (The idea is for these two arrays to act like teams) and then assign these values to a team column in my database.
Let me explain a bit more
I want the id's in the array to be assigned to a team.
In array one the team will be, uhm. Blue! and in the other array the team will be red.
I will be using these randomised arrays to update my database table which contains a column called "team".
Basically my question is how i can use these arrays to assign the specific id's in each array to a team. example, Can i select everything in array one and update all those values to team blue?
For this question i'm sorry because i really don't know what i should include to make this question answerable, it might already be! but i'm not sure, so i if i have missed something just comment and i will clarify.
Update
This is what my database table looks like
https://gyazo.com/ed3c681575b26f31b77246436b43439a
As you can see, i have a column called "team", i want to update this to something depending on what team the user got selected into using the array splitter.
Everyone in Array one will have this updated to "team blue", and team red for array2.
You could use a For Loop to construct a query that does this for you.
An array inside of an array is called a Multidimensional Array.
You can access the elements of an array inside an array by just simply indexing twice:
$array = [ [1,2], [3,4] ];
echo $array[0][0]; // echo's 1
So you could loop over the first array for each team and loop into the second array to get each user id:
$array = [ [1,2], [3,4] ];
foreach($array as $team_id => $player_ids){
foreach($player_ids as $player_id){
echo "Player $player_id is in team $team_id";
}
}
This example uses a Foreach Loop.
In this for loop you can construct a query that updates the rows for the players in the database.
If you are new to arrays in PHP I suggest you read this article: http://php.net/manual/en/language.types.array.php

PHP Array of arrays made unique

I have an array of arrays as following:
$a = array(
1 => array("sport", "geo"),
2 => array("sport", "geo", "history"),
3 => array("geo", "history"),
4 => array("golf", "sport"),
...
);
From that I need to get keys, in such a way so that values are unique.
So from that I would need to get something like:
$b = array( 1, 3, 4 );
$a[2] would be cut out, since it has the same values as $a[1], but since $a[2] is not there, $a[3] is fine.
If some values get completely cut out, that's fine. I will have 30+ keys, from which I need to get 10, which have unique values.
Key is a question ID, and values are tags.
I want to get 10 questions, which are different from each other (so that I don't get 10 questions about Sport).
I tried array_unique(), but that just returns this:
Array (
[1] => Array (
[0] => sport
[1] => geo
)
)
which doesn't seem to help much.
Can you guys point me towards something that could help me?
I guess I could try to list all possible categories, make that array unique, sort it by random. I would need to preserve Keys, but Keys are unique...
you can use array_diff() to detect unique tags
Returns an array containing all the entries from array1 that are not present in any of the other arrays.
Then use array_merge() to store unique value to our $tags variable
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.
<?php
$a = array(
1 => array("sport", "geo"),
2 => array("sport", "geo", "history"),
3 => array("geo", "history"),
4 => array("golf", "sport")
);
$tags = [];
foreach($a as $tag){
$tags = array_merge($tags, array_diff( $tag, $tags));
}
print_r($tags);
Output :
Array
(
[0] => sport
[1] => geo
[2] => history
[3] => golf
)
Just iterate through the initial array of questions, every time save the value (array of tags) to another temporary array with checking if actual tags already exists in temporary array - if not add the question to temporary array, if exists go next. Do it until you have 10 questions in your temporary array, if you finish the question array without already having 10 questions - repeat the iteration but this time add other questions even if the tags are repeating - until you have 10.

Compare elements in multidimensional array

I have a multidimensional array where one of the elements looks like the following:
Array
(
[9-0048-1:G07] => Array
(
[wafer] => 9-0048-1
[cell] => G07
[isc] => 2.922336
[start_time] => 2014-07-21 09:51:56
)
I set the key of each element equal to the wafer and cell.
Each wafer has 5 different types of cells.
I want to compare the 5 different cells in each wafer and do some calculations.
The 5 different type of cells are C09, c05, K05, K09, and G07. I want to be able to
do calculations with the ISC of each cell. Such as I want to compare the ISC of two cells (c09 and c05) and find the uniformity ((bigger value - lower value)/bigger value) of the two. I'm not sure how to go about being able to compare the different values.
This code may help you, let's say your multidimensional array is $Array and you need to compare elements with wafer="9-0048-1", then this code will help you get all the elements with the wafer="9-0048-1" in an array
$wafer_to_compare = "9-0048-1";
$array_to_compare = [];
foreach ($Array as $key => $array)
if(strpos($key, $wafer_to_compare)!=null)
$array_to_compare[] = $array;

How to add a new [key] => [value] pair after a specific Associative Key in an Assoc Array in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
array_splice() for associative arrays
How to add an array value to the middle of an associative array?
How can I add a new [key] => [value] pair after a specific Associative Key in an Assoc Array in PHP ?
For example, let's say we have an array called $fruits:
array {
[apple] => 1
[banana] => 3
[orange] => 4
}
How can I add [plum] => 2 to $fruits so that it appears after the [apple] key but before [banana] key ?
Thanks.
You'll probably need to use array_splice() to cut the array into two pieces, and then recreate the array with array_merge() - Try this nifty function I just whipped up.
Live Sample: http://codepad.org/gGm5C1od
<?php
$orig = array(
"apple" => 1,
"banana" => 3,
"orange" => 4,
);
$orig = InsertKeyValuePair($orig, "plum", 2, 1);
var_dump($orig);
function InsertKeyValuePair($arr, $key, $val, $index){
$arrayEnd = array_splice($arr, $index);
$arrayStart = array_splice($arr, 0, $index);
return (array_merge($arrayStart, array($key=>$val), $arrayEnd ));
}
?>
If your ultimate goal is serial number(or something else) of fruits based on there name
for eg to find serial number of apple you will use fruits['apple'] then it doesn't matter where a particular element in array exists and if you really want to do it your own way then you can use array_splice() function
Here's a nice tutorial
http://www.ferolen.com/blog/insert-into-add-element-to-array-at-specific-position-php/
Assuming associative keyed arrays have a predictable order is not good design. If you want enforced order, you should mandate it in your design. For example key the array by the order number, then write custom functions to retrieve data by fruit name.
If you keep assuming keys have a natural order, one day you will run into a scenario where the order may not be as you expected it, and you will have broken functionality.

Categories