This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
I have an array like this:
[12601] => Array (
['docUpload'] => html dom.txt
)
[12602] => Array (
['docUpload'] => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
)
[12603] => Array (
['docUpload'] =>
)
How to get it like this:
12601 => html dom.txt
12602 => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
can you help me please?
Use array_column() to get the values, then combine them with array_combine() and array_keys().
$values = array_column($array, 'docUpload');
$newArray = array_combine(array_keys($array), $values);
Live demo at https://3v4l.org/lG4KO
You can loop over the array by foreach()
Steps:
1) Take a new blank array. We are appending our results into this.
2) If the array is not empty, loop over the array by foreach
3) Use key value pairs. Key is the id in required array.
4) Value is an array with key docUpload to be the document name.
5) Append new element with id and value (docUpload).
6) Resulting array will be a single dimensional array.
Final Code:
$arr = [];
$arr[12401] = ['docUpload' => ''];
$arr[12601] = ['docUpload' => 'html dom.txt'];
$arr[12602] = ['docUpload' => 'PYTHON AND DJANGO ARE HUGE IN FINTECH.txt'];
$arr[12603] = ['docUpload' => ''];
$newArr = [];
if (! empty($arr)) {
foreach ($arr as $id => $docArr) {
$newArr[$id] = $docArr['docUpload'];
}
}
echo '<pre>';print_r($newArr);echo '</pre>';
Output:
Array
(
[12401] =>
[12601] => html dom.txt
[12602] => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
[12603] =>
)
Working Link:
You need to do as follows:
foreach ($arrayData as &$value) {
$value = isset($value['docUpload']) ? $value['docUpload'] : '';
}
It will result in following array:
[docUpload] => [
[12601] => html dom.txt
[12602] => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
];
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have two types of arrays:
1:
$array1["a"][] = "value1";
$array1["a"][] = "value2";
$array1["b"][] = "value3";
2:
$array2["0"] = "a";
What I need now is to somehow find difference between these two arrays. I need to filter out array1 by key, which is located in array2 value. I have tried doing the following:
array_diff(array_keys($array1), array_values($array2));
But I get the following error on that line:
ErrorException Array to string conversion
Any ideas?
Something like this?
foreach ($array1 as $key => $value)
if( array_search ($key , $array2 ))
unset($array1[$key]);
If $array1 needs to have the values, you just need to put the diff in $array1 :
$array1 = array_diff(array_keys($array1), array_values($array2));
Depending on how you constructed your arrays, it should work. The following code (based on your question) worked:
<?php
$array1=array("a" => array(),"a" => array(),"b" => array());
$array2=array("0"=>"a");
print_r(array_keys($array1));
echo("<br/>");
print_r(array_values($array2));
echo("<br/>");
print_r(array_diff(array_keys($array1), array_values($array2)));
>
This results in:
Array ( [0] => a [1] => b )
Array ( [0] => a )
Array ( [1] => b )
This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 8 years ago.
I have array A :
Input:
A ={2,3,2,{1},3,2,{0},3,2,0,11,7,9,{2}}
I want output to be Array B
Output:
B={0,1,2,3,7,9,11}
How can i remove the duplicate values and sort them ascending with PHP?
if you have:
$a = array(2,3,2,1,3,2,0,3,2,0,11,7,9,2);
you can use array_unique() to remove duplicates:
$a = array_unique($a);
and then use asort() to sort the array values:
asort($a);
//Try this out...
$array = array(2,3,2,(1),3,2,(0),3,2,0,11,7,9,(2));
$array_u = array_unique($array);
sort($array_u);
print_r($array_u);
Sample output
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 7
[5] => 9
[6] => 11
)
First step: flattern
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}
then using asort and array_unique you can remove duplicates and sort ascendent.
$result = array_unique(flattern($array));
asort($result);
Sources:
How to Flatten a Multidimensional Array?
This question already has answers here:
Remove duplicates from Array
(2 answers)
Closed 9 years ago.
I have an array like this
Array
(
[0] => u1,u2
[1] => u2,u1
[2] => u4,u3
[3] => u1,u3
[4] => u1,u2
)
I want to remove similar values from the array
I want an out put like
Array
(
[0] => u1,u2
[1] => u4,u3
[2] => u1,u3
)
I tried to loop thru the input array, sort the value of the indexes alphabetically and then tried array_search to find the repeated values. but never really got the desired output
any help apprecated
You cannot use array_unique() alone, since this will only match exact duplicates only. As a result, you'll have to loop over and check each permutation of that value.
You can use array_unique() to begin with, and then loop over:
$myArray = array('u1,u2', 'u2,u1', 'u4,u3', 'u1,u3', 'u1,u2');
$newArr = array_unique($myArray);
$holderArr = array();
foreach($newArr as $val)
{
$parts = explode(',', $val);
$part1 = $parts[0].','.$parts[1];
$part2 = $parts[1].','.$parts[0];
if(!in_array($part1, $holderArr) && !in_array($part2, $holderArr))
{
$holderArr[] = $val;
}
}
$newArr = $holderArr;
The above code will produce the following output:
Array (
[0] => u1,u2
[1] => u4,u3
[2] => u1,u3
)
Use array_unique() PHP function:
http://php.net/manual/en/function.array-unique.php
Use the function array_unique($array)
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
php manual
since u1,u2 !== u2,u1
$array=array('u1,u2','u2,u1','u4,u3','u1,u3','u1,u2');
foreach($array as $k=>$v)
{
$sub_arr = explode(',',$v);
asort($sub_arr);
$array[$k] = implode(',',$sub_arr);
}
$unique_array = array_unique($array);
//$unique_array = array_values($unique_array) //if you want to preserve the ordered keys
This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I have array that i had to unset some indexes so now it looks like
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
as you can see [2] is missing all i need to do is reset indexes so they show [0]-[3].
Use array_values.
$myarray = array_values($myarray);
$myarray = array_values($myarray);
array_values
array_values does the job :
$myArray = array_values($myArray);
Also some other php function do not preserve the keys, i.e. reset the index.
This might not be the simplest answer as compared to using array_values().
Try this
$array = array( 0 => 'string1', 2 => 'string2', 4 => 'string3', 5 => 'string4');
$arrays =$array;
print_r($array);
$array=array();
$i=0;
foreach($arrays as $k => $item)
{
$array[$i]=$item;
unset($arrays[$k]);
$i++;
}
print_r($array);
Demo