When I copy PHP array with reference, copy already has references from original
$arr = [1,2,3];
print_r($arr); echo"<br>";
$x = &$arr[1];
$arr2 = $arr;
print_r($arr); print_r($arr2); echo"<br>";
$x = 8;
print_r($arr); print_r($arr2); echo"<br>";
Result:
Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 8 [2] => 3 ) Array ( [0] => 1 [1] => 8 [2] => 3 )
How can I copy an array, so it has not changed with the original reference?
Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 8 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If your issue is solved using the duplicate link in your question's first comment (supported by 4 others at the time I'm writing this). Please delete your question so that SO can reduce duplicate questions / needless bloat.
Otherwise, just declare a static copy of the original array for future use.
$arr = [1,2,3];
$copy=$arr; // preserve the original
print_r($arr); echo"<br>";
$x = &$arr[1];
$arr2 = $arr;
print_r($arr); print_r($arr2); echo"<br>";
$x = 8;
print_r($arr); print_r($copy); echo"<br>";
Related
I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)
I want to get only one array result like:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
Add all your arrays into one array.
$all_arrays[] =Array
(
[1] => 1 ,
[2] => 1,
[3] => 1
);
$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;
$all_arrays[] = Array
(
[1] => 1
);
$results = [];
foreach($all_arrays as $arr){
foreach($arr as $user_id=>$value){
if(in_array($user_id,$results)){
$results[$user_id] = $results[$user_id] + $value;
}else{
$results[$user_id] = $value;
}
}
}
You would need to specify all arrays in the array_merge:
$result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));
If you can get the arrays dynamically added to another $main_array like:
Array
(
[0] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
[1] => Array
(
[1] => 1
[2] => 1
)
[2] => Array
(
[1] => 1
)
)
Then it would be much easier:
$result = array_count_values(array_keys(array_merge(...$main_array)));
I have this array :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => 11
[id_list] => 2
[id] => 2
)
[1] => Array
(
[0] => 12
[id_list] => 2
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[0] => 13
[id_list] => 4
[id] => 4
)
)
)
and this code (where $dataListe is the result of a fetchAll query) :
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).
I want to remove the duplicate of each element in the general array.
But when I use array unique, I get this result :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
)
Please I need help to only keep 1 item of each array at the end !
EDIT : I have almost the good result with the code below, but the id 12 is missing
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
As the PHP docs says :
Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)
You can try this solution
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
as suggested by #daveilers on this question How to remove duplicate values from a multi-dimensional array in PHP.
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 5 months ago.
I have a two arrays and need to get one from them. array_merge, array_map etc. Doesn't give right result.
$array1 = [1,2,3,4,5];
$array2 = [a,b,c,d,e];
I need $array3 = [[1,a], [2,b], [3,c]]...
What is the best way to get that result?
using array_map(null, $arr1, $arr2) you can achieve the result.
http://php.net/manual/en/function.array-map.php
php > $q = array_map(null, $array1, $array2);
php > print_r($q);
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)
The for loop approach is the following:
<?php
$array1 = [1,2,3,4,5];
$array2 = ['a','b','c','d','e'];
$array3 = array();
if(count($array1) == count($array2))
{
for($i = 0; $i < count($array1); $i++)
{
$array3[] = [$array1[$i],$array2[$i]];
}
}
else
{
die("SIZE MISMATCH");
}
echo '<pre>';
print_r($array3);
And the output is:
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)
I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?
foreach ($my_array as &$val) {
$val[1] -= 3
}
Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}
new here so thanks for taking the time to read my question.
I am running some PHP code that compares the numbers enter on the screen with those in a database. The problem I am having is ordering the two dimensional array after manipulating each line. It looks as though the array id numbers are being removed. I would like to order the array by column [2] in descending order. Can anyone offer any help?
while( $a_row = mysql_fetch_array( $result))
{
$draw = array($a_row['Drawn1'],
$a_row['Drawn2'],
$a_row['Drawn3'],
$a_row['Drawn4'],
$a_row['Drawn5'],
$a_row['Drawn6'],
$a_row['Drawn7'],
$a_row['Drawn8']);
$numbers = array("6", "9", "4", "8", "14", "18");
if (count(array_intersect($draw, $numbers)) >= 1) {
$rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
} else {
$rs = null;
}
array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
print_r ($rs);
echo "<br />";
}
This is what the output looks like.
Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )
Array ( [0] => A0051 [1] => 14 [2] => 1 )
Array ( [0] => A0052 [1] => 18 [2] => 1 )
Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 )
Array ( [0] => A0054 [1] => 6 [2] => 1 )
Array ( [0] => A0055 [1] => 14 [2] => 1 )
Array ( [0] => A0056 [1] => 4 [2] => 1 )
Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 )
Thanks for your time
zeroanarchy
Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.
you are probably looking for:
http://php.net/manual/en/function.array-multisort.php