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 )
Related
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
How can I convert a multidimensional array like the one below
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
into a single line array like the one below?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
Here is simple code to work around,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
Here is the working link
array_walk — Apply a user supplied function to every member of an array
The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)
Code: (Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
The non-variadic version:
Code: (Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
Input:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
Output from either method:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
Nearly exact duplicate page: Combining array inside multidimensional array with same key
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);
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:
Closed 11 years ago.
Possible Duplicate:
How to Flatten a Multidimensional Array?
THe array:
itemA => value
itemA1 => value
itemA2 => value
itemA11 => value
itemB => value
and so on...
How can I get the values gathered like this:
[0] => value (from itemA)
[1] => value (from itemA1)
[2] => value (from itemA2)
[3] => value (from itemA11)
[4] => value (from itemB)
...
?
By writing your own array function, using array_walk() ?
Or, i dont know, but maybe with array_values() ?
edit: i found this:
$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
echo $v, " ";
}