Remove key from mixed array and reindex - php

I am new to postgresql with php. I get below array using pg_fetch_array() function.
Array
(
[0] =>
[name] =>
[1] => 1
[status] => 1
[2] => C2005
[code] => C2005
)
after removing index 1 and value of key status, i have to reindex this array so that expected output should become like:
Array
(
[0] =>
[name] =>
[1] => C2005
[code] => C2005
)
I tried
unset($row[1]);
unset($row['status'];
$foo = array_values($row);
echo "<pre>";
print_r($foo)
echo "</pre>";
and got output
Array
(
[0] =>
[name] =>
[2] => C2005
[code] => C2005
)
How the numeric index can be re-indexed after removing particular keys from the array?

You can filter the array by their key. Live demo.
unset($array['status']);
$number_keys = array_values(array_filter($array, function($k){return is_int($k) && $k != 1;}, ARRAY_FILTER_USE_KEY));
$nonnumber_keys = array_filter($array, function($k){return is_string($k);}, ARRAY_FILTER_USE_KEY);
$result = array_merge($number_keys, $nonnumber_keys);

Related

How to remove duplicate values from array - php

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

PHP Array to PHP variables

I got a PHP multidimensional array, I want to create variables from it first element as variable name and the second element as the variable value. I want to use this logic to print create the variables based on the language selected, the first column always will have the same names but the second value will generate different strings based on the language selected.
Array
(
[0] => Array
(
[0] => el1
[1] => Grouping
)
[1] => Array
(
[0] => el2
[1] => Type
)
[2] => Array
(
[0] => el3
[1] => Starting Date
)
[3] => Array
(
[0] => el4
[1] => Ending Date
)
[4] => Array
(
[0] => el5
[1] => Section
)
[5] => Array
(
[0] => el6
[1] => Cell
)
[6] => Array
(
[0] => el7
[1] => Client
)
[7] => Array
(
[0] => el8
[1] => Status
)
[8] => Array
(
[0] => el9
[1] => Article
)
[9] => Array
(
[0] => el10
[1] => Search
)
)
I want to assign the [0] value as a variable name and [1] as the variable value, the declaration should be in this way related to my array presented before:
<?php
el1="Grouping";
el2="Type";
el3="Starting Date";
?>
... and so on.
I want to echo out on the HTML page the string from the variable.
Try this :
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
You loop throught your big array
You get the first value with key 0 and transform it as variable : see documentation
You assign the value with key 1 and assign it to your variable
Test :
$array = array(
0 => array(
0 => "test",
1 => "value"
),
1 => array(
0 => "test2",
1 => "value2"
)
);
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
var_dump($test, $test2);
The output is :
string 'value' (length=5)
string 'value2' (length=6)
A simple loop through the array should do the trick:
foreach ($data as $item) {
$temp = $item[0];
${$temp} = $item[1];
}

check multidimensional array exists another multidimensional array

I have toe different multidimensional array following :
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 1
)
)
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
)
)
I want to check small multidimensional array exists in bigger array. Any suggestion please.
I am using
$diff = Hash::diff(samllarray, $bigger array);
of cakephp and its result is
Array
(
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 1
)
)
but in result I want only 3rd key but its also given me 2rd key see above
You can use is_array() to see if a variable is an array.
$arrs = array(
0 => "big array",
1 => "big array",
3 => array(
0 => "nested array",
1 => "nested array"
)
);
foreach ($arrs as $key=>$value) {
if (is_array($value)) {
echo "we've got an array at index {$key}";
}
}

Opposite function of strpos to grab values that do not match a specific pattern in a string or array

What is a function I can use that's basically the opposite of doing
if(strpos($array['some_key'], $value)!==false) {
that means there's a match and confinue
}
I basically want to loop through two arrays and grab the ones that don't have a match to the $value in $array.
$array =
Array
(
[0] => GPPZ20
[1] => GPPZ45
[2] => GPPZ75
[3] => GPPZH20
[4] => GPPZH45
)
$codes =
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH20SWYE4A2VZU
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH2077434178J6
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 17
[code] => PMMC4
[amount] => 25
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH2052910M8V62
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH45B3116LD1VW
[amount] => 45
)
)
so what i want to do is grab all the ones in the $codes array where the $codes['code'] value does not match any of the ones in the $array value.
right now i have the ones that match and grabbing those by doing
foreach($codes as $code) {
foreach($array as $key=>$value) {
if(strpos($code['code'], $value)!==false) {
//it matches grab those values
}
}
}
I basically now need something like this to grab the ones that do not match
You should use array_filter function - http://php.net/manual/en/function.array-filter.php e.g.:
function myFilter($val){ return strpos('foo', $val) === false; }
$array = array("foobar", "foo", "bar");
var_dump(array_filter($array, myFilter));
You can also use preg_match method instead of strpos.

PHP resort keys from multidimensional array

i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);

Categories