PHP array sorting not working as expected - php

Below is an output of my array
$array1 = Array ( [d] => 5 [e] => 1 [a] => 3 [b] => 3 [c] => 3 [f] => 3 )
I want to sort it like...
Array ( [d] => 5 [a] => 3 [b] => 3 [c] => 3 [f] => 3 [e] => 1)
I am using arsort($array1)
which results in var_dump($array1)
array (size=6)
'd' => int 5
'f' => int 3
'c' => int 3
'a' => int 3
'b' => int 3
'e' => int 1
anyways to fix this?

Try this :
$array1 = [
'd' => 5,
'e' => 1,
'a' => 3,
'b' => 3,
'c' => 3,
'f' => 3
];
array_multisort(array_values($array1), SORT_DESC, array_keys($array1), SORT_ASC, $array1);
print_r($array1);
Here first array_values($array1), SORT_DESC will sort the values in descending order and then array_keys($array1), SORT_ASC will sort the keys into ascending order and finally both the thing applies to the main array i.e. $array1.
O/P - Array ( [d] => 5 [a] => 3 [b] => 3 [c] => 3 [f] => 3 [e] => 1 )
I hope this time I get what you want. Finger crossed !!!

you can work like this its working.
<?php
$array1 = array( "[d]" => 5,"[e]" => 1,"[a]" => 3,"[b]" => 3,"[c]" => 3,"[f]" => 3 );
$a = arsort($array1);
foreach($array1 as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
output:
Key=[d], Value=5
Key=[f], Value=3
Key=[c], Value=3
Key=[a], Value=3
Key=[b], Value=3
Key=[e], Value=1

You can use uasort for this.
$array = array('d' => 5, 'e' => 1, 'a' => 3, 'b' => 3, 'c' => 3, 'f' => 3);
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
uasort($array, 'cmp');
print_r($array);
I tested the code and surely it will work for you.

Related

Convert array values to an array

I can't figure out how to effectively convert the values of this array from a string to array. I really appreciate any suggestion.
array(6) {
["A"]=>
string(31) "['B' => 3, 'C' => 5, 'D' => 9],"
["B"]=>
string(41) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],"
["C"]=>
string(51) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],"
["D"]=>
string(51) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],"
["E"]=>
string(41) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],"
["F"]=>
string(31) "['C' => 3, 'D' => 2, 'E' => 5],"
}
Desired output:
$graph = [
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
];
Seems like you're trying to convert array string to an array.
You can repeat through loop or make function to get your desired output.
I'm using regular expression with preg_match_all
Code
$rawArray = array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
"B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
"C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
"D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
"E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
"F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
);
foreach($rawArray as $k => $v){
preg_match_all("/\'(.)\'/", $v, $key);
preg_match_all("/=> (\d)/", $v, $val);
$graph[$k] = array_combine($key[1], $val[1]);
}
print_r($graph);
Output
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
Live demo
Explanation:
$rawArray is associate array, each of it's element contain string similar to php array.
We're looping through array and converting that string to array by using preg_match_all and building $graph multidimension array.
When loop execute first time $k is equal to A and $v is equal to ['B' => 3, 'C' => 5, 'D' => 9],
First preg_match_all make array of keys from $v (['B' => 3, 'C' => 5, 'D' => 9],), and assign it to $key[1]. Now $key[1] is array ['B', 'C', 'D'].
Second preg_match_all make array of values from $v (['B' => 3, 'C' => 5, 'D' => 9],), and assign it to $val[1]. Now $val[1] is array [2, 5, 9].
We're combining$key[1]as keys and $val[1] as values by using array_combine to the $graph[$k] where $k is A.
How preg_match_all works?
preg_match_all($pattern, $string, $out);
It's matches pattern from string and then assign result to the $out as array.
Learn more about.
preg_match_all
regex pattern cheat sheet
Note: We're using non-capturing pattern so, it's return both exact match and desired match... So our desired record found in$key[1].
This is how you can do it,
<?php
$graph = array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
"B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
"C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
"D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
"E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
"F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
);
foreach ($graph as $key => $value) {
$val = str_replace("[","{",$value);
$val = str_replace("]","}",$val);
$val = str_replace("'",'"',$val);
$val = str_replace("=>",":",$val);
$val = rtrim($val, ',');
$graph[$key] = json_decode($val, true);
}
echo "<pre>";
print_r($graph);
echo "</pre>";
Output
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
A little ugly but I think this finally does the trick.
I downloaded your file and ran this locally so that the source is exactly as you stated. Then I proceeded to parse it and convert the string value to an actual array
Here's how it looks now:
// Parse graph.json file
$json = json_decode(file_get_contents('graph.json'), true);
foreach ($json as $key => $value) {
foreach ($value as $k => $val) {
$str = str_replace(['[', ']'], '', $val);
$str = str_replace(' => ', ',', $str);
$str = str_replace("'", "", $str);
$str = explode(',', $str);
for ($x = 0; $x < count($str); $x = $x + 2) {
$graph[$k][trim($str[$x])] = $str[$x+1];
}
}
}
// Result
echo "<pre>";
print_r($graph);
// Proof it is an array now (result 3)
// echo '<pre>';
// print_r($graph['A']['B']);
Final Result:
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
If you run the below which is your expected output example and then compare the output to my output it is identical:
$graph = [
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
];
echo '<pre>';
print_r($graph);
The proper answer is: don't create such a strange array ;) But since you did, this should do the trick:
//or $graph instead of $result
$result = array_map(function($value) {
//use eval to directly evaluate the string
//we just need to remove the trailing comma
//and add a semicolon
eval('$ret = '.rtrim($value,',').';');
return($ret);
}, $array); // replace $array with the var name of your array!
But remember: eval is evil. If you don't trust the input you need to write your own parser.
Temporary edit for clarification. This is what I get when I run your github example trough json_decode(...,true):
array(6) {
[0]=>
array(1) {
["A"]=>
string(30) "['B' => 3, 'C' => 5, 'D' => 9]"
}
[1]=>
array(1) {
["B"]=>
string(40) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7]"
}
[2]=>
array(1) {
["C"]=>
string(50) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3]"
}
[3]=>
array(1) {
["D"]=>
string(50) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2]"
}
[4]=>
array(1) {
["E"]=>
string(40) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5]"
}
[5]=>
array(1) {
["F"]=>
string(30) "['C' => 3, 'D' => 2, 'E' => 5]"
}
}
This differs from your question.

replace and combine arrays

I'm trying to combine 2 arrays and replace the the $x array cells with the $y cells.
I have this code:
<?php
$x = array (
'a' => '1',
'b' => '2',
'd' => '6'
);
$y = array (
'a' => '3',
'b' => '4',
'c' => '5'
);
how can I get an array like this:
a => 3,
b => 4,
c => 5,
d => 6
?
Thanks.
Use array_merge function.
$z = array_merge($x, $y);
Will output:
Array
(
[a] => 3
[b] => 4
[d] => 6
[c] => 5
)
Use array_merge and make sure you put the array that you want to override first.
So in your case since you want $y to overrride $x use: array_merge($x,$y);

How to merge array and preserve keys?

I have two arrays:
$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
I want to merge them and keep the keys and the order and not re-index!!
How to get like this?
Array
(
[a] => new value
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
[123] => 456
)
I try to array_merge() but it will not be preserved the keys:
print_r(array_merge($array1, $array2));
Array
(
[a] => 'new value'
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
[0] => 456
)
I try to the union operator but it will not overwriting that element:
print_r($array1 + $array2);
Array
(
[a] => 1 <-- not overwriting
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
[123] => 456
)
I try to swapped place but the order is wrong, not my need:
print_r($array2 + $array1);
Array
(
[d] => 4
[e] => 5
[f] => 6
[a] => new value
[123] => 456
[b] => 2
[c] => 3
)
I dont want to use a loop, is there a way for high performance?
You're looking for array_replace():
$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
print_r(array_replace($array1, $array2));
Available since PHP 5.3.
Update
You can also use the union array operator; it works for older versions and might actually be faster too:
print_r($array2 + $array1);
Let suppose we have 3 arrays as below.
$a = array(0=>['label'=>'Monday','is_open'=>1],1=>['label'=>'Tuesday','is_open'=>0]);
$b = array(0=>['open_time'=>'10:00'],1=>['open_time'=>'12:00']);
$c = array(0=>['close_time'=>'18:00'],1=>['close_time'=>'22:00']);
Now, if you want to merge all these array and want a final array that have all array's data under key 0 in 0 and 1 in 1 key as so on.
Then you need to use array_replace_recursive PHP function, as below.
$final_arr = array_replace_recursive($a, $b , $c);
The result of this will be as below.
Array
(
[0] => Array
(
[label] => Monday
[is_open] => 1
[open_time] => 10:00
[close_time] => 18:00
)
[1] => Array
(
[label] => Tuesday
[is_open] => 0
[open_time] => 12:00
[close_time] => 22:00
)
)
Hope the solution above, will best fit your requirement!!
#Jack uncovered the native function that would do this but since it is only available in php 5.3 and above this should work to emulate this functionality on pre 5.3 installs
if(!function_exists("array_replace")){
function array_replace(){
$args = func_get_args();
$ret = array_shift($args);
foreach($args as $arg){
foreach($arg as $k=>$v){
$ret[(string)$k] = $v;
}
}
return $ret;
}
}
array_replace_recursive() or array_replace() is the function you are looking for
$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
var_dump(array_replace_recursive($array1, $array2));
Demo
I think this might help if i understand properly:
foreach ($i = 0, $num = count($array2); $i < $num; $i++)
{
$array = array_merge($array1, $arrar2[$i]);
}

Merge two multidimensional arrays and reindex all subarrays

I have two arrays, I want to merge these two arrays into single array. Please view the detail below:
First Array:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 3
[b] => 2
[c] => 1
)
)
Second Array:
Array
(
[0] => Array
(
[d] => 4
[e] => 5
[f] => 6
)
[1] => Array
(
[d] => 6
[e] => 5
[f] => 4
)
)
I want this result. Does somebody know how to do this?
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[3] => Array
(
[0] => 6
[1] => 5
[2] => 4
)
)
Hope you have understand the question.
Thank you in advance.
Try array_merge:
$result = array_merge($array1, $array2);
FIXED (again)
function array_merge_to_indexed () {
$result = array();
foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}
return $result;
}
Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.
EDIT 03/2014: Improved readability and efficiency
more simple and modern way is:
$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];
new array syntax from php 5.4
If you want to return the exact result you specify in your question then something like this will work
function array_merge_no_keys() {
$result = array();
$arrays = func_get_args();
foreach( $arrays as $array ) {
if( is_array( $array ) ) {
foreach( $array as $subArray ) {
$result[] = array_values( $subArray );
}
}
}
return $result;
}
As a purely native function solution, merge the arrays, then reindex each subarray.
Code: (Demo)
$a = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 3, 'b' => 2, 'c' => 1],
];
$b = [
['d' => 4, 'e' => 5, 'f' => 6],
['d' => 6, 'e' => 5, 'f' => 4],
];
var_export(
array_map('array_values' array_merge($a, $b))
);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 3,
1 => 2,
2 => 1,
),
2 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
3 =>
array (
0 => 6,
1 => 5,
2 => 4,
),
)

php, how to jumble / randomize order of associative array while keeping key/value pairs

what is the php function to randomize the associative array while keeping key/values pairs. I don't mean to just randomly pick out a key value pair, but actually changing the array (similar to the uasort function, but not in order).
TIA
example:
original array
(
[a] => 4
[b] => 8
[c] => -1
[d] => -9
[e] => 2
[f] => 5
[g] => 3
[h] => -4
)
random ordered array
(
[d] => -9
[a] => 4
[b] => 8
[c] => -1
[h] => -4
[e] => 2
[g] => 3
[h] => -4
[f] => 5
)
Edit
Comparison between 2 solutions.
$start = microtime(true);
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
$newArray[$key] = $array[$key];
}
print_r ($newArray);
$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.<br>";
$start = microtime(true);
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$keys = array_keys( $array );
shuffle( $keys );
print_r(array_merge( array_flip( $keys ) , $array ));
$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.<br>";
Array ( [h] => -4 [e] => 2 [b] => 8 [d] => -9 [a] => 4 [c] => -1 [f] => 5 [g] => 3 )
array values took 3.0994415283203E-5 seconds.
Array ( [e] => 2 [a] => 4 [d] => -9 [c] => -1 [g] => 3 [f] => 5 [b] => 8 [h] => -4 )
array values took 4.2915344238281E-5 seconds.
You could use shuffle() on array_keys, then loop around your array adding them to the list in the new order.
E.g.
$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
$newArray[$key] = $array[$key];
}
A comment on shuffle() might do the trick: http://ch2.php.net/manual/en/function.shuffle.php#104430
<?php
function shuffle_assoc( $array )
{
$keys = array_keys( $array );
shuffle( $keys );
return array_merge( array_flip( $keys ) , $array );
}
?>

Categories