I need to split single array in to multiple arrays.
For example:
Array
(
[0] => Array
(
[pageviews] => 26
[visits] => 20
)
[1] => Array
(
[pageviews] => 9
[visits] => 4
)
[2] => Array
(
[pageviews] => 18
[visits] => 9
)
)
I need to split the array like below:
Array
(
[ga:pageviews] => 26
[ga:visits] => 20
)
Array
(
[ga:pageviews] => 9
[ga:visits] => 4
)
Array
(
[ga:pageviews] => 18
[ga:visits] => 9
)
How can i do this?
Any help will be thankful and grateful...
Thanks in advance..
Ok, so using foreach:
foreach ( $original as $item ) {
var_dump( array(
'ga:pageviews' => $item['pageviews'],
'ga:visits' => $item['visits'],
) );
}
try
$array = Array
(
[0] => Array
(
[pageviews] => 26
[visits] => 20
)
[1] => Array
(
[pageviews] => 9
[visits] => 4
)
[2] => Array
(
[pageviews] => 18
[visits] => 9
)
)
for($x=0; $x<count($array); $x++){
$newArray = $array[$x]; // that extract the second array, containing pageview and visits.
}
According to your samples, you seem to want to split one variable into multiple variables (or perhaps you used incorrect notation in the 2nd one?). If that is the case, and you know how many arrays are in the starting variable, you can do this:
list($one, $two, $three) = $originalArray;
If you don't know how many arrays are in the original array, or there is more than a handful, I have to wonder why you would want to or need to do this in the first place...
Related
This question already has answers here:
Associative array, sum values of the same key
(5 answers)
Closed 3 years ago.
Following is my array. I need to add its Sum field according to each emp_firstname. Some has only one time coming, some coming two times. how can we sum the field and make the array unique?
Array
(
[0] => Array
(
[emp_firstname] => Alistair
[non_pm] => AMZ
[sum] => 2
)
[1] => Array
(
[emp_firstname] => Shakkeer
[non_pm] => SHK
[sum] => 3
)
[2] => Array
(
[emp_firstname] => Waqas
[non_pm] => WAS
[sum] => 12
)
[3] => Array
(
[emp_firstname] => Zain
[non_pm] => ZAI
[sum] => 9
)
[4] => Array
(
[emp_firstname] => Shakkeer
[gud_pmeditor] => SHK
[sum] => 4
)
[5] => Array
(
[emp_firstname] => Zain
[gud_pmeditor] => ZAI
[sum] => 2
)
)
You can get the desired result using this approach
$res=[];
foreach($arr as $val){
if(array_key_exists($val['emp_firstname'], $res))
$res[$val['emp_firstname']]['sum'] = ($res[$val['emp_firstname']]['sum'] + $val['sum']);
else
$res[$val['emp_firstname']] = $val;
}
Live Demo
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 got 2 arrays(One single and one multidimensional).
Single array "A" looks like
[questionid] => Array
(
[0] => 12
[1] => 13
[2] => 55
[3] => 15
[4] => 16
)
Multidimensional array "B" looks like
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 15
[answer] =>
)
[3] => Array
(
[quid] => 16
[answer] =>
)
[4] => Array
(
[quid] => 55
[answer] =>
)
)
Now I want the array B (quid) values to be rearranged depending upon the values from array A. So in array B the value of quid last element(55) is at the very end whereas in array A it is in 3rd position.
I want the array B look like this
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 55
[answer] =>
)
[3] => Array
(
[quid] => 15
[answer] =>
)
[4] => Array
(
[quid] => 16
[answer] =>
)
)
The code for multidimensional array is
$ansid = array
(
array
(
"quid" => 12,
"answer" => "AAA"
),
array
(
"quid" => 13,
"answer" => "neighbour"
),
array
(
"quid" => 15,
"answer" =>""
),
array
(
"quid" => 16,
"answer" =>""
),
array
(
"quid" => 55,
"answer" =>""
)
);
Not using array_walk() as to be mor demonstrative, you could just
$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
With the user sort function:
$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements
function my_comp($a, $b) {
return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}
usort($mult_dim_array, "my_comp");
This will get the index on your first array to determine which element goes first or later. The function reads $single_array as a global variable (defined outside the function).
Documentation at http://php.net/manual/en/function.usort.php
I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else... I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...
Essentially, I just want to combine an array while keeping the keys?
//Here are the original arrays
[field_sreference] => Array
(
[0] => Array
(
[nid] => 28
)
[1] => Array
(
[nid] => 28
)
[2] => Array
(
[nid] => 29
)
)
[field_idelta] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 1
)
[2] => Array
(
[value] => 0
)
)
[field_iswitch] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 0
)
[2] => Array
(
[value] => 0
)
)
//Here is what I'm trying to achieve:
[combinedarray] => Array
(
[0] => Array
(
[nid] => 28
[idelta] => 0
[iswitch] => 0
)
[1] => Array
(
[nid] => 28
[idelta] => 1
[iswitch] => 0
)
[2] => Array
(
[nid] => 29
[idelta] => 0
[iswitch] => 0
)
)
You can solve this is O(n) by simply iterating the arrays...
$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
$combinedarray[] = array("nid" => $field_sreference[$i]['nid'],
"idelta" => $filed_idelta[$i]['value'],
"iswitch" => $field_iswitch[$i]['value']);
}
This assumes, the 3 arrays are all of equal length.
A bit quickly typed, but this should work:
$result = array();
foreach ($arrays as $array)
{
foreach ($array as $index => $data)
{
$result[$index] += $data;
}
}
As you have not provided some input array in some easy form, you need to test it on your own. Let's say it's pseudo-code and I leave it here as an exercise. The + operator is the array union operator.
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.