I have the following declaration of array
Array
(
[name] => 1
[callrate] => 1
[maxcalls] => 100000
[mintime] => 5
[maxtime] => 16
[skillexps] => Array
(
[0] => 1
[1] => 2
)
)
How can I check the the array contains the array. I tried the
function is_multi($a) {
foreach ($a as $v) {
if (is_array($v))
{
return "has array";
break;
}
break;
}
return 'only value';
}
But this only gives 'only value'. I need to check the If Associative Array is two dimensional.
Why your existing code doesn't work?
Because you're looking every element of array to check is it an array or not using foreach() and is_array()? if not then break; so when it checks for name element the value is not an array, it is just a integer so it immediately break; and goes out of the foreach()loop and returns only value but when I removed the extra break; from your code it works fine because then it checks for each and every element of your array to verify that is it contains an array or not . I've also added a good looking way how to check array is multi-dimensional or not. Hope this helps :)
function is_multi($a) {
foreach ($a as $v) {
if (is_array($v))
{
return "has array";
break;
}
// removed extra break; from here
}
return 'only value';
}
To check array is multi-dimensional or not? I'll do this way,
<?php
function is_multi(array $array) {
return count($array) !== count($array, COUNT_RECURSIVE);
}
$array = array
(
'name' => 1,
'callrate' => 1,
'maxcalls' => 100000,
'mintime' => 5,
'maxtime' => 16,
'skillexps' => array
(
1,
2
)
);
echo is_multi($array);
?>
DEMO: https://3v4l.org/LANsh
If you are trying to check whether the array is 2d or not , you can use array filter like below
$data=array
(
'name' => 1,
'callrate' => 1,
'maxcalls' => 100000,
'mintime' => 5,
'maxtime' => 16,
'skillexps' =>array(1,2)
);
$filteredItems = array_filter($data, function($elem) {
return is_array($elem);
});
if(count($filteredItems)>0){
echo "multi dimensional";
}
else{
echo "1 dimensional";
}
If you are interested to check every keys,you can use array_map with closure function like below
$data=array
(
'name' => 1,
'callrate' => 1,
'maxcalls' => 100000,
'mintime' => 5,
'maxtime' => 16,
'skillexps' => array
(
0 => 1,
1 => 2
)
);
$array = array_map(function ($a) {
return is_array($a) ? "has array" : "only value";
}, $data);
print_r($array);
output
Array
(
[name] => only value
[callrate] => only value
[maxcalls] => only value
[mintime] => only value
[maxtime] => only value
[skillexps] => has array
)
If you want to check entire array is multidimensional or not then just add one line below
$multi=array_search('has array', $array) ? "is multi " : "is not multi";
echo $multi;
Related
I'm trying to convert mib-style strings into PHP array indices. The trick is that I have to do this for a variable number of strings. As an example:
$strings = ['1.1.1' => 1, '1.1.2' => 2, '1.2.1' => 1];
# Given the above, generate the below:
$array = [ 1 => [ 1 => [1 => 1, 2 => 2] ], 2 => [1 => 1] ] ] ] ]
I can't think of a way to do it that isn't just a brute-force, inefficient method. Any helpful function/approach/advice is welcome.
You could take a recursive approach since the problem/result you provide seems to be of a recursive nature. (You can achieve the same result with a loop, applying the same logic as the recursive function ofcourse)
So under the assumption that there are no conflicting string inputs/edge cases what so ever, the following could be one approach:
Loop over all the strings and their values, break it up and create a nested structure by passing the result array by its reference.
function createNested($pieces, $currentIndex, &$previous, $value)
{
$index = $pieces[$currentIndex];
// Our base case: when we reached the final/deepest level of nesting.
// Hence when the we reached the final index.
if ($currentIndex == count($pieces) - 1) {
// Can now safely assign the value to index.
$previous[$index] = $value;
} else {
// Have to make sure we do not override the key/index.
if (!key_exists($index, $previous)) {
$previous[$index] = [];
}
// If the key already existed we can just make a new recursive call (note one level deeper as we pass the array that $previous[$index] points to.
createNested($pieces, $currentIndex + 1, $previous[$index], $value);
}
}
$strings = ['1.1.1' => 1, '1.1.2' => 2, '1.2.1' => 1];
$result = [];
foreach ($strings as $string => $value) {
// Break up the string by .
createNested(explode('.', $string), 0, $result, $value);
}
echo '<pre>';
print_r($result);
echo '</pre>';
Will output:
Array
(
[1] => Array
(
[1] => Array
(
[1] => 1
[2] => 2
)
[2] => Array
(
[1] => 1
)
)
)
I have array:
Array
(
[5] => Array
(
[0] => 19
[1] => 18
)
[6] => Array
(
[0] => 28
)
)
And I'm trying to delete element by value using my function:
function removeElementWithValue($obj, $delete_value){
if (!empty($obj->field)) {
foreach($obj->field as $key =>$value){
if (!empty($value)) {
foreach($value as $k=>$v){
if($v == $delete_value){
$obj->field[$key][$k] = '';
}
}
}
}
}
return urldecode(http_build_query($obj->field));
}
echo removeElementWithValue($request, '19');
After operation above I have: 5[0]=&5[1]=18&6[0]=28; // Right!!!
echo removeElementWithValue($request, '18');
After operation above I have: 5[0]=&5[1]=&6[0]=28; // Wrong ???
But my expected result after second operation is:
5[0]=19&5[1]=&6[0]=28;
Where is my mistake?
Thanks!
Use array_walk_recursive to find and change value
$arr = Array (
5 => Array ( 0 => 19, 1 => 18 ),
6 => Array ( 0 => 28));
$value = 18;
array_walk_recursive($arr,
function (&$item, $key, $v) { if ($item == $v) $item = ''; }, $value);
print_r($arr);
result:
Array (
5 => Array ( 0 => 19, 1 => ),
6 => Array ( 0 => 28));
A simpler function might be..
function removeElementWithValue($ar,$val){
foreach($ar as $k=>$array){
//update the original value with a new array
$new_ar = array_diff_key($array,array_flip(array_keys($array,$val)));
if($new_ar){
$ar[$k]=$new_ar;
}else{
unset($ar[$k]);//or remove the empty value
}
}
return $ar;
}
I need to divide an array into two arrays.
One array will contain all positive values (and zeros), the other all negative values.
Example array:
$ts = [7,-10,13,8,0,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7];
Negatives result array:
[-10,-7.2,-12,-3.7,-9.6,-1.7,-6.2];
Non-negatives result array:
[7,13,8,0,4,3.5,6.5,7];
Without using any array functions..
Pretty straightforward. Just loop through the array and check if the number is less than 0, if so , push it in the negative array else push it in the positive array.
<?php
$ts=array(7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7);
$pos_arr=array(); $neg_arr=array();
foreach($ts as $val)
{
($val<0) ? $neg_arr[]=$val : $pos_arr[]=$val;
}
print_r($pos_arr);
print_r($neg_arr);
OUTPUT :
Array
(
[0] => 7
[1] => 13
[2] => 8
[3] => 4
[4] => 3.5
[5] => 6.5
[6] => 7
)
Array
(
[0] => -10
[1] => -7.2
[2] => -12
[3] => -3.7
[4] => -9.6
[5] => -1.7
[6] => -6.2
)
You can use array_filter function,
$positive = array_filter($ts, function ($v) {
return $v > 0;
});
$negative = array_filter($ts, function ($v) {
return $v < 0;
});
Note: This will skip values with 0, or you can just change condition to >=0 in positive numbers filter to considered in positive group.
DEMO.
The most elegant is to use phps array_filter() function:
<?php
$ts = [ 7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7 ];
print_r( array_filter( $ts, function( $val ) { return (0>$val); } ) );
print_r( array_filter( $ts, function( $val ) { return ! (0>$val); } ) );
?>
If you are still using an older php version you need some longer implementation:
<?php
$ts = array( 7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7 );
print_r( array_filter( $ts, create_function( '$val', 'return (0>$val);' ) ) );
print_r( array_filter( $ts, create_function( '$val', 'return ! (0>$val);' ) ) );
?>
Food for thought, you could write a generic function that splits an array based on a boolean result:
// splits an array based on the return value of the given function
// - add to the first array if the result was 'true'
// - add to the second array if the result was 'false'
function array_split(array $arr, callable $fn)
{
$a = $b = [];
foreach ($arr as $key => $value) {
if ($fn($value, $key)) {
$a[$key] = $value;
} else {
$b[$key] = $value;
}
}
return [$a, $b];
}
list($positive, $negative) = array_split($ts, function($item) {
return $item >= 0;
});
print_r($positive);
print_r($negative);
Demo
Rather than declaring two arrays, I recommend declaring one array with two subarrays. You can either give the subarrays an index of 0 or 1 depending on the conditional evaluation with zero, or you can go a little farther by declaring a lookup array to replace the integer key with an expressive word.
Regardless of if you choose to create one array or two arrays, you should only make one loop over the input array. Making two loops or by calling array_filter() twice is needlessly inefficient.
Code: (Demo)
$ts = [7,-10,13,8,4,-7.2,-12,-3.7,3.5,-9.6,6.5,-1.7,-6.2,7];
const KEY_NAMES = [0 => 'negative', 1 => 'non-negatuve'];
$result = [];
foreach ($ts as $v) {
$result[KEY_NAMES[$v >= 0]][] = $v;
}
var_export($result);
Output:
array (
'non-negatuve' =>
array (
0 => 7,
1 => 13,
2 => 8,
3 => 4,
4 => 3.5,
5 => 6.5,
6 => 7,
),
'negative' =>
array (
0 => -10,
1 => -7.2,
2 => -12,
3 => -3.7,
4 => -9.6,
5 => -1.7,
6 => -6.2,
),
)
Ok so i have a post that looks kind of this
[optional_premium_1] => Array
(
[0] => 61
)
[optional_premium_2] => Array
(
[0] => 55
)
[optional_premium_3] => Array
(
[0] => 55
)
[premium_1] => Array
(
[0] => 33
)
[premium_2] => Array
(
[0] => 36 )
[premium_3] => Array
(
[0] => 88 )
[premium_4] => Array
(
[0] => 51
)
how do i get the highest number out of the that. So for example, the optional "optional_premium_" highest is 3 and the "premium_" optional the highest is 4. How do i find the highest in this $_POST
You could use array_key_exists(), perhaps something like this:
function getHighest($variableNamePrefix, array $arrayToCheck) {
$continue = true;
$highest = 0;
while($continue) {
if (!array_key_exists($variableNamePrefix . "_" . ($highest + 1) , $arrayToCheck)) {
$continue = false;
} else {
highest++;
}
}
//If 0 is returned than nothing was set for $variableNamePrefix
return $highest;
}
$highestOptionalPremium = getHighest('optional_premium', $_POST);
$highestPremium = getHighest('premium', $_POST);
I have 1 question with 2 parts before I answer, and that is why are you using embedded arrays? Your post would be much simpler if you used a standard notation like:
$_POST['form_input_name'] = 'whatever';
unless you are specifically building this post with arrays for some reason. That way you could use the array key as the variable name and the array value normally.
So given:
$arr = array(
"optional_premium_1" => "61"
"optional_premium_2" => "55"
);
you could use
$key = array_keys($arr);
//gets the keys for that array
//then loop through get raw values
foreach($key as $val){
str_replace("optional_premium_", '', $val);
}
//then loop through again to compare each one
$highest = 0;
for each($key as $val){
if ((int)$val > $highest) $highest = (int)$val;
}
that should get you the highest one, but then you have to go back and compare them to do whatever your end plan for it was.
You could also break those into 2 separate arrays and assuming they are added in order just use end() http://php.net/manual/en/function.end.php
Loop through all POST array elements, pick out elements having key names matching "name_number" pattern and save the ones having the largest number portion of the key names. Here is a PHP script which does it:
<?php // test.php 20110428_0900
// Build temporary array to simulate $_POST
$TEMP_POST = array(
"optional_premium_1" => array(61),
"optional_premium_2" => array(55),
"optional_premium_3" => array(55),
"premium_1" => array(33),
"premium_2" => array(36),
"premium_3" => array(88),
"premium_4" => array(51),
);
$names = array(); // Array of POST variable names
// loop through all POST array elements
foreach ($TEMP_POST as $k => $v) {
// Process only elements with names matching "word_number" pattern.
if (preg_match('/^(\w+)_(\d+)$/', $k, $m)) {
$name = $m[1];
$number = (int)$m[2];
if (!isset($names[$name]))
{ // Add new POST var key name to names array
$names[$name] = array(
"name" => $name,
"max_num" => $number,
"key_name" => $k,
"value" => $v,
);
} elseif ($number > $names[$name]['max_num'])
{ // New largest number in key name.
$names[$name] = array(
"name" => $name,
"max_num" => $number,
"key_name" => $k,
"value" => $v,
);
}
}
}
print_r($names);
?>
Here is the output from the script:
Array
(
[optional_premium] => Array
(
[name] => optional_premium
[max_num] => 3
[key_name] => optional_premium_3
[value] => Array
(
[0] => 55
)
)
[premium] => Array
(
[name] => premium
[max_num] => 4
[key_name] => premium_4
[value] => Array
(
[0] => 51
)
)
)
Though ineffective, you could try something like
$largest = 0;
foreach($_POST as $key => $value)
{
$len = strlen("optional_premium_");
$num = substr($key, $len);
if($num > $largest)
$largest = $num;
}
print_r($largest);
The problem being that this will only work for one set of categories. It will most likely give errors throughout the script.
Ideally, you would want to reorganize your post, make each array be something like
[optional_premium_1] => Array
(
[0] => 61
["key"] => 1
)
[optional_premium_2] => Array
(
[0] => 61
["key"] => 2
)
Then just foreach and use $array["key"] to search.
This question already has answers here:
Get difference between associative rows of two 2-dimensional arrays
(5 answers)
Closed last year.
It seems that every PHP function I read about for comparing arrays (array_diff(), array_intersect(), etc) compares for the existence of array elements.
Given two multidimensional arrays with identical structure, how would you list the differences in values?
Example
Array 1
[User1] => Array ([public] => 1
[private] => 1
[secret] => 1
)
[User2] => Array ([public] => 1
[private] => 0
[secret] => 0
)
Array 2
[User1] => Array ([public] => 1
[private] => 0
[secret] => 1
)
[User2] => Array ([public] => 1
[private] => 0
[secret] => 0
)
Difference
[User1] => Array ([public] => 1
[private] => 0 //this value is different
[secret] => 1
)
So my result would be - "Of all the users, User1 has changed, and the difference is that private is 0 instead of 1."
One way is to write a function to do something similar to this..
function compareArray ($array1, $array2)
{
foreach ($array1 as $key => $value)
{
if ($array2[$key] != $value)
{
return false;
}
}
return true;
}
You could easily augment that function to return an array of differences in the two..
Edit - Here's a refined version that more closely resembles what you're looking for:
function compareArray ($array1, $array2)
{
var $differences;
foreach ($array1 as $key => $value)
{
if ($array2[$key] != $value)
{
$differences[$key][1] = $value;
$differences[$key][2] = $array2[$key];
}
}
if (sizeof($differences) > 0)
{
return $differences;
}
else
{
return true;
}
}
I think this does what you're looking for.
Using your sample data, doing a loop on the outer arrays, then using array_diff_assoc on the users each time through. (Note, this assumes that when there's a difference, array_diff_assoc returns the value from the second array passed in, which it seems to do).
<?php
$user1 = array("public" => 1, "private" => 1, "secret" => 1);
$user2 = array("public" => 1, "private" =>1, "secret" => 1);
$array1 = array ("user 1"=>$user1, "user 2"=>$user2);
$user1 = array("public" => 1, "private" => 0, "secret" => 1);
$user2 = array("public" => 1, "private" => 1, "secret" => 1);
$array2 = array("user 1"=>$user1, "user 2"=>$user2);
$results = array();
foreach ( $array1 as $user => $value )
{
$diff = array_diff_assoc( $array1[$user], $array2[$user] );
if ($diff) {
array_push($results,array($user=>$diff));
}
}
print_r($results);
?>
It returns:
Array
(
[0] => Array
(
[user 1] => Array
(
[private] => 1
)
)
)
Try this function:
function arrayDiff($array1, $array2)
{
if (!is_array($array1) || !is_array($array2)) {
return false;
}
foreach ($array1 as $key => $val) {
if (array_key_exists($key, $array2) && gettype($val) != "array" && $val === $array2[$key]) {
unset($array1[$key]);
continue;
}
if (is_array($val)) {
$val = diff($val, $array2[$key]);
if ($val !== false) {
$array1[$key] = $val;
}
}
}
return $array1;
}
$array1 = array(
array(
array('foo', 'bar', 'baz'),
0
)
);
$array2 = array(
array(
array('foo', 'bar')
)
);
var_dump(diff($array1, $array2));
If you're looking for differences in the values, how about array_diff_assoc. The http://us2.php.net/manual/en/function.array-diff-assoc.php">php manual says it "returns an array containing all the values from array1 that are not present in any of the other arrays" and gives the following example:
In this example you see the "a" =>
"green" pair is present in both arrays
and thus it is not in the ouput from
the function. Unlike this, the pair 0
=> "red" is in the ouput because in the second argument "red" has key
which is 1
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[b] => brown
[c] => blue
[0] => red
)
Is this what you're looking for?