This question already has answers here:
How to convert array values to lowercase in PHP?
(10 answers)
Closed 5 years ago.
I have this code that is looking for $array2 in $array1.
The issue that I have is that I need to lowercase both arrays so the in_array matching works and this code operates as expected but $array1 is greater than 20k objects -- is there anyway to do the lowercase without losing the array structure and looping?
$array1 = array(code => 200, status => success,
array(
'email' => 'Example1223#sample.com',
'status' => 'Pending'
),
array(
'email' => 'example123#sample.com',
'status' => 'Approved: Printed & Cleared'
),
array(
'email' => 'example23#sample.com',
'status' => 'Approved'
),
array(
'email' => 'Example22#sample.com',
'status' => 'Approved: Printed & Cleared'
),
);
$yourArray = array();
$array = array();
foreach ($array1 as &$array){
$yourArray[] = array_map('strtolower', $array);
}
echo "<pre>"; print_r($yourArray);
$array2 = array(
'email' => 'example1223#sample.com',
'status' => 'Pending'
);
$yourArray2 = array_map('strtolower', $array2);
if(in_array($yourArray2 , $yourArray)) {
echo "match";
} else {
echo "no match";
}
echo "<pre>"; print_r($yourArray2);
You can always use preg_grep() function:
preg_grep("/ONe/i", $yourArray2);
Related
I want to build an array of arrays which in the next step will be used as argument to json_encode().
Each element in the array looks like this:
$element = array(
'ITEM_ID' => $itemID,
'STATUS' => $status
)
An example of a desired result with two elements is:
array( array('ITEM_ID' => 1,'STATUS' => "ok"), array('ITEM_ID' => 2,'STATUS' => "not ok") )
I have tried:
array_push($elementArray, $element1);
array_push($elementArray, $element2);
But is does not give the desired result. What should I do?
push_array is not a php functionyou can try with array_push() or more simple
Try with
$element = array(
'ITEM_ID' => $itemID,
'STATUS' => $status
)
$element2 = array(
'ITEM_ID' => $itemID,
'STATUS' => $status
)
$finalArray[] = $element;
$finalArray[] = $element2;
echo "<pre>";
print_r($finalArray);
I am trying to find the difference of 2 multidimensional arrays. I am attempting to solve this with a modified recursive array difference function.
If I have the following array setup:
$array1 = array(
0 => array(
'Age' => '1004',
'Name' => 'Jack'
),
1 => array (
'Age' => '1005',
'Name' => 'John'
)
);
$array2 = array(
0 => array(
'Age_In_Days' => '1004',
'Name' => 'Jack'
),
1=> array(
'Transaction_Reference' => '1005',
'Name' => 'Jack'
)
);
I am trying to match the arrays however the keys are not the same. I want to return the difference between the two multidimensional arrays where
$array1[$i]['Age'] == $array2[$i]['Age_In_Days'];
I want to keep the original array structure if the above condition holds true so the output I am looking for is:
$diff = array (1 => array (
'Age' => '1005',
'Name' => 'John'
));
However I am having issues with how to modify the recursive function to achieve this. Any help is appreciated! Thanks!
You need to loop through first array and compare values with second array. Then follows your condition. If condition is true then push this unique value to third array. Values in third array are now diff between first and second array.
$diff = [];
foreach ($array1 as $value1) {
foreach ($array2 as $value2) {
if ($value1['Age'] !== $value2['Age_In_Days']) {
array_push($diff, $value1);
}
}
}
I have the following string: "1,3,4,7" which I need to explode into an Array in the following format:
$data = array(
array(
'id' => 1
),
array(
'id' => 3
),
array(
'id' => 4
),
array(
'id' => 7
),
);
This seems to be causing me a lot more pain than I'd have thought. Can anyone kindly assist please?
You can use a combination of array_map() and explode(): First you create an array with the values and than you map all these values to the format you need in a new array.
Something like:
$vals = "1,3,4,7";
$map = array_map(function($val) {
return array('id' => $val);
}, explode(',', $vals));
var_dump($map);
An example.
Firstly you explode the string to get the values in an array. Then you iterate through the array and set the data as array to another array
$string = "1,2,3,4";
$array = explode(",", $string);
$data = array();
foreach($array as $arr){
$data[] = array('id' => $arr);
}
<?php
$myArray=explode(",","1,3,5,7");
$result=array();
foreach($myArray as $key=>$arr)
{
$result[$key]['id']=$arr;
}
print_r($result);
?>
Here is a thinking-outside-the-box technique that doesn't require a loop or iterated function calls.
Form a valid query string with the desired structure and parse it. The empty [] syntax will generate the indexes automatically.
Code: (Demo)
$vals = "1,3,4,7";
$queryString = 'a[][id]=' . str_replace(',', '&a[][id]=', $vals);
parse_str($queryString, $output);
var_export($output['a']);
Output:
array (
0 =>
array (
'id' => '1',
),
1 =>
array (
'id' => '3',
),
2 =>
array (
'id' => '4',
),
3 =>
array (
'id' => '7',
),
)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
recursive array_diff()?
I have a static multidimensional array which is always going to be in the same form. E.g. it will have the same keys & hierarchy.
I want to check a posted array to be in the same 'form' as this static array and if not error.
I have been trying various methods but they all seem to end up with a lot of if...else components and are rather messy.
Is there a succinct way to achieve this?
In response to an answer from dfsq:
$etalon = array(
'name' => array(),
'income' => array(
'day' => '',
'month' => array(),
'year' => array()
),
'message' => array(),
);
$test = array(
'name' => array(),
'income' => array(
'day' => '',
'month' => array(),
'year' => array()
),
'message' => array(),
);
// Tests
$diff = array_diff_key_recursive($etalon, $test);
var_dump(empty($diff));
print_r($diff);
And the results from that are
bool(false)
Array ( [name] => 1 [income] => Array ( [month] => 1 [year] => 1 ) [message] => 1 )
Author needs a solution which would test if the structure of the arrays are the same. Next function will make a job.
/**
* $a1 array your static array.
* $a2 array array you want to test.
* #return array difference between arrays. empty result means $a1 and $a2 has the same structure.
*/
function array_diff_key_recursive($a1, $a2)
{
$r = array();
foreach ($a1 as $k => $v)
{
if (is_array($v))
{
if (!isset($a2[$k]) || !is_array($a2[$k]))
{
$r[$k] = $a1[$k];
}
else
{
if ($diff = array_diff_key_recursive($a1[$k], $a2[$k]))
{
$r[$k] = $diff;
}
}
}
else
{
if (!isset($a2[$k]) || is_array($a2[$k]))
{
$r[$k] = $v;
}
}
}
return $r;
}
And test it:
$etalon = array(
'name' => '',
'income' => array(
'day' => '',
'month' => array(),
'year' => array()
),
'message' => ''
);
$test = array(
'name' => 'Tomas Brook',
'income' => array(
'day' => 123,
'month' => 123,
'year' => array()
)
);
// Tests
$diff = array_diff_key_recursive($etalon, $test);
var_dump(empty($diff));
print_r($diff);
This will output:
bool(false)
Array
(
[income] => Array
(
[month] => Array()
)
[message] =>
)
So checking for emptiness of $diff array will tell you if arrays have the same structure.
Note: if you need you can also test it in other direction to see if test array has some extra keys which are not present in original static array.
You could user array_intersect_key() to check if they both contain the same keys. If so, the resulting array from that function will contain the same values as array_keys() on the source array.
AFAIK those functions aren't recursive, so you'd have to write a recursion wrapper around them.
See User-Notes on http://php.net/array_diff_key
Are you searching for the array_diff or array_diff_assoc functions?
use foreach with the ifs...if u have different tests for the different inner keys eg
$many_entries = ("entry1" = array("name"=>"obi", "height"=>10));
and so on, first define functions to check the different keys
then a foreach statement like this
foreach($many_entries as $entry)
{
foreach($entry as $key => $val)
{
switch($key)
{
case "name": //name function
//and so on
}
}
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP: get keys of independent arrays
Hello.
I have a multi-dimensional array. I want a function that finds the position of the given array key (all my array keys are strings) and then returns the position of the key as an array.
E.g:
$arr = array
(
'fruit' => array(
'apples' => array(),
'oranges' => array(),
'bananas' => array()
),
'vegetables' => array(
'tomatoes' => array(),
'carrots' => array(),
'celery' => array(),
'beets' => array
(
'bears' => array(),
'battlestar-galactica' => array()
),
),
'meat' => array(),
'other' => array()
);
Now if I call the function like this:
theFunction('bears');
It should return:
array(1, 3, 0);
function array_tree_search_key($a, $subkey) {
foreach (array_keys($a) as $i=>$k) {
if ($k == $subkey) {
return array($i);
}
elseif ($pos = array_tree_search_key($a[$k], $subkey)) {
return array_merge(array($i), $pos);
}
}
}