I have a multidimensional array like this one:
Array
(
[site1] => Array
(
[0] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[1] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[2] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[site2] => Array
(
[0] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[1] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[2] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
)
)
I am trying to randomize the data for each site ([site1], [site2]) but don't mix the data between sites. So it would be like a second tire randomization.
For example, after the randomization, the position [0] for [site1] would have different data, maybe the data from the earlier position [3].
Any ideas how to do it?
You map the shuffle function to the array:
$shuffle = function($array) {
$r = shuffle($array);
return $array;
};
$sites = array_map($shuffle, $sites);
Or:
foreach ($sites as &$site) shuffle($site);
unset($site);
foreach($sites as $i => $site) {
shuffle($sites[$i]);
}
Another, (not as good as shuffle) way ;-)
foreach ($site as $key => $data) {
$values = array();
$rand = array_rand($data, count($data));
for ($i = 0; $i < count($rand); $i++)) {
$values[] = $site[$key][$rand[$i]];
}
$site[$key] = $values;
}
Related
I've an php array that I've got from Excel file, for example:
$arrayOne
Array
{
[0] => Array
{
[0] => 0_age
[1] => 1_academic id
[2] => 2_name
[3] => 3_sex
}
[1] => Array
{
[0] => 0_18
[1] => 1_110291
[2] => 2_Jason
[3] => 3_Male
}
}
and in the mid of proccess, the array value from index [0] that consist data from Excel Header set into duallistbox for elimination and sorting then set into new array $newArray. So then, I got this array result:
$newArray
Array
{
[0] => Array
{
[0] => 2_name
[1] => 1_academic id
[2] => 3_sex
}
}
And I expect the system also can eliminate and sorting the data of array from index [1] that consist of Excel Data.
So the expected result is like this:
$expectedArray
Array
{
[0] => Array
{
[0] => 2_Jason
[1] => 1_110291
[2] => 3_Male
}
}
anyone know idea or how to solve this case? I've add an id(e.g: 0_) in each of array value that maybe useful for sorting.
Thanks
Edited
The sorting is based on the id that have been set on each value of array, so each element on array index [1] from $arrayOne is reset into new sequence adapted to same id from $newArray.
One way
<?php
$arr = array(
array
(
"0_age",
"1_academic id",
"2_name",
"3_sex",
),
array
(
"0_18",
"1_110291",
"2_Jason",
"3_Male",
),
array
(
"0_28",
"1_110291111",
"2_Jason Second",
"3_Female",
)
);
?>
<pre>
<?php
$new_array = array();
$count = 0;
foreach($arr as $a){
if($count==0){
}else{
$new_array[] = array(
$arr[0][0] => $a[0],
$arr[0][1] => $a[1],
$arr[0][2] => $a[2],
$arr[0][3] => $a[3],
);
}
$count=$count+1;
}
print_r($new_array);
?>
</pre>
Output
Array
(
[0] => Array
(
[0_age] => 0_18
[1_academic id] => 1_110291
[2_name] => 2_Jason
[3_sex] => 3_Male
)
[1] => Array
(
[0_age] => 0_28
[1_academic id] => 1_110291111
[2_name] => 2_Jason Second
[3_sex] => 3_Female
)
)
Second Way that exactly match with your output
$new_array = array();
$count = 0;
foreach($arr as $a){
if($count==0){
}else{
$new_array[] = array(
//$a[0],
$a[2],
$a[1],
$a[3],
);
}
$count=$count+1;
}
print_r($new_array);
Output:
Array
(
[0] => Array
(
[0] => 0_18
[1] => 1_110291
[2] => 2_Jason
[3] => 3_Male
)
[1] => Array
(
[0] => 0_28
[1] => 1_110291111
[2] => 2_Jason Second
[3] => 3_Female
)
)
You can search, Sort and remove any field. Update on the basis of your requirement.
i have this array
$items = Array (
[0] => Array ( [0] => xx [1] => 'update')
[1] => Array ( [0] => bx [1] => 'update')
[2] => Array ( [0] => xx [1] => 'creation')
[3] => Array ( [0] => fs [1] => 'creation')
[4] => Array ( [0] => tx [1] => 'update')
[5] => Array ( [0] => bx [1] => 'creation')
)
i'm trying to remove duplicate values based on the first element (xx,bx,ax etc)
if two elements from the same table match, i'd like to keep the last one with higher index,
the result would be like the following
$items = Array (
[0] => Array ( [0] => xx [1] => 'creation')
[1] => Array ( [0] => bx [1] => 'creation')
[2] => Array ( [0] => fs [1] => 'creation')
[3] => Array ( [0] => tx [1] => 'update')
)
keeping the last one was a bit confusing to me as i'm new to PHP.
Thank you in advance
Generic deduplication:
$result = [];
foreach ($array as $item) {
$result[$item[0]] = $item;
}
Functional:
$result = array_reduce($array, function ($acc, $i) {
return [$i[0] => $i] + $acc;
}, [])
Awesome:
array_column($array, null, 0);
Well, i found the solution,
i started bu reversing the array, then executed the following code
$taken = array();
$reverse = array_reverse($items, true);
foreach($reverse as $key => $item) {
if(!in_array($item[0], $taken)) {
$taken[] = $item[0];
} else {
unset($reverse[$key]);
}
}
Good afternoon :-)
Well, I have two arrays like this:
1st Array
Array
(
[0] => DATA 1\n
[1] => DATA 2\n
[2] => DATA 3\n
[3] => =\n
[4] => DATA 4\n
[5] => DATA 5\n
[6] => DATA 6\n
[7] => DATA 7\n
[8] => =\n
[9] => DATA 8\n
[10] => DATA 9\n
)
2nd Array
Array
(
[0] => 567
[1] => 568
[2] => 569
)
As you can see, in my 1st array I have = element as a delimiter, and my final array must look like this:
Final Array
Array
(
[567] => Array
(
[0] => DATA 1\n
[1] => DATA 2\n
[2] => DATA 3\n
)
[568] => Array
(
[0] => DATA 4\n
[1] => DATA 5\n
[2] => DATA 6\n
[2] => DATA 7\n
)
[569] => Array
(
[0] => DATA 8\n
[1] => DATA 9\n
)
)
I'm using this code to read the 1st array and detect the = delimiter,
for ($i = 0; $i < count($raw_data); $i++) {
if ($raw_data[$i] == "=\n") {
# Code here...
}
}
But I'm confused with the logic to:
Get all array elements before and after delimiter (I will use a lot of delimiters, what will happens with the array elemets of the last one?)
Assign the 2nd array values to the corresponding 1st array values as keys
Any help will be appreciated.
Thanks.
You could do something like this:
// $raw_data = 1st Array
// $keys = 2nd Array
$output = array();
foreach ($raw_data as $data) {
if ($data == '=\n') {
next($keys);
continue;
}
$output[current($keys)][] = $data;
}
Here is a double foreach loop solution:
// Value array
$one = array(
0 => 'DATA 1\n',
1 => 'DATA 2\n',
2 => 'DATA 3\n',
3 => '=\n',
4 => 'DATA 4\n',
5 => 'DATA 5\n',
6 => 'DATA 6\n',
7 => 'DATA 7\n',
8 => '=\n',
9 => 'DATA 8\n',
10 => 'DATA 9\n');
// Key array
$two = array(567,568,569);
// Loop through to-be-keys array
foreach($two as $nums) {
// Loop through your to-be-values array
foreach($one as $key => $data) {
// If value is not =\n
if($data !== '=\n') {
// Save new array with assigned key and value
$new[$nums][] = $data;
// Unset keys as you go
unset($one[$key]);
}
else {
// If you hit =\n, unset the value
//break the loop
unset($one[$key]);
break;
}
}
}
// Output new array
print_r($new);
OUTPUT:
Array
(
[567] => Array
(
[0] => DATA 1\n
[1] => DATA 2\n
[2] => DATA 3\n
)
[568] => Array
(
[0] => DATA 4\n
[1] => DATA 5\n
[2] => DATA 6\n
[3] => DATA 7\n
)
[569] => Array
(
[0] => DATA 8\n
[1] => DATA 9\n
)
)
1. Using For-each Loop
$inner = array();
foreach($short_array as $key => $val){
foreach($long_array as $k => $v){
if($v == '=\n'){ continue; }
$inner .= $key.'=>'.$v;
}
}
The Performance May Not Be too Good here
username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);