if only one array is there for example
$values = array(x, y, z);
i am adding them into database like this
foreach ($values as $value)
{
$insertFunction = addValues($value);
}
my arrays:
$array1 = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 );
$array2 = Array ( fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
But i want both array to combine and insert them into database.
How can i do this please help me
Updated:
When i am printing the POST values i am getting out put like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 )
Array ( [0] => fb1 [1] => or1 [2] => fb2 [3] => or2 [4] => fb3 [5] => or3 [6] => fb4 [7] => or4 [8] => fb5 [9] => or5 )
when i tried with array_merge my out put is like this
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 1 [5] => 2 [6] => 1 [7] => 2 [8] => 1 [9] => 2 [10] => fb1 [11] => or1 [12] => fb2 [13] => or2 [14] => fb3 [15] => or3 [16] => fb4 [17] => or4 [18] => fb5 [19] => or5 )
How to insert them in separate columns in a table $array1 and $array2
my database table is like this
1.id
2.username
3.network_id
id is primary key
network_id values coming in array1
username values coming in array2
EDIT:
After you mentioned seperated columns I think I understand what you're looking for:
I'm assuming that array1 and array2 are in the same size.
for($i = 0; $i < count($array1); $i++)
{
$array2[$i] = (int)$array2[$i]; //"validating" the username (an integer)
mysql_query("INSERT INTO yourTableName (`username`,`network_id`) VALUES('".$array2[$i]."','".$array1[$i]."')");
}
Result:
tblName:
username: 1 2 1 ...
network_id: fb1 or1 fb2 ...
Is that what you were looking for?
Ignore this and merging:
$combined = array_merge($array1 , $array2);
//$combined = Array ( 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,fb1, or1, fb2, or2, fb3, or3, fb4, or4, fb5, or5 );
I think you need array_merge.
You can use array_merge(), function to merge multiple array into one.
$arrays = array_merge($array1 , $array2);
foreach ($arrays as $value)
{
$insertFunction = addValues($value);
}
If you want associate an element from array a to an element from array b, you have to use array_combine() function.
$arrays = array_combine($array1,$array2);
foreach ($array as $aValue)
{
$insertFunction = addValues($aValue);
}
Related
I originally have this array
Array
(
[0] => Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25
[1] => Logico deduttive^7^^21,^0.75^0^-0.25
[2] => Situazionali^8^^20,^0.75^0^0.375
)
Using the function explode and array_diff i can get to this
Array
(
[0] => Amministrativo
[1] => 25
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,
[4] => 0.75
[5] => 0
[6] => 0.25
)
Array
(
[0] => Logico deduttive
[1] => 7
[2] =>
[3] => 21,
[4] => 0.75
[5] => 0
[6] => -0.25
)
Array
(
[0] => Situazionali
[1] => 8
[2] =>
[3] => 20,
[4] => 0.75
[5] => 0
[6] => 0.375
)
but I would like to concatenate the elements of each array to get a unique array. I think I need to use the array_map function but I don't know how. This below is the result I would like to achieve
Array (
[0] => Amministrativo Logico deduttive Situazionali
[1] => 25 7 8
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,21,20,
[4] => 0.75 0.75 0.75
[5] => 0 0 0
[6] => 0.25 -0.25 0.375
)
tnks
EDIT:
I tried the code that is here and it is fine.
But now I realized that there is also the problem that the arrays can be in variable number 1, 2, 3 or more and I can't know it before, I should adapt this code
$result = array_map(function ($item1, $item2,$item3) {
return "$item1 $item2 $item3";
}, $test[0], $test[1],$test[2]);
The lines of the original array are split with explode. The result is a two-dimensional array. This is transposed (swapping rows and columns). Then the lines are reassembled with implode.
function array_transpose(array $a) {
$r = array();
foreach($a as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) $r[$keyCol][$keyRow] = $value;
}
return $r;
}
$arr = [
'Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25',
'Logico deduttive^7^^21,^0.75^0^-0.25',
'Situazionali^8^^20,^0.75^0^0.375',
];
foreach($arr as $i => $row){
$arr[$i] = explode('^',$row);
}
$arr = array_transpose($arr);
foreach($arr as $i => $row){
$arr[$i] = implode(' ',$row);
}
var_export($arr);
Demo: https://3v4l.org/IdsDh
I have two arrays:
Array ( [0] => a [1] => b [2] => c [3] => d )
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
I want to merge them so they end up like this:
Array ( [0] => a [1] => 1 [2] => b [3] => 2 [4] => c [5] => 3 [6] => d [7] => 4 )
Something like array 1[1], array 2[1], array 1[2], array 2[2], etc.
It's probably simple but I can't find an answer anywhere!
You would have to use a loop as far as I know, based on the length of the longest array.
// input arrays
$array1 = array( 1, 2, 3, 4 );
$array2 = array( 'a', 'b', 'c', 'd' );
// output arrays
$array3 = array();
// get the longest for the loop
$length = count($array1) > count($array2)? count($array1) : count($array2);
// if there is an element set for the index append to the output array
for ( $i=0; $i<$length; $i++ ){
if ( isset($array1[$i]) ) $array3[] = $array1[$i];
if ( isset($array2[$i]) ) $array3[] = $array2[$i];
}
print_r( $array3 );
Results in:
Array ( [0] => 1 [1] => a [2] => 2 [3] => b [4] => 3 [5] => c [6] => 4 [7] => d )
Run in this PHP Fiddle.
I have two arrays like this
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 5
)
Array
(
[0] => Array
(
[1] => Test1
[2] => Location1
[3] => Email1
[4] => Name1
[5] => Address1
[6] => Age1
[7] => Gender1
[8] => Phone1
[9] => Website1
[10] => Cell1
)
[1] => Array
(
[1] => Test2
[2] => Location2
[3] => Email2
[4] => Name2
[5] => Address2
[6] => Age2
[7] => Gender2
[8] => Phone2
[9] => Website2
[10] => Cell2
)
[2] => Array
(
[1] => Test3
[2] => Location3
[3] => Email3
[4] => Name3
[5] => Address3
[6] => Age3
[7] => Gender3
[8] => Phone3
[9] => Website3
[10] => Cell3
)
)
Now i have to select 1,3,4 and 5 index value from each second array. How can i do this without two loops. I know i will have to use one but i don't want to use two loops
Output Required
Array
(
[0] => Array
(
[1] => Test1
[3] => Email1
[4] => Name1
[5] => Address1
)
[1] => Array
(
[1] => Test2
[3] => Email2
[4] => Name2
[5] => Address2
)
[2] => Array
(
[1] => Test3
[3] => Email3
[4] => Name3
[5] => Address3
)
)
Using one loop, array_flip, and array_intersect_key, you can do it like this:
$array_one = array(1, 3, 4, 5);
$array_two = array(
array(1 => 'Test1', 'Location1', 'Email1', 'Name1', 'Address1', 'Age1', 'Gender1', 'Phone1', 'Website1', 'Cell1'),
array(1 => 'Test2', 'Location2', 'Email2', 'Name2', 'Address2', 'Age2', 'Gender2', 'Phone2', 'Website2', 'Cell2'),
array(1 => 'Test3', 'Location3', 'Email3', 'Name3', 'Address3', 'Age3', 'Gender3', 'Phone3', 'Website3', 'Cell3')
);
$array_one_flip = array_flip($array_one);
foreach($array_two as $k => $v) {
$result[] = array_intersect_key($v, $array_one_flip);
}
print_r($result);
The result would be:
Array
(
[0] => Array
(
[1] => Test1
[3] => Email1
[4] => Name1
[5] => Address1
)
[1] => Array
(
[1] => Test2
[3] => Email2
[4] => Name2
[5] => Address2
)
[2] => Array
(
[1] => Test3
[3] => Email3
[4] => Name3
[5] => Address3
)
)
You could try it with array_intersect_key:
$keys = array_flip($keys_array); // flip the keys array for matching with array_intersect_key
$result = array();
foreach ($content_array as $arr)
{
$result[] = array_intersect_key($arr, $keys);
}
Working example on codepad. Thanks #Michael Irigoyen for the arrays...
Just use an array_map function like this:
$keys_to_keep = array(1, 3, 4, 5);
$key_diff_array = array_fill_keys($keys_to_keep, 'not used');
$array; // your main array you want to filter assume value has been set elsewhere
$filtered_array = array_map(function ($value) use $key_diff_array {
return array_intersect_key($value, $key_diff_array);
}, $array);
using one loop:
<?php
$selection = array(1, 3, 4, 5);
for($i = 0; $i < count($array); $i++){
reset($selection);
$first = current($selection);
$newarray[$i][$first] = $array[$i][$first];
$next = next($selection);
$newarray[$i][$next] = $array[$i][$next];
$next = next($selection);
$newarray[$i][$next] = $array[$i][$next];
$next = next($selection);
$newarray[$i][$next] = $array[$i][$next];
}
echo '<pre>';
print_r($newarray);
You might also find this Note about array_flip useful.
Ensure that $array1 has values that can be accepted as array keys when flipped.
http://php.net/manual/en/language.types.array.php
The key can either be an integer or a string. The value can be of any type.
Additionally the following key casts will occur:
Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
Null will be cast to the empty string, i.e. the key null will actually be stored under "".
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
http://www.php.net/manual/en/function.array-flip.php
array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.
Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.
If a value has several occurrences, the latest key will be used as its value, and all others will be lost.
I have an array like below,
[test] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
[4] => 32
[5] => 51
)
i need to change this array into like below,
[test] => Array
(
[2] => 1
[4] => 3
[6] => 5
[8] => 13
[10] => 32
[12] => 51
)
i need to change the key value. How can i do this?.
$newArray = array_combine(
range(2,count($originalArray)*2,2),
array_values($originalArray)
);
The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as
$arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
$count = 1;
$tempArr = array();
foreach ($arr as $key => $val) {
$tempArr[$count * 2] = $val;
$count++;
}
var_dump($tempArr);exit;
Try this code at your side.
I have a array with values based on a specific player and I want to use some of these values(match_id and minutes) to compare to a second array and then create the third array to hold the returned values.
I'm writing this for a Joomla based website and so I'm using an inbuilt array functions LoadRowList(); which returns an indexed array of indexed arrays from the table records returned by the query.
The first array contains values populated based on a particular player
$search = array();
$query = "SELECT first_name,match_id,m_name,minutes,points
FROM #__bl_players as pl,#__bl_match_events as me,#__bl_matchday as md, #__bl_match as m
WHERE pl.id = me.player_id
AND pl.id = ".$player_id."
AND me.match_id = m.id
AND m.m_id = md.id
";
$db->setQuery($query);
$search = $db->loadRowList();
The $search array outputs an array in the format below. print_r($search);
Array (
[0] => Array ( [0] => Marco [1] => 33 [2] => Xornada 04 [3] => 7 [4] => 3 )
[1] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 1 [4] => 2 )
[2] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 6 [4] => 2 )
)
The second array contains hundreds of records with the same layout as the first.
$data = array();
$query = "SELECT first_name,match_id, m_name,minutes,ecount
FROM #__bl_players as pl,#__bl_match_events as me,#__bl_matchday as md, #__bl_match as m
WHERE pl.id = me.player_id
AND me.match_id = m.id
AND m.m_id = md.id
AND md.s_id = ".$s_id."
";
$db->setQuery($query);
$data = $db->loadRowList();
The $data array outputs in the same format as above. print_r($data);
Array (
[0] => Array ( [0] => Pablo [1] => 8 [2] => Xornada 01 [3] => 2 [4] => 3 )
[1] => Array ( [0] => Juan Ramón [1] => 9 [2] => Xornada 01 [3] => 1 [4] => 0 )
[2] => Array ( [0] => Pedro [1] => 9 [2] => Xornada 01 [3] => 1 [4] => 3 )
)etc etc etc
And finally, I need the third array to be populated by records that are a match between the $search and $data arrays based only on match_id and minutes.
Array(
[0]=>Array ([1]=> PlayerName(from the first array)[2]=> MatchID[3]=> MatchName[4]=> TimePlayed[5]=> Score(from the first array)[6]=> Score(from the second array)[7]=> PlayerName(from the second array))
)etc etc etc
I have revised my original question to include more information I'm no expert so I'm looking for all and any ideas and feedback. Thanks
Note:The column names areas follows Name = first_name, Match ID = match_id, Match name = m_name,The event time = minutes and the Points scored = points
Issues I'm having
In response to comments this is my problem take the following code for example (its only an example):
$matches = array();
foreach ($data as $entry){
if ($entry[2] == $search[2]) {
match =$search;
$matches[] = $match
}
}
print_r($mtaches)
If I use a comparison like $entry[2] == $search[2] it will be comparing the 2nd arrays from each ie
`[2] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 6 [4] => 2 )
==
[2] => Array ( [0] => Pedro [1] => 9 [2] => Xornada 01 [3] => 1 [4] => 3 )`
so my question is how do you use a type of wildcard so I can compare key values ie
$entry[*][2] == $search[*][2]
Thanks to everyone for their help and patience and especially to #erisco for sharing his knowledge
This problem can likely be solved by a change in query, but without an exact database structure given it is difficult to guess as to what it may look like. If I couldn't change the query, I would solve the problem along these lines.
You want to match based on match_id and minutes, so it would be ideal to have those as indexes.
Array (
[0] => Array ( [0] => Marco [1] => 33 [2] => Xornada 04 [3] => 7 [4] => 3 )
[1] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 1 [4] => 2 )
[2] => Array ( [0] => Marco [1] => 129 [2] => Xornada 08 [3] => 6 [4] => 2 )
)
Changes to:
array(
33 => array(
7 => array("Marco", "Xornada 04", 3)
),
129 => array(
1 => array("Marco", "Xornada 08", 2)
6 => array("Marco", "Xornada 08", 2)
)
)
The same would apply to the second array as well. Then, you can merge them together based on key. If your second array looked like this:
array(
33 => array(
7 => array("Pablo", "Xornada 04", 3)
)
)
Then the merged version would look like this:
array(
33 => array(
7 => array(
array("Marco", "Xornada 04", 3),
array("Pablo", "Xornada 04", 3)
)
)
)
(Whether you throw out non-matches at the time of merging or if you simply exclude match_id/minutes indexes that don't have at least two players later on is up to you.)
Now you can reduce this array to your desired form.
array(
array("Marco", 33, "Xornada 04", 7, 3, 3, "Pablo")
)
This is an example of how the transformation could take place.
<?php
$one = array(
array("Marco", 33, "Xornada 04", 7, 3),
array("Marco", 129, "Xornada 08", 1, 2),
array("Marco", 129, "Xornada 08", 6, 2)
);
$two = array(
array("Pablo", 33, "Xornada 04", 7, 1),
array("Pedro", 129, "Xornada 08", 1, 0),
array("Garcia", 45, "Xornada 01", 2, 2)
);
$three = array();
foreach ($one as $o) {
$three[$o[1]][$o[3]][] = array($o[0], $o[2], $o[4]);
}
foreach ($two as $t) {
if (isset($three[$t[1]][$t[3]])) {
$three[$t[1]][$t[3]][] = array($t[0], $t[2], $t[4]);
}
}
$four = array();
foreach ($three as $matchId => $t) {
foreach ($t as $minutes => $tt) {
// filter out non-matches
if (count($tt) > 1) {
$p1 = $tt[0];
$p2 = $tt[1];
$four[] = array($p1[0], $matchId, $p1[1], $minutes, $p1[2], $p2[2], $p2[0]);
}
}
}
print_r($four);
$search = array(
1 => 'PlayerName',
2 => 'MatchID',
3 => 'MatchName',
4 => 'TimePlayed',
5 => 'Score',
);
$data = array(
array(
// match
1 => 'PlayerName',
2 => 'MatchID',
3 => 'MatchName',
4 => 'TimePlayed',
5 => 'Score',
),
array(
// match
1 => 'OtherPlayerName',
2 => 'MatchID',
3 => 'OtherMatchName',
4 => 'TimePlayed',
5 => 'OtherScore',
),
array(
// no match
1 => 'OtherPlayerName',
2 => 'OtherMatchID',
3 => 'OtherMatchName',
4 => 'OtherTimePlayed',
5 => 'OtherScore',
),
);
$matches = array();
foreach ($data as $entry) {
if ($entry[2] == $search[2] && $entry[4] == $search[4]) {
$match = $search;
$match[6] = $entry[5];
$match[7] = $entry[1];
$matches[] = $match;
}
}
print_r($matches);