I have more than 4 arrays in single variable as shown below
$myArray = Array ( [0] => NA [1] => NA [2] => USA [3] => NA [4] => Texas )Array ( [0] => NA [1] => NA [2] => UK [3] => NA [4] => Texas )Array ( [0] => NA [1] => NA [2] => USA [3] => NA [4] => Texas ) Array ( [0] => NA [1] => NA [2] => UAE [3] => NA [4] => Texas )
Now i need to compare each and every array of [2] index.
If second index ([2] => USA) is present in some other array then delete duplicate array.
Finally the arrays should look like this
Array ( [0] => NA [1] => NA [2] => USA [3] => NA [4] => Texas )Array ( [0] => NA [1] => NA [2] => UK [3] => NA [4] => Texas )Array ( [0] => NA [1] => NA [2] => UAE [3] => NA [4] => Texas ).
I have tried this but not able to sort it out.
$myArray = array_map("unserialize", array_unique(array_map("serialize", $myArray)));
Is there any way?
You can use a foreach loop for this, like DEMO:
$match = [];
foreach($myArray as $key => $value)
{
if(!in_array($value[2], $match))
{
$match[] = $value[2];
continue;
}
unset($myArray[$key]);
}
This will remove all of the arrays which have duplicate value for [2]
You need to iterate through your array with two loops and check the second index value in each of the inner arrays and if there is a duplicated value you can just unset the second one.
Here is a piece of code that should do well:
<?php
for($i=0; $i < count($myArray); $i++) {
for($j=$i+1; $j < count($myArray); $j++) {
if ($myArray[$i][2] == $myArray[$j][2]) {
unset($myArray[$j][2]);
}
}
}
P.S: The code is not tested and may need some modifications, but you could get the main idea.
Related
I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??
Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records
This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z
I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.
This is my array () ...not much English'm using google translator.
I'm printing this array with print_r (). but what I deceo is sort of form this down
Array
(
[0] => Array
(
[0] => 606125999550609
[1] => Patricia
[2] => Michelle
)
[1] => Array
(
[0] => 724417787635260
[1] => Nasshy
[2] => Green
)
[2] => Array
(
[0] => 1121064174618668
[1] => Luisanna
[2] => Rodriguez
)
[3] => Array
(
[0] => 1057585894278115
[1] => Libane
[2] => Heredia
)
)
Basically what I need is to sort this array as follows......
So I do not know how to sort follows in PHP...
Array
(
[0] => 606125999550609
[1] => 724417787635260
[2] => 1121064174618668
[3] => 1057585894278115
[4] => Patricia
[5] => Nasshy
[6] => Luisanna
[7] => Libane
[8] => Michelle
[9] => Green
[10] => Rodriguez
[11] => Heredia
)
This isn't so much "sorting", its' more of a manipulation/restructure. Using a loop to regenerate your array would be the option, but if you can modify the data from where it comes from, then that's always recommended.
$new = array();
array_map(function($obj) use(&$new) {
foreach($obj as $i => $elem) {
$new[$i][] = $elem;
}
}, $array);
In the example above, we're using array_map() to apply our function() {... that runs the loop of each element, applying it to our $new array.
All you need to do is pass your $array in as you see above.
Example/Demo
i have an array which is like this
Array
(
[0] => Array
(
[name] => Amanda Coleman
[id] => 98764676778
)
[1] => Array
(
[name] => Hashimi Sultana
[id] => 876304848
)
[2] => Array
(
[name] => Maren Shawesh
[id] => 988363747
)
[3] => Array
(
[name] => Ajo Khan
[id] => 039494857587
)
[4] => Array
(
[name] => Negar Jan
[id] => 948437484748
)
[5] => Array
(
[name] => Mehran Khan
[id] => 3948474947
)
[6] => Array
(
[name] => Sal Man
[id] => 039383734647
)
this is upto 566 mean this is my facebook friends name and ids
what i am trying to do is i want to make autocomlete of facebook friends
which work fine for me its showing what i want to do but i need the ids also against name
here is my code
if(isset($_REQUEST['queryString'])) {
lets $var = fa
$var = ucfirst ($_REQUEST['queryString']); //making first character uppercase for matching with fb name
$friends_inner=$_SESSION['friends'];
echo "<pre>";
print_r($friends_inner);
echo "</pre>";
the output is above array
for($i=0; $i<sizeof($friends_inner); $i++)
{
$selected_friend=$friends_inner[$i];
$selected_friend_id=$selected_friend['id']; //array of ids
$selected_friend_name=$selected_friend['name']; // array of names
$name[$i]=$selected_friend_name;
}
$result=preg_grep('/^'.$var.'.*/', $name);//returns array matching with requested alphabet from textbox which work fine dispalaying names
echo "<pre>";
print_r($result);
echo "</pre>";
here is the output
Array
(
[17] => Fazal Muhammad
[18] => Fawad Ahmad
[39] => Fayaz Zafar
[42] => Farhan Bogra
[66] => Farzana KArim Elora
[81] => Fahim Khan
[92] => Fahad Shams
[119] => Fazal Yazdan
[166] => Fakhar Alam
[173] => Faheem Ur Rahman
[183] => Fawaid Khan
[187] => Faizan Sabir
[258] => Fayaz Ali
[269] => Faizan Khattak
[308] => Faridullah Khan
[411] => Fawad Qamar
[446] => Fahad Khan
[458] => Fahad Khan
[507] => Faisl Qureshi
[529] => Faisal Khan
[538] => Faiza Baloch
[555] => Fawad Khan
)
as it its index is not sequentially so i am diong so to make it start from zero
$final_result=array();
$maxindex = max(array_keys($result));
for($i=0;$i<=$maxindex;$i++){ //returns array that start with zero
if(isset($result[$i])){
array_push($final_result,$result[$i]);
}
}
echo "<pre>";
print_r($final_result);
echo "</pre>";
the result is
Array
(
[0] => Fazal Muhammad
[1] => Fawad Ahmad
[2] => Fayaz Zafar
[3] => Farhan Bogra
[4] => Farzana KArim Elora
[5] => Fahim Khan
[6] => Fahad Shams
[7] => Fazal Yazdan
[8] => Fakhar Alam
[9] => Faheem Ur Rahman
[10] => Fawaid Khan
[11] => Faizan Sabir
[12] => Fayaz Ali
[13] => Faizan Khattak
[14] => Faridullah Khan
[15] => Fawad Qamar
[16] => Fahad Khan
[17] => Fahad Khan
[18] => Faisl Qureshi
[19] => Faisal Khan
[20] => Faiza Baloch
[21] => Fawad Khan
)
$ids_of_result=array();
now here is the big problem i want to extract all user ids from $friends_inner array which name equal to my $final_result array.i am doing so but the problem is that it goes upto last index i-e 22 and than through error that index 22 and ahead is not valid offset.how can i extract the ids from this $friends_inner which is my main array
for($j=0; $j<sizeof($friends_inner); $j++){
$selected_friend=$friends_inner[$j];
if($final_result[$j] == $selected_friend['name']){
echo $selected_friend['name'];
echo $selected_friend['id'];
array_push($ids_of_result,$selected_friend['id']);
//returns array of ids against result
}
}
echo "<pre>";
print_r($ids_of_result);
echo "</pre>";
it result in
Array
(
)
$counter=0;
foreach($final_result as $key=>$value){
echo '<li onClick="fill(\''.$ids_of_result[$counter].'\');">'.$value.'</li>';
$counter++;
}
}
please help me my code work fine in sense of autocomplete but i want the ids too.Graph api does not provide the id from name.if so i would not write much code.any help me thanx in advance
?>
function sort_arrays($final_result,$friends_inner) {
$ids_of_result = array();
foreach($final_result as $fnl_rslt){
foreach($friends_inner as $frnd_in){
if($fnl_rslt == $frnd_in['name']){
//echo 'Name : '.$frnd_in['name'].'<br>';
//echo 'Id : '.$frnd_in['id'].'<br>';
array_push($ids_of_result,$frnd_in['id']);
}
}
}
return $ids_of_result;
}
$ids_of_result = sort_arrays($final_result,$friends_inner);
Array ( [kanye] => Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane [4] => Kaye ) [wst] => Array ( [0] => ST [1] => St [2] => st [3] => EST [4] => West ) )
Array
(
[0] => Kanya
[1] => Janaye
[2] => Kayne
[3] => Kane
[4] => Kaye
)
Array
(
[0] => ST
[1] => St
[2] => st
[3] => EST
[4] => West
)
I've got those two arrays inside one array. The top array holds them both, then below is each one individually. When I am displaying the individual arrays, how do I echo their name?
So the first would be kanye, then list the contents, etc.
Hope that makes sense. I know it will be a simple bit of code but it's stumping me.
You can use a foreach statement to get the key value pair of the array:
$outer_arr = array('kanye' => array('Kanya', 'Janaye', 'Kayne', 'Kane'));
foreach($outer_arr as $key => $val) {
print($key); // "kanye"
print_r($val); // Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane )
}
If you just need to get the keys, you can use array_keys
$myArray = array(
"Kanye" => array("Kane", ...)
"West" => array("Wst", ...)
);
print_r(array_keys($myArray));
/*
array (
0 => Kanye
1 => West
)
*/
How about just print_r on the outer array?