PHP: Splitting an array into a deeper array - php

I have the following array:
Array(
[0] => 0,0
[1] => 0,1
[2] => 0,2
[3] => 0,3
[4] => 1,0
[5] => 1,1
[6] => 1,2
[7] => 2,0
[8] => 2,1
[9] => 2,2
[10] => 2,3
)
And I would like to split it into the following structure:
Array(
[0] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[2] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[3] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
i.e., in the "X,Y" value, X corresponds to the position of value "Y" in the new array. I can't seem to get my head around the logic, its my first using 'foreach' in such a way.
Any help would be greatly appreciated pointing me in the right direction.

The input and output arrays in your example don't quite match up, but I'm guessing you're looking for this:
$array = array(…as above…);
$newArray = array();
foreach ($array as $value) {
list($x, $y) = explode(',', $value);
$newArray[$x][] = $y;
}

Related

merge and count in multidimensional array

I have an array
Array
(
[array_name_1] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
)
[array_name_2] => Array
(
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => 1
)
)
I want to merge and calculate the number of arrays above so that it becomes
array
(
[array_name_1] => 5
[array_name_2] => 6
)
can anyone help to provide a solution? thank you
Simply use foreach loop
$finalArr = [];
foreach($yourMainArr as $key => $arr){
$finalArr[$key] = count($arr);
}
print_r($finalArr);

Multiplying 2 array multidimensional and remove key

I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?
The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)

PHP convert associative array into indexed array

So long story short, I have the array $total which looks like this:
Array (
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
However I need to pass it to a function that looks like this:
$count = cartesian(
Array("GH20"),
Array(1,3),
Array(6,7,8),
Array(9,10)
);
I have asked a similar question but I suspect the answer they gave was correct, I do not think asked the question in the right way. The cartesain function does not seem to accept:
$count = cartesian(
Array($total[0]),
Array($total[1]),
Array($total[2])
);
After some digging around I believe this is down to the fact that the arrays have keys which is not causing an error however instead of any values being returned, it just returns "array" for everything.
My question, in two parts then, is how do I remove the keys from the arrays so it is formatted as required. If possible, could the be done dynamically?
I have tried what everybody suggestes on google which is use the array_values() function, but this did not help
Any help is greatly appreciated, thanks, Nick
As requested, casertain function:
function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
This is my desired output (Every permutation of ($total):
Array (
[0] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 9
)
[1] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 10
)
[2] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 9
)
[3] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 10
)
[4] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 9
)
[5] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 10
)
[6] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 9
)
[7] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 10
)
[8] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 9
)
[9] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 10
)
[10] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 9
)
[11] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 10
)
)

PHP remove entry if array value equals to 0

This is what i get when i print_r my array. it's a multi-dimensional array which contains the following values.
[7] => Array
(
[0] => 1
[1] => 34
[2] => 181
[3] => 50
)
[9] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 47
)
[2] => Array
(
[0] => 20
[1] => 0
[2] => 1621
[3] => 45
)
[3] => Array
(
[0] => 120
[1] => 0
[2] => 121
[3] => 45
)
I would like to remove all entries in which the key [1] equals to 0. After doing the modifications, My final array should like this
[7] => Array
(
[0] => 1
[1] => 34
[2] => 181
[3] => 50
)
[9] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 47
)
Any ideas ?
foreach to the rescue:
foreach($arr as $key => $entry) {
if(isset($entry[1]) && $entry[1] === 0) {
unset($arr[$key]);
}
}
And an array_filter example:
$arr = array_filter($arr, function($entry) {
return $entry[1] !== 0;
});
(assumes at least php 5.3, though you can get around that by creating a named function and passing that as the second parameter to array_filter)
If you want only remove array with value 0 whatever the key, you can use array_filter .
<?php
$array = array(1,2,3,4,5,0,'',null);
print_r(array_filter($array));
?>
Output :
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
You can remove empty,null and 0 value from array using this code
Code:-
$array = array('one', 'two', '', 'three', null,'four','0');
$filteredarray = array_values( array_filter($array) );
print_r($filteredarray);
Output:-
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
Thank You!! All The Best!!

Sum specefic values in multidimensional array PHP

I have this array and I want to sum up all Hits which figures at the $day[$key][2]
Array
(
[1] => Array
(
[0] => 01/07/13
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 02/07/13
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 03/07/13
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 04/07/13
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on .I've tried this and it doesn't seem to work!
foreach ($day as $key => $value){
$day[$key][2] += $day[$key][2];
}
Any errors in my code ? Thanks
Do it with:
$result = array_reduce($array, function(&$cur, $x)
{
return $cur+=$x[2];
}, 0);
You code also has correct logic, but you shouldn't try to modify existing array values. Just sum up into some result variable.
Something like this ?
$hits=0;
foreach($arr as $k=>$arr)
{
$hits+=$arr[2];
}
echo $hits; //"prints" 1362
Demo

Categories