Filtering a php array using binary - php

A have a php array
$arr = array(
1 => "a",
2 => "b",
4 => "c",
8 => "d",
16 => "e",
32 => "f"
);
and a binary number
$filter=101101
I want to filter the array and keep only the keys where the respective value on binary is 1
For this example I would have:
$arr = array(
1 => "a",
4 => "c",
8 => "d",
32 => "f"
);
Or for
$filter=110001
to get
$arr = array(
1 => "a",
2 => "b",
32 => "f"
);

Assuming that the length of $filter is always the same as the number of array elements:
$filter_arr = str_split($filter);
$new_arr = array();
$i = 0;
foreach ($arr as $key => $val) {
if ($filter_arr[$i] == 1) {
$new_arr[$key] = $val;
}
$i++;
}
Using your given array, and filter equal to 101101, $new_arr will equal:
Array
(
[1] => a
[4] => c
[8] => d
[32] => f
)
See demo

This should work for you:
<?php
$arr = array(
1 => "a",
2 => "b",
4 => "c",
8 => "d",
16 => "e",
32 => "f"
);
$filter=110001;
$filerValue = str_split($filter);
$count = 0;
foreach($arr as $k => $v) {
if($filerValue[$count] == 0)
unset($arr[$k]);
$count++;
}
var_dump($arr);
?>
Output:
array(3) {
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[32]=>
string(1) "f"
}

Related

How to delete previous all elements from a specified index in PHP?

I have an array and I want to delete previous all elements from the current specified index
For example:
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
I have an index like 3, so I want to delete previous all like
0 => "a", 1 => "b", 2 => "c"
and only have
3=>"d", 4=>"e"
in my new array.
Can anyone help me?
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$output = array_slice($array, 3);
output:
array(2) {
[0]=> string(1) "d"
[1]=> string(1) "e"
}
Another solution with saving index
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$output = array_slice($array, 3, null, true);
output:
array(2) {
[3]=> string(1) "d"
[4]=> string(1) "e"
}
https://www.php.net/manual/en/function.array-slice.php
You may to use array_slice()
In example :
<?php
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$startingPosition = 3;
// Preserve keys
// |
// Your array Delete from Delete to |
// | | (if null, |
// | | to the end) |
// | | | |
// v v v v
$array = array_slice($array, $startingPosition , null, true);
var_dump($array);
Output :
array(2) {
[3]=>
string(1) "d"
[4]=>
string(1) "e"
}
You can also use unset() to remove the elements. As shown below.
<?php
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$index = 3;
for($i = 0; $i<$index; $i++)
{ unset($array[$i]); }
echo "<pre>";print_r($array);
?>
You can use veriation of array-slice and so on (as array_slice($array, 3) ) but also simple for loop:
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$copy = false;
foreach($array as $k => $v) {
$copy |= ($k == 3);
if ($copy)
$res[$k] = $v;
}
Try this
<?php
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$new_array = array_slice($array, 3); // 3 is your key to slice
print_r($new_array);
?>
You can use array_slice:
$array = [0 => "a", 1 => "b", 2 => "c", 3=>"d", 4=>"e"];
$newArray = array_slice($array, 3, NULL, TRUE);
echo '<pre>';
print_r($newArray);
echo '</pre>';
Output:
Array
(
[3] => d
[4] => e
)
Note that 4 th parameter: TRUE -> preserve_keys is very important.
If it is set to true, preserves the keys in the output array.
Your new array will now have all elements only after index 3
All elements before 3 are not returned here.

Indent value from array in php like a table

I've an array like:
$array = array(
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
6 => "G",
7 => "H",
);
Max lenght $array can be is 9, so max is index = 8 and min is 0 (at least 1 element inside array).
I've to indent this list inside a TCPDF box with limited height where, with some test, i've seen can support max 3 lines. But this box is large so, when the array lenght is bigger than 3, the others element need to be aligned in the side of the first column created like:
A D G
B E H
C F
I can't use X,Y coordinated cause i'm using writeHTML method in TCPDF.
I need to create 3 strings like:
$line1 = "A D G";
$line2 = "B E H";
$line3 = "C F";
What's the best method to create this 3 variables from my array?
UPDATE: using suggest method array_chunk is not the solution for me cause for my purpose I'd like to receive an array like:
Array ( [0] => Array (
[0] => A
[1] => D
[2] => G
)
[1] => Array (
[0] => B
[1] => E
[2] => H
)
[2] => Array (
[0] => C
[1] => F )
)
I think a for loop can solve OP's problem. This is another possible solution that use PHP array functions:
<?php
$array = array(
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
6 => "G",
7 => "H",
);
$cols = 3;
$array = array_chunk($array, $cols);
$results = array_map(function($index) use ($array) {
return array_column($array, $index);
}, range(0, $cols - 1));
var_dump($results);
View online here: https://eval.in/945787
<?php
$array = array(
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
6 => "G",
7 => "H",
);
$array_chunk = array_chunk($array, 3,false);
$line = '';
foreach ($array_chunk as $chunk_key => $chunk_value) {
$line = '"';
foreach ($chunk_value as $key => $value) {
$line.=$value." ";
}
$line = "$" . "line" . ($chunk_key+1) . " = " . rtrim($line, " ") . '"' . "<br/>";
echo $line;
$line='';
}
Demo

PHP: exchange all keys with their associated values in an array and preserving even if value has several indentical occurrences

In PHP we do have function array_flip for exchanging all keys with their associated values in an array, however I am looking for a the solution as follows with the help of php functions
INPUT
array(
"a" => 1,
"b" => 1,
"c" => 2
);
OUTPUT
array(
1 => array("a", "b"),
2 => "c"
);
I don't think there is a php function for it, so you'll have to write your own. If you really want a mixture of nested arrays and values the way to go would be:
function my_array_flip($arr)
{
$new_arr = array();
foreach ($arr as $k => $v)
{
if (array_key_exists($v, $new_arr))
{
if (is_array($new_arr[$v]))
{
$new_arr[] = $k;
}
else
{
$new_arr[$v] = array($new_arr[$v]);
$new_arr[$v][] = $k;
}
}
else
{
$new_arr[$v] = $k;
}
}
return $new_arr;
}
For your array this will return:
Array
(
[1] => Array
(
[0] => a
[1] => b
)
[2] => c
)
$source = array(
"a" => 1,
"b" => 1,
"c" => 2
);
foreach ($source as $key => $value)
{
$result[$value][] = $key;
}
Don't think exists a function for that but you can achieve it with some code:
$input = [
"a" => 1,
"b" => 1,
"c" => 2,
];
$grouped = [];
foreach ($input as $key => $group) {
$grouped[$group][] = $key;
}
Result:
var_dump($grouped);
array(2) {
[1] =>
array(2) {
[0] =>
string(1) "a"
[1] =>
string(1) "b"
}
[2] =>
array(1) {
[0] =>
string(1) "c"
}
}
Sets always array as values because I guess this will simplify things when you'll use the result in your code.

Change array order (like columns)

I want to change the order from:
$array = array(
"a" => "bar",
"b" => "foo",
"c" => "bar",
"d" => "foo",
"e" => "bar",
"f" => "foo",
"g" => "bar",
"h" => "foo",
"i" => "bar",
"j" => "foo"
);
To:
$array = array(
"a" => "bar", "f"=> "foo",
"b" => "foo", "g"=> "bar",
"c" => "bar", "h"=> "foo",
"d" => "foo", "i"=> "bar",
"e" => "bar", "j"=> "foo"
);
The point of this is that I want to fill a table with the items in the array:
The array should not be sorted like this:
<table>
<tr><td>Item 1</td><td>Item 2</td></tr>
<tr><td>Item 3</td><td>Item 4</td></tr>
</table>
But like this:
<table>
<tr><td>Item 1</td><td>Item 3</td></tr>
<tr><td>Item 2</td><td>Item 4</td></tr>
</table>
Thanks
There is no need to rearrange the original array. Just split it up in two pairs using array_chunk, and loop through them when building the html.
$array = array(....);
$size = ceil(count($array) / 2);
list($left, $right) = array_chunk($array, $size, true);
echo '<table>';
while (count($left) > 0) {
echo '<tr>';
echo '<td>', key($left), ': ', array_shift($left), '</td>';
echo '<td>', key($right), ': ', array_shift($right), '</td>';
echo '</tr>';
}
echo '</table>';
I assume that it does not mater whether the key is an string or a integer.
$array = array(
4 => 'd',
2 => 'b',
3 => 'c',
6 => 'f',
5 => 'e',
1 => 'a'
);
$x = floor(count( $array ) / 2);
for( $i=1; $i <= $x; $i++ )
{
$array2[ $i ] = $array[ $i ];
$array2[ $i + $x ] = $array[ $i + $x ];
}
will output:
$Array2
(
[1] => a, [4] => d,
[2] => b, [5] => e,
[3] => c, [6] => f
)
JB

How to convert string to array?

I need to convert string
"name1", "b", "2", "name2", "c", "3", "name3", "b", "2", ....
to an array like
$arr[0]['name'] = "name1";
$arr[0]['char'] = "b";
$arr[0]['qnt'] = "2";
$arr[1]['name'] = "name2";
$arr[1]['char'] = "c";
$arr[1]['qnt'] = "3";
$arr[2]['name'] = "name3";
$arr[2]['char'] = "b";
$arr[2]['qnt'] = "2";
I used explode to extract an string to array but it not work
Any idea?
$input = '"name1", "b", "2", "name2", "c", "3", "name3", "b", "2"';
$input = str_replace('"', '', $input);
$input = explode(', ', $input);
$output = array();
$i = 0;
while ($i < count($input)) {
$output[] = array(
'name' => $input[$i++],
'char' => $input[$i++],
'qnt' => $input[$i++]
);
}
print_r($output);
Output:
Array
(
[0] => Array
(
[name] => name1
[char] => b
[qnt] => 2
)
[1] => Array
(
[name] => name2
[char] => c
[qnt] => 3
)
[2] => Array
(
[name] => name3
[char] => b
[qnt] => 2
)
)
If you do not care about the array keys being numeric, you can do:
$string = 'name1, b, 2, name2, c, 3, name3, b, 2';
print_r( array_chunk( explode(',', $string), 3 ) );

Categories