i'm having trouble with combining array values. I have tried to combine them and make pairs something what i have done already but i want to erase some pairs that i do not want. Please some one to help me this happen .
// the Code I have now is:
$inputarray = array('Mussafiri', 'Fire', 'Ubungo', 'Mbezi');
$outputarray = array();
$i = 0;
foreach($inputarray as $values) {
$j = 0;
foreach($inputarray as $values2) {
if($values != $values2){
$outputarray[] = array($values => $values2);
}
$j++;
}
$i++;
}
print_r($outputarray);
//Output array is:
Array ( [0] => Array ( [Mussafiri] => Fire)
[1] => Array ( [Mussafiri] => Ubungo)
[2] => Array ( [Mussafiri] => Mbezi)
[3] => Array ( [Fire] => Mussafiri)
[4] => Array ( [Fire] => Ubungo)
[5] => Array ( [Fire] => Mbezi)
[6] => Array ( [Ubungo] => Mussafiri)
[7] => Array ( [Ubungo] => Fire)
[8] => Array ( [Ubungo] => Mbezi)
[9] => Array ( [Mbezi] => Mussafiri )
[10] => Array ( [Mbezi] => Fire)
[11] => Array ( [Mbezi] => Ubungo) )
NOTE: I DO NOT WANT THE FOLLOWING PAIRS TO APPEAR, I want element to pair with the next element and not pair with the previous element so i do not like index 3,6,7,8,9,10 and 11 to appear in the pair list i.e
[3] => Array ( [Fire] => Mussafiri)
[6] => Array ( [Ubungo] => Mussafiri)
[7] => Array ( [Ubungo] => Fire)
[8] => Array ( [Ubungo] => Mbezi)
[9] => Array ( [Mbezi] => Mussafiri)
[10] => Array ( [Mbezi] => Fire)
[11] => Array ( [Mbezi] => Ubungo)
If I understand correctly you're trying to create all unique pairs from the array, in which case something like this should work:
$pairs = array();
foreach ($inputarray as $key => $value) {
for ($i = $key + 1; isset($inputarray[$i]); $i++) {
$pairs[] = array($value => $inputarray[$i]);
}
}
The for loop sets $i as the key of the current array element + 1, so it'll never be referencing itself, and will never reference any previous array members.
for($i=0; $i<$inputarray.length;$i++){
$k=$i+1;
for($j=$k; $j<$inputarray.length;$j++){
$outputarray[] = array($inputarray[$i] => $inputarray[$j]);
}
}
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 am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}
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
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);
Dear All,
I want to push the data into array. i am using flowing code. There are two arrays. one holding keys and 2nd values. i am using flowing code
while($data=mysql_fetch_array($result))
{
foreach ($arrTemp as $val)
{
array_push($arrKeys, $val);
array_push($arrValues, $data[$val]);
}
}
print_r($arrKeys);
print_r($arrValues);
$arrReturn = array_combine($arrKeys,$arrValues);
...................................
and get flowing results of two arrays.
Array ( [0] => due_date [1] => flag_code [2] => due_date [3] => flag_code [4] => due_date [6] => flag_code )
Array ( [0] => 12:04:2011 [1] => 0 [2] => 13:04:2011 [3] => 0 [4] => 14:04:2011 [6] => 0 )
when i try to combined the array using array_combined function it only return an array of two values like: Array (due_date => 14:04:2011 flag => 0)
how i can get all values in single array.....!
Its because your have multiple the same array keys. So first it inserts due_date, then flag_code, then it will try and insert another due_date but since this already exists in the array it will overwrite it. Thus the only values left in the array will be the last pair.
The solution is to not have multiple keys that are the same in one array (your due_date and flag_code)
You could do:
foreach ($arrTemp as $val) {
$arrReturn[] = array($val => $data[$val];
}
This will give you each set of results keyed in an array like so:
$arrReturn[0] = array (due_date => 14:04:2011 flag => 0);
$arrReturn[1] = array (due_date => 14:04:2011 flag => 0);
$arrReturn[2] = array (due_date => 14:04:2011 flag => 0);
...
$ctr = 0;
foreach ($arrKeys as $id => $key) {
$res_array[$ctr][$key] = $arrValues[$id];
if ($key == 'flag_code') $ctr++;
}
print_r($res_array);
Output:
Array
(
[0] => Array
(
[due_date] => 12:04:2011
[flag_code] => 0
)
[1] => Array
(
[due_date] => 13:04:2011
[flag_code] => 0
)
[2] => Array
(
[due_date] => 14:04:2011
[flag_code] => 0
)
)