This question already has answers here:
How to reindex an array?
(6 answers)
Closed last year.
i'm having trouble with how array works.
$a = array("24","33","12");
$b = array("24","12");
$c = array_intersect($a,$b);
echo json_encode($c);
// {"0":"24","2":"12"}
I expect the output to be like this:
// ["24","12"]
How can i achieve that output?
echo json_encode(array_values($c));
output
["24","12"]
array_intersect Computes the intersection of arrays
<?php
$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);
var_dump(array_intersect($array1, $array2));
var_dump(array_intersect($array2, $array1));
?>
will yield
array(3) {
[0]=> int(2)
[1]=> int(4)
[2]=> int(6)
}
array(3) {
[1]=> int(2)
[3]=> int(4)
[5]=> int(6)
}
. json_encode returns string containing the JSON representation of value. For example
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
will output
{"a":1,"b":2,"c":3,"d":4,"e":5}
As mamta answered you can use array_values to return all the values of an array and json_encode it like
echo json_encode(array_values($c))
Related
So I have a number of arrays. Let's say I have 3 arrays.
$array1 = array( 1, 2, 3 );
$array2 = array( 2, 3, 4 );
$array3 = array( 2, 5, 6 );
So 2 is in all the arrays. So I want it to be 1st value of new merged array, then 3 is common in array1 & array2. I want result as below:
array( 2, 3, 1, 4, 5, 6 )
merge the arrays (array_merge)
count the values (array_count_values)
sort the values (reversed) and keep the keys (arsort)
get the keys (array_keys)
$array1 = array( 1, 2, 3 );
$array2 = array( 2, 3, 4 );
$array3 = array( 2, 5, 6 );
$ar = array_merge($array1, $array2, $array3);
$counts = array_count_values($ar);
arsort($counts);
$output = array_keys($counts);
var_dump($output);
Your output will be:
array(6) {
[0]=>
int(2)
[1]=>
int(3)
[2]=>
int(1)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
}
It should resolves your problem:
$array1 = array( 1, 2, 3 );
$array2 = array( 2, 3, 4 );
$array3 = array( 2, 5, 6 );
$merged = array_merge($array1, $array2, $array3);
$result = array();
foreach($merged as $item) {
if(!isset($result[$item])) {
$result[$item] = 0;
}
$result[$item]++;
}
arsort($result);
print_r(array_keys($result));
I want to make some encryption and write numbers
I used:
$a = [100,101,102,103,104,105]
function decrition (array $a){
return preg_replace('/101/','a',$a);
}
And it's returns me all letters "a" for each 101 in array.
How can I change next? 101 to "b", 102 to "c" etc.
return preg_replace('[101|102|103|104|105]','a',$a);
this method replace all this numbers to letter "a"
return preg_replace('[101|102|103|104|105','a|b|c|d|e',$a);
unfortunately it's not working
Why do you try to treat it as a string?
<?php
$a = [ 101, 102, 103 ];
$replace_array = array(101 => "a", 102 => "b");
$b = array_map(function($val) use ($replace_array) {
return (isset($replace_array[$val]) ? $replace_array[$val] : $val);
}, $a);
var_dump($a, $b);
Gives the following output:
array(3) {
[0]=>
int(101)
[1]=>
int(102)
[2]=>
int(103)
}
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
int(103)
}
Maybe you are looking for something like this?
$test = str_replace($a, array('a','b','c','d','e','f'), $a);
print_r($test);
This solution works
return str_replace(['101', '102', '103', '104', '105'], ['a', 'b', 'c', 'd', 'e'], $a);
I have some problems during working with arrays using array_merge function. Here an example:
First example:
$first = array('01' => 1, '03' => 73);
$second = array('14'=>11, '15' => 23);
var_dump(array_merge($first, $second));
Result is:
array(4) { ["01"]=> int(1) ["03"]=> int(73) [0]=> int(11) [1]=> int(23) }
Expected:
array(4) { ["01"]=> int(1) ["03"]=> int(73) [14]=> int(11) [15]=> int(23) }
Second example:
$first = array('01'=>3, '03'=>10);
$second = array('05'=>44, '07'=>3);
var_dump(array_merge($first,$second));
Result is(as expected):
array(4) { ["01"]=> int(3) ["03"]=> int(10) ["05"]=> int(44) ["07"]=> int(3) }
Third example:
var_dump(array_merge(array("somekey"=> array("some value"))));
Result is(as expected):
array(1) { ["somekey"]=> array(1) { [0]=> string(10) "some value" } }
Fourth example:
var_dump(array_merge(array("34"=> array("some value"))));
Result is:
array(1) { [0]=> array(1) { [0]=> string(10) "some value" } }
Expected:
array(1) { [0]=> array(1) { ["34"]=> string(10) "some value" } }
var_dump(array_merge(array("34"=> array("some value"))));
As you can see from third and fourth examples I set string for keys but the result was not as expected.
What is wrong or incorrect of understanding?
Thanks for helping.
Edited. Why (example first and second) the result is different, but the keys are string and consist of only with digest?
Quoting from the manual:
If the input arrays have the same string keys, then the later value
for that key will overwrite the previous one. If, however, the arrays
contain numeric keys, the later value will not overwrite the original
value, but will be appended.
Values in the input array with numeric keys will be renumbered with
incrementing keys starting from zero in the result array.
Keys with a leading zero are being treated as strings, while numeric keys without a leading zero are being treated as numeric
If you want to retain the keys exactly as they are:
$first = array('01' => 1, '03' => 73);
$second = array('14'=>11, '15' => 23);
var_dump(
array_combine(
array_merge(
array_keys($first), array_keys($second)
),
array_merge(
$first, $second
)
)
);
as long as keys are unique between $first and $second
Definition : Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
INPUT
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
OUTPUT
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Hope it helps:)
Try array_merge() like below:-
$first = array('01' => 1, '03' => 73);
$second = array('14'=>11, '15' => 23);
$output = array_merge($first, $second);
print_r($output);
EDIT:-
$first = array('01' => 1, '03' => 73);
$second = array('14'=>11, '15' => 23);
$output = $first + $second;
print_r($output);
I have an array with string indices, that I need partially sorted. That is, some elements must be moved first, but the others should remain untouched in their current (PHP-internal) order:
# The element with key "c" should be first
$foo = array(
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
);
uksort($foo, function ($a, $b) {
if ($a === "c") {
return -1;
} elseif ($b === "c") {
return 1;
}
return 0;
});
var_dump($foo);
What I expected:
array(4) { ["c"]=> int(3) ["a"]=> int(1) ["b"]=> int(2) ["d"]=> int(4) }
//--------------------------^ "a" remains first of the unsorted ones
What I got:
array(4) { ["c"]=> int(3) ["d"]=> int(4) ["b"]=> int(2) ["a"]=> int(1) }
//--------------------------^ "d" moved above "a"
This seems due to the sorting algorithm uksort() uses internally, which destroys the fragile order of elements. Is there any other way to achieve this sorting?
Using any sort function is overkill for this task. You merely need to merge the input array into a lone array containing the element which should come first. The array union operator (+) will do nicely for your associative array (otherwise array_merge() will do).
Codes: (Demos)
if key c is guaranteed to exist:
$foo = array(
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
);
$foo = ['c' => $foo['c']] + $foo;
var_export($foo);
if key c might not exist check for it first:
$bar = array(
"a" => 1,
"b" => 2,
"d" => 4,
);
if (array_key_exists('c', $bar)) {
$bar = ['c' => $bar['c']] + $bar;
}
var_export($bar);
Output:
array (
'c' => 3,
'a' => 1,
'b' => 2,
'd' => 4,
)
and
array (
'a' => 1,
'b' => 2,
'd' => 4,
)
This worked for me and returned:
array(4) { ["c"]=> int(3) ["a"]=> int(1) ["b"]=> int(2) ["d"]=> int(4) }
<?php
# The element with key "c" should be first
$foo = array(
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
);
uksort($foo, function ($a, $b) {
if ($a === "c") {
return -1;
} else
return 1;
});
var_dump($foo);
?>
Given two arrays:
$foo = array('a', 'b', 'c');
$bar = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
Is there a built-in PHP function to produce the following result array?
$result = array('a' => 1, 'b' => 2, 'c' => 3);
I've been through the Array Functions list on php.net, but can't seem to find what I'm looking for. I know how to do it myself if need be, but I figured this might be a common-enough problem that there might be a built-in function that does it and didn't want to reinvent the wheel.
Another way using array_flip and array_intersect_keys:
$foo = array('a', 'b', 'c');
$bar = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$common = array_intersect_key($bar, array_flip($foo));
Output
array(3) {
["a"]=>
int(0)
["b"]=>
int(1)
["c"]=>
int(2)
}
It's a bit of a dirty hack, but it works:
function extractKeys($keys, $data) {
extract($data);
return call_user_func_array('compact', $keys);
}
$foo = array('a', 'b', 'c');
$bar = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
var_dump(extractKeys($foo, $bar));
Output:
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
After posting, I thought of one way of doing this:
array_intersect_key($bar, array_fill_keys($foo, NULL))
Though, this isn't really the concise, built-in function that I had hoped for, it's definitely better than constructing the resultant array manually.