I'm trying to rename my keys in a multidimensional array. I looked at this: Renaming the keys in multidimensional associate arrays and it does rename it, but only for 1 of my 2 arrays. How can I get it to rename the 1st array time and the second array count?
My output right now is :
Array
(
[0] => Array
(
[0] => 00:00
[1] => 00:15
[2] => 00:30
)
[1] => Array
(
[0] => 8
[1] => 9
[2] => 8
)
)
I need [0] to be time and [1] to be count.
If I use this:
foreach ($sliced_array as $id => $dataset) {
$newArray["time"] = $dataset;
}
I can get it to output only array [1] renamed as time. It should be count and array [0] disappears entirely. Is there a way to focus which dataset the foreach targets? dataset[0] does not work.
My expected output is:
Array
(
[time] => Array
(
[0] => 00:00
[1] => 00:15
[2] => 00:30
)
[count] => Array
(
[0] => 8
[1] => 9
[2] => 8
)
)
Did you just try:
$myArray["time"] = $myArray[0];
$myArray["count"] = $myArray[1];
unset($myArray[0]);
unset($myArray[1]);
or just:
$newArray["time"] = $myArray[0];
$newArray["count"] = $myArray[1];
?
$newArray = array_combine(array('time','count'),$sliced_array);
http://es1.php.net/array_combine
Related
I am trying to remove duplicate and empty values from array with array_unique() function, but getting wrong output.
Data:
Array (
[0] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
[1] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
[5] => 101
)
[2] => Array (
[0] =>
[1] =>
[2] => 108
[3] =>
)
)
PHP:
$array = array_filter($userids);
$arrays = array_unique($array, SORT_REGULAR);
print_r($arrays);
nothing happens with SORT_REGULAR - output comes same as raw data, and without SORT_REGULAR this output is coming:
$array = array_filter($userids);
$arrays = array_unique($array);
print_r($arrays);
output:
Array (
[0] => Array
(
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
)
output I am looking for:
Array (
[0] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
)
Those array functions only works on a single level. If you flatten the array (adding all elements in a single element array), it should be pretty straight forward.
Flatten the array
$array = array_merge(...$array);
Note: This method works fine for flattening indexed arrays like in your example, but not for associative arrays where any of the sub arrays contains the same keys.
Then filter out all empty
$array = array_filter($array);
and then remove all duplicates
$array = array_unique($array);
Or as a one-liner:
$array = array_unique(array_filter(array_merge(...$array)));
Demo: https://3v4l.org/pEJAJ
http://prntscr.com/fl69px
Hi, how can I get the part shown in the picture? There are many arrays.
Array
(
[max] => 46.784
[total] => 74.562
)
Array
(
[0] => 6
)
Array
(
[0] => 3
)
Array
(
[0] => 18 Oct 2017 14:12
)
Array
(
[0] => 2017-06-18T14:12:33+03:00
)
Array
(
[0] => New Cup
)
In short, I am not asking: New Cup
There are 16 in total.
Assuming that you want this array:
Array(
[bet] => 3
[featureEvent] => 0
[bank] => 0
[column] => 2
[requiredBet] => 2
)
You can create a basic php foreach-loop, which loops through your array and returns each key and value.
Currently we do not know how you created your array nor how the array name is, but lets assume that the array's name above is $bets
The foreach loop would look like this then:
foreach($bets as $betKey => $betValue)
{
// do something ...
// echo the values
echo $betValues;
}
I have a array like below
Array
(
[0] => Array
(
[0] => Date
[1] => Name
[2] => Hours
)
[1] => Array
(
[0] => 2013-01-02
[1] => Test User
[2] => 7:59
)
[2] => Array
(
[0] => 2013-01-03
[1] => Test User
[2] => 7:53
)
[3] => Array
(
[0] => 2013-01-04
[1] => Test User
[2] => 8:12
)
.
.
.
.
[16] => Array
(
[0] =>
[1] => Total
[2] => 103:1
)
[17] => Array
(
[0] =>
)
)
And want to remove last item from array, I have tried array_pop but this is not working after passing above array to array_pop gives me output
Array
(
[0] =>
)
How can I achieve this with minimum code.
Try:
unset ($array_name[count($array_name)-1]);
$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);
This will pop the last element from each triplet.
Try like
$my_cnt = count($my_arr);
unset($my_arr[$my_cnt-1]);
if all your arrays are indexed by numbers from zero to max without any breaks, you can use
unset($ar[count($ar)-1]);
otherwise try
end($ar);
unset($ar[key($ar)]);
You are seeing the "popped" element instead of the modified array.
array_pop() returns the data in the element that it removes from the array.
This means that you wrote:
print_r(array_pop($array));
Instead, modify the array with array_pop(), then print the array.
array_pop($array);
print_r($array);
I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.
So I have an array such as this one:
Array
(
[-1] => Array
(
[3] => 3
[1] => 1
[6] => 6
[7] => 7
[5] => 5
)
)
It also contains some other keys that should not be modified.
I'd like to the numbers which are in a second array to come first (in the order of that second array), and then will be the numbers that don't exist in the second array, if any.
So for that matter, the second array would be:
Array
(
[0] => 6
[1] => 5
[2] => 3
)
And the final array should be as follows (please remember, there are some more keys inside of that array that should stay as they are):
Array
(
[-1] => Array
(
[6] => 6
[5] => 5
[3] => 3
[1] => 1
[7] => 7
)
)
Any ideas how that can be done?
Thanks!
It's not and shouldn't be termed as sorting but may be this code snippet may help you do what you want to:
$a1 = Array ( [-1] => Array ( [3] => 3 [1] => 1 [6] => 6 [7] => 7 [5] => 5 ) );
$a2 = Array ( [0] => 6 [1] => 5 [2] => 3 );
$sorted = getSortedArray($a1[-1] , $array2);
function getSortedArray($array1 , $array2){
$temp = Array();
$count = 0;
$totalKeys = sizeof($array2);
for($i=0;$i<sizeof($array2);$i++){
$temp[i] = $array1[$array2[i]];
unset($array1[$array2[i]]);
}
while($count!=sizeof($array1))
$temp[$totalKeys++] = $array1[$count++];
return $temp;
}
I believe the function you're looking for is called array_multisort().
array_multisort() can be used to sort
several arrays at once, or a
multi-dimensional array by one or more
dimensions.