I have the following array containing a master record of training courses held on a system (select_group just identifies related course groups):
Array
(
[DE00041-1] => Array
(
[select_group] => 1
)
)
The training course ids are then queried against a table containing only running courses whilst also grabbing extra information.
As one training course may run more than once per year they have a running_id which gives the following:
Array
(
[DE00041-1] => Array
(
[title] => xxx
[405] => Array
(
[start_quarter] => 1
[end_quarter] => 1
)
)
)
What I then need to do is add the original array select_group data into the newly created array.
The end result should be:
Array
(
[DE00041-1] => Array
(
[title] => xxx
[select_group] => 1
[405] => Array
(
[start_quarter] => 1
[end_quarter] => 1
)
)
)
I have looked over other SO / Google answers but couldn't find what I was looking for.
There may be 1-20 training courses listed, each one running once to four times per year.
foreach($yourArray as $key => $value){
$yourArray[$key]['select_group'] = $select_group_array[$key]['select_group'];
}
Another variant:
$result = array_merge_recursive($arr1, $arr2);
Related
I'm dealing with this array, but the key [row_204] changes each time (ie sometimes it is [row_79] or [row_109]) but all other key names stay the same in this exact structure. I need to get the value of UUID and userID but can't find a solution to get the value by key in that [row_] array.
I need to be able to extract the values and place them in strings, for example,
$uuid =
and so on.
I can't seem to find a similar query and have tried so many variations. Many thanks in advance.
Array
(
[action] => edit
[data] => Array
(
[row_204] => Array
(
[UUID] => 148367FF-FBEB-413D-8495-6B1539BDC5DC
[userID] => 7
[maxPoints] => 7
[awardedPoints] => 6
[Date] => 2017-06-08
)
)
)
If you know there is always only one row_* item in that array, you can just pull the first item (i.e., the only one in your case) off the front of the list with array_shift():
$data = array_shift($array['data']);
print_r($data);
Will give you:
Array (
[UUID] => 148367FF-FBEB-413D-8495-6B1539BDC5DC
[userID] => 7
[maxPoints] => 7
[awardedPoints] => 6
[Date] => 2017-06-08
)
Then you can just deference the keys you want:
$uuid = $data['UUID'];
The easiest would probably be:
$data = current($_POST['data']);
Then just echo $data['UUID'];.
If you need the key for whatever reason:
list($key, $data) = each($_POST['data']);
I've got a multidimensional array written in php that holds an array of arrays. I've read a lot about how to search this, but it seems most solutions either:
A. require you have unique values for the keys, such as a product id
or
B. are satisfied with returning multiple results in an array
I am looking to search the array given the round number (which is the array number of the highest/first level array), and a player name (which will be the value of either the key player 1 or player 2).
The array looks something like this:
Array (
[0] => Array ( )
[8] => Array (
[1] => Array (
[Match] => 1
[Player1seed] => (Q)
[Player1name] => Mahut
[Player2seed] => (2)
[Player2name] => Goffin
[Matchscore] => 7-6(1), 6-1
[Round] => Finals
)
)
[7] => Array (
[1] => Array (
[Match] => 1
[Player1seed] => (2)
[Player1name] => Goffin
[Player2seed] =>
[Player2name] => Muller
[Matchscore] => 7-6(4), 6-4
[Round] => Semi-Finals
)
[2] => Array
(
[Match] => 2
[Player1seed] => (Q)
[Player1name] => Mahut
[Player2seed] => (WC)
[Player2name] => Haase
[Matchscore] => 5-7, 6-3, 6-4
[Round] => Semi-Finals
)
)
etc.
Essentially, I need to be able to search specifically one subset such as array[7] and be returned the results that contains either player1 or player2 as a name, say Goffin.
But I don't want it to return results from other tournament rounds such as array[8] or array[6] where either player is Goffin.
I can't seem to find this solution anywhere. Am I setting up my array incorrectly? Or expecting database functions from a lesser data set?
Any help would be appreciated.
It's not exactly the way I wanted to solve the problem, but I was able to get the results I wanted by running a loop after identifying the specific round number:
$r = $roundnumber;
foreach( $matchesarray[$r] AS $key=>$data ){
$winnerseed=$data['Player1seed'];
$winnername=$data['Player1name'];
$loserseed=$data['Player2seed'];
$losername=$data['Player2name'];
$matchurl=$data['Matchurl'];
$score=$data['Matchscore'];
if ($p1name == $winnername || $p2name == $losername){
$winner=$p1;
}
else if ($p2name == $winnername || $p1name == $losername){
$winner=$p2;
}
}
Below is a array generated by a query builder.
$random_array = Array ( [0] => Array ( [text] => A great time was had by all! )
[1] => Array ( [text] => KILL SHOT )
[2] => Array ( [text] => How is it possible)
[3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
[4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
)
Currently i am doing like this to print the random value
print_r(array_rand($random_array,1));
This is printing the array key as 3 or 1 etc(random from above array).I want to print the value of the key and not the key.
e.g I want to print the random value like this "http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!" instead of 3 or 1 which is printing now.
Is it possible to do this.
You will have one more line of code as shown below:
$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] ); //use the key to print value
What about simply calling
$randNumber = rand(0,count($random_array))-1; //index in array starts with 0
print (string) $random_array[$randNumber];
I'm using a SESSION variable to hold items added to an ingredients page. I'm wondering how I can uniquely identify each key in the array.
I'm adding ingredients via the following and it's working fine.
$_SESSION['ingredients'][] = array($_POST['ingredient'],$_POST['qty']);
If I stick a few ingredients in there and print the array I get..
Array ( [0] => 1 [1] => 50 ) Array ( [0] => 2 [1] => 50 ) Array ( [0] => 3 [1] => 50 )
Where 1, 2 and 3 are the ingredient IDs.
I can remove ingredients from the array based on their ID no problem, but if I put the same ingredient in twice I won't be able to distinguish between them. I was wondering if I can add an incremental number to ID the key?
Each of the items in $_SESSION['ingredients'] already has a unique index (starting from 0 in your case). When you print your $_SESSION['ingredients'] array, you should get this:
Array ( [0] => Array ( [0] => 1 [1] => 20 ) [1] => Array ( [0] => 2 [1] => 20 ) [2] => Array ( [0] => 1 [1] => 10 ) )
Notice that each array combination has an index preceding it (starting at 0)
The following code demonstrates this:
<?php
session_start();
unset($_SESSION['ingredients']);
$_SESSION['ingredients'][] = array(1, 20);
$_SESSION['ingredients'][] = array(2, 20);
$_SESSION['ingredients'][] = array(1, 10); // adding the same ingredient again
print_r($_SESSION['ingredients']);
?>
Why not use the ingredient id as the key in the session array and then append each value to it as an element
$_SESSION['ingredients'][$_POST['ingredient']][] = $_POST['qty'];
This would give you
Array(
[1] => array(
[0] => 50,
[1] => 50
)
)
Just a thought, I don't know if this would work for your use case
change your inserted array to this:
$_SESSION['ingredients'][count($_SESSION['ingredients'])] = array($_POST['ingredient'],$_POST['qty']);
I use it in my program.
I have four tables: followers, users, mixes, songs I am trying to get all the mixes from all the followers of one user, I have that part figured out, but I also want to get the songs from each of those mixes, currently my query is giving me results but each result is for one song on the mix, rather than an array of songs within each result for one mix ... any help would be amazing, my sql skills aren't the greatest and I have spent a lot of time trying to figure this out!
my current query is:
SELECT followers.following_id, users.id, users.user_username, mixes.id, mixes.mix_created_date, mixes.mix_name,songs.song_artist
FROM followers, users, mixes,songs
WHERE followers.user_id = 46
AND users.id = followers.following_id
AND mixes.user_id = followers.following_id
AND mixes.id > 0
ORDER BY mixes.mix_created_date DESC
LIMIT 10
the current result is (from running this through a cakephp custom query)
Array
(
[0] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[song_artist] => Yo La Tengo
)
)
[1] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[song_artist] => Animal Collective
)
)
as you can see the mix id's are the same, I am trying to get the songs to be an array inside of each result like :
Array
(
[0] => Array
(
[followers] => Array
(
[following_id] => 47
)
[users] => Array
(
[id] => 47
[user_username] => someguy
)
[mixes] => Array
(
[id] => 45
[mix_created_date] => 2012-07-21 2:42:17
[mix_name] => this is a test
)
[songs] => Array
(
[0]=>array(
['song_artist'] => Yo La Tengo
),
[1]=>array(
['song_artist'] => Animal Collective
)
)
)
Really hoping this can be done with just one sql statement! thanks in advance!
You can use the SQL join command to make multiple queries together..
Use this...
sql_join
first a note: it looks like you have a missing condition. according to the above query, every song in songs table will be joined with every result possible. probably there should be a condition similar to the following added: (column names can be different based on your tables):
...
and mix.song_id=songs.song_id
...
as for your question: I don't know php so i regard mysql alone: I don't think it is possible to do it with mysql. mysql returns rows in the result set and each row can contain a single value in each column. to add a group of values (song names) in one column, they must be concatenated (and that is possible: Can I concatenate multiple MySQL rows into one field?), and later you split them back in your php script. this is not a good idea as you will need to choose a separator that you know will never appear in the values that are concatenated. therefore I think its better to remove the songs table from the query and after getting the mix id, run a second query to get all songs in that mix.