Filtering array result - php

My array :
<?php
$hoppa = array
(
"0" => array
("0","0","0","0","0","0","0","0","0","0"),
"1" => array
("0","0","0","0","0","0","0","0","0","0"),
"2" => array
("1","0","0","1","0","0","0","0","0","0"),
"3" => array
("1","0","0","1","0","1","1","1","1","0"),
"4" => array
("1","1","1","1","0","0","0","0","1","0"),
"5" => array
("1","0","0","1","0","1","1","1","1","0"),
"6" => array
("1","0","0","1","0","1","0","0","1","0"),
"7" => array
("1","0","0","1","0","1","1","1","1","0"),
"8" => array
("0","0","0","0","0","0","0","0","0","0"),
"9" => array
("0","0","0","0","0","0","0","0","0","0")
);
?>
My array's output:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[2] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[3] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[5] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[6] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 0
[7] => 0
[8] => 1
[9] => 0
)
[7] => Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 0
)
[8] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
[9] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
)
it will give 0 (zero) as result
echo $hoppa[1][1];
it will give 1 (one) as result
echo $hoppa[2][0];
I dont want 0 (zero) values to show in the results. I want php script to return only 1 as results. Which functions should use? or can you give me a sample?

Use:
foreach($hoppa as $k => $v) {
$hoppa[$k] = array_filter($v);
}
This results in:
php > print_r($hoppa);
Array
(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
[0] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[4] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[8] => 1
)
[5] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[6] => Array
(
[0] => 1
[3] => 1
[5] => 1
[8] => 1
)
[7] => Array
(
[0] => 1
[3] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
)
[8] => Array
(
)
[9] => Array
(
)
)

Related

How do I combine two arrays, one as a key and the other as an array?

The output of the first array as the value for the update in the third array
Array
(
[0] => Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 1
[3] => 1
[4] => 1
[5] => 1
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => 0
[3] => 0
[4] => 0
[5] => 1
)
)
The output of the second array as a key for updating the third array
Array
(
[0] => Array
(
[0] => 4
)
[1] => Array
(
[0] => 5
)
[2] => Array
(
[0] => 6
)
[3] => Array
(
[0] => 7
)
[4] => Array
(
[0] => 8
)
[5] => Array
(
[0] => 9
)
This is the third array with 48 keys
Array
(
[1] => Array
(
[1] => 1
)
[2] => Array
(
[1] => 1
)
[3] => Array
(
[1] => 1
)
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => Array
(
[4] => 1
)
[11] => Array
(
[4] => 1
)
[12] => Array
(
[4] => 1
)
[14] => Array
(
[5] => 1
)
[15] => Array
(
[5] => 1
)
[17] => Array
(
[6] => 1
)
[18] => Array
(
[6] => 1
)
[20] => Array
(
[7] => 1
)
[21] => Array
(
[7] => 1
)
[23] => Array
(
[8] => 1
)
[24] => Array
(
[8] => 1
)
[27] => Array
(
[9] => 1
)
[29] => Array
(
[10] => 1
)
[30] => Array
(
[10] => 1
)
[32] => Array
(
[11] => 1
)
[33] => Array
(
[11] => 1
)
[34] => Array
(
[12] => 1
)
[36] => Array
(
[12] => 1
)
[38] => Array
(
[13] => 1
)
[39] => Array
(
[13] => 1
)
[42] => Array
(
[14] => 1
)
[44] => Array
(
[15] => 1
)
[45] => Array
(
[15] => 1
)
[46] => Array
(
[16] => 1
)
[47] => Array
(
[16] => 1
)
[48] => Array
(
[16] => 1
)
The problem is that I can not use one as a key and the other as a value to update the third array
Code I have tried :
foreach($arraykey as $key => $value){
for ($i=0;$i < count($array3) ; $i++){
if ($value2==$i){
$array3[$value2]=$arrayval[0][0][$i];
}
}
}
You can use array_combine(), for example like this:
<?php
$a = [1,2,3,4];
$b = [4,5,6,7];
$c = array_combine($b,$a);
print_r($c);
?>
result:
Array (
[4] => 1
[5] => 2
[6] => 3
[7] => 4 )

Php array remove duplicate and put toghether in another array

I have this php array:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 1
)
[1] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[2] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 0
[10] => 0
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[4] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 0
)
)
I want i new array:
1) if element [0] exists in the array I want to control elements 8, 9 and 10 and if one of this is 1 I want to have 1 in the final array, but I don't want to have the same array 2 times (the index [0] is the key).
My final array shound be:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 1
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 0
)
)
I have only one time the index 2505 and 2505 and index 8 and 9 and 10 are 1 for the 2505 and the index 8 and 9 are 1 for the 2525.
this does the job: (assuming that your first array is $arrays and your desired result is $result)
$result = array();
foreach($arrays as $array)
{
if(!isset($result[$array[0]]))
{
$result[$array[0]] = $array;
}
else
{
$result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8];
$result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9];
$result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
}
}
but wherever you get your data from, e.g. a database, I would use element[0] as the key, so you don't get multiple entries in $arrays. Another option would be to create a class which holds your data...

Parsing a Two level - array, PHP

What I have
I have two-level array as follows:
<?php $myarray = Array (
[0] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[1] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[2] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[3] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 )
[4] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 )
[5] => Array ( [1] => 1 [2] => 0 [3] => 0 [4] => 0 )
[6] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[7] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[8] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[9] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[10] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[11] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[12] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[13] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 )
[14] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => red )
[15] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[16] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[17] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[18] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => red )
[19] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[20] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[21] => Array ( [1] => 0 [2] => 1 [3] => 0 [4] => 0 [color] => yellow )
[22] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[23] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
[24] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 0 [color] => yellow )
[25] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => red )
[26] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 1 [color] => yellow )
) ?>
The Inner arrays (index 0 - 26 ) contain 4 or 5 entries each corresponding to positions 1,2,3,4 and entry 5 which can be red, yellow or green. A position can be 0 or 1.
What I want to do:
1) I want to add all values at position 1, position 2, position 3 and position 4 of the inner arrays (Total of a column )
2) If any inner array has more than one "position" that is 1, the whole "row" (entry) needs to be flagged and "unset from $myarray
If it is not clear, please ask some more and I will try my best to clarify.
Thanks
total of a column is pretty simple by using array_sum() combined with array_column():
$position1Sum = array_sum(array_column($myarray,'1'));
$position2Sum = array_sum(array_column($myarray,'2'));
$position3Sum = array_sum(array_column($myarray,'3'));
$position4Sum = array_sum(array_column($myarray,'4'));
to clean the array of anything with more than one position active:
/* editing the same array you are looping thru is typically bad - lets make a copy first.*/
$cleanArray = $myarray;
/* loop thru - and unset the rows where more than one position are 1
(which will make their sum more than 1) */
foreach($myarray as $key => $value){
if($value[1] + $value[2] + $value[3] +$value[4] > 1){
unset($cleanArray[$key]);
}
}
$cleanArray will now contain the same contents as $myarray, minus the rows that have more than one position set to 1. Of course there are several other ways to potentially tackle this - I try to stick to the cleanest to read later if possible... :-)

Matrix of Controller to View - ZF

In the controller
My Matrix is $mes:
print_r($mes);
Array (
[0] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 3
[7] => 1
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
Array (
[1] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 2
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
Send data to view:
$this->view->repMes = $mes;
In the view
<?php print_r($this->repMes); ?>
Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 2
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
As can be evidenced missing the first part of the matrix, as it could be solved?
I believe you want to have a two dimensional array but looking at you code i see you have two different arrays . so you $mes variable points to the second array that is why you see only the second array in the view. you should have you array in the controller as:
$mes = Array (
[0] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 3
[7] => 1
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
[1] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 2
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
Notice when you print your matrix, it shows as two different arrays, that is because your code flow is not sending the right array.
Your array "$mes" should be this:
Array (
[0] => Array (
[0] => 0,
[1] => 0,
[2] => 0,
[3] => 0,
[4] => 0,
[5] => 0,
[6] => 3,
[7] => 1,
[8] => 0,
[9] => 0,
[10] => 0,
[11] => 0,
[12] => 0 ),
[1] => Array ( // See here you should have the second subindex, and not the closing and reopening
[0] => 0, // of "Array"
[1] => 0,
[2] => 0,
[3] => 0,
[4] => 0,
[5] => 0,
[6] => 2,
[7] => 0,
[8] => 0,
[9] => 0,
[10] => 0,
[11] => 0,
[12] => 0)
)
I would suggest using a pro-tempore counter variable to check if your "print" is repeating
Some like:
echo "<pre> Loop --> \n";
print_r($mes);
echo "End Loop\n</pre>";
This way you can check the real array.

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories