This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed 6 years ago.
I wonder why I don't get the 3 elements of my array.
$array1 = array(
"One" => 1,
"Two" => 2,
"One" => 1
);
When I print it:
echo 'array1:<pre>'; print_r($array1); echo '</pre>';
I get this:
array1:
Array
(
[One] => 1
[Two] => 2
)
This is not what I want. I need to show the following:
array1:
Array
(
[One] => 1
[Two] => 2
[One] => 1
)
Any help wil be appreciated.
Thanks in advance
Your array is a set of key/value pairs. Think of it as a dictionary:
array( "elephant" => "Big grey animal with tusks",
"canary" => "Little Yellow Bird",
"elephant" => "Candy that tastes like Skittles"
)
When you print this one, you will always get the second definition of "elephant", just like in your code. Try changing the second "one" => 1 to "one" => 77.
Because PHP reads from top-down, the last one will always be the final answer, just like in variables.
You can't have duplicate keys in an array. So by declaring the array['one'] you might replace the old value set before for key array['one'].
Use array of array..I think you are trying to achieve that..
But this looks weird
array( ['one'] => array(1,3), ['two']=> 2);
Related
I want to need multiple array combaine in one array
I have array
array(
0=> test 1
)
array(
0=> test 2
)
array(
0=> test 3
)
I need expected output
`array(
0=>Test1
1=>Test2
2=>test3
)`
You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.
Syntax:
array_merge(array ...$arrays): array
Example:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
You can check more here.
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));
O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )
Hope you are doing well and good.
So, as per your requirement i found solution to get result.
$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];
print_r(array_merge($array1,$array2,$array3));
In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).
What is array_merge()?
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 1 year ago.
Consider this array:
Array( [Wingspan] => 5
[Scythe] => 1
[Spirit Island] => 2
[Everdell] => 1)
How can I sort this array with the highest value(5) as first, then 2 then lowest (1) as the last? I used this:
print_r(rsort($_POST['item']), SORT_NUMERIC);
But I get this:
Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
)
It changes the key, but this is wrong. I expect to get this:
Array( [Wingspan] => 5
[Spirit Island] => 2
[Scythe] => 1
[Everdell] => 1)
I searched and found this: Sort an Array by numerical value but that did not help.
Use the method
arsort($_POST['item'])
instead of rsort. rsort only works for simple arrays, not for associative ones. But anyways usually if the order of te items matters to you, probably an associative array is not the best choice. You could use a simple array with objects inside containing the values and the keys at once
I would like to convert a strings array into an array that counts the occurrences of each string and store it in the new array in the string index.
[a][a][b] will turn into an `array("a" => "2", "b" => "1")
EDIT: I don't know the original array values. I didn't wrote any code yet since I don't know how to approach this problem.
Try this: use array_count_values.for more visit here:http://php.net/manual/en/function.array-count-values.php
$arr=array("a","a","b");
print_r(array_count_values($arr));
output:
Array
(
[a] => 2
[b] => 1
)
array_count_values() best suits your need ! It does what you need, meaning counting occurences of element in your array, creating another array in result containing for keys the element, and for values the number of this element.
(Exemple taken from PhP documentation)
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
will result in an array containing :
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
This question already has answers here:
How to re-index all subarray elements of a multidimensional array?
(6 answers)
Closed 2 years ago.
I use a PHP array to store data about all the people a user is following on a website. Here is an example of how I have it set up:
$data = array(
['user1'] => array(
[0] => 'somedata',
[1] => 'moredata',
[2] => array(
[0] => 'Jim',
[1] => 'Bob',
[2] => 'Nick',
[3] => 'Susy',
)
),
);
As you can see, it is $data[user][2] that lists all the friends. The array has this exact appearance with [0] and [1] for keys because that is how var_export() does it. Now my problem is this. When someone unfollows somebody, I use unset() to delete that friend from the array. So if I want to unfollow Bob in the example above, it would be left with Jim, Nick, and Susy.
The only issue now is that the array keys do not renumber properly when they rename. So once Bob is gone it goes from 0 to 2 rather than Nick taking on the array key of 1. Now I can think of ways to do this myself but I would highly prefer if there were some PHP function specifically for solving this issue, that is, renaming these array keys to the proper numerical order. I checked out the sort() function but that seems for alphabetizing array values not keys.
You can use array_values to re index the array numerically.
$newArray = array_values($array);
If you just want to re-index the array at that level, you could simply use array_values();
For example, assuming you are removing the "bob" entry, just call array_values at the level directly above bob after removing it.
unset($data['user1'][2][1]);
$data['user1'][2] = array_values($data['user1'][2]);
I'd use array_values like this:
$data['user1'][2]=array_values($data['user1'][2]);
Here's the full code:
$data = array(
'user1' => array(
'somedata',
'moredata',
array(
'Jim',
'Bob',
'Nick',
'Susy',
)
),
);
unset($data['user1'][2][1]);
var_export ($data['user1'][2]);
echo "\n\n";
$data['user1'][2]=array_values($data['user1'][2]);
var_export($data['user1'][2]);
Result
array (
0 => 'Jim',
2 => 'Nick',
3 => 'Susy',
)
array (
0 => 'Jim',
1 => 'Nick',
2 => 'Susy',
)
See it in action here:
Sandbox
You could use array_splice
$removedElement = array_splice($data['user1'][2], $indexOfUserToRemove, 1);
This alters the original array reindexing it, but only if the keys of the array are numeric.
This question already has answers here:
Remove item from array if it exists in a 'disallowed words' array
(2 answers)
What is the most efficient way to remove all the elements of one array from another array?
(3 answers)
Closed 8 years ago.
I have a very simple PHP question.
Imagine i have two array :
[array1] {
[0] => zero
[1] => one
[2] => two
[3] => three
}
and
[array2] {
[0] => zero
[1] => test1
[2] => test2
[3] => three
}
I want to delete every value from the second array that is in the first one.
For example, from that two arrays at top, I want to have this below array ::
[array2] {
[0] => test1
[1] => test2
}
How can we do it in PHP ?
Thanks in advance.
You can use array_diff():
$array2 = array_diff($array2, $array1);
Edit: Here is an example:
$array1 = array('zero', 'one', 'two', 'three');
$array2 = array('zero', 'test1', 'test2', 'three');
$array2 = array_diff($array2, $array1);
print_r($array2);