I've been trying to write a PHP function which searches the id index valeus in array, and once found, returns the path which lead to it's discovery.
Take the following array:
Array
(
[0] => Array
(
[id] => 1
[data] => Array
(
[0] => Array
(
[id] => 8
)
[1] => Array
(
[id] => 9
)
[2] => Array
(
[id] => 10
[data] => Array
(
[0] => Array
(
[id] => 15
[data] => Array
(
[0] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 21
)
)
)
)
)
)
If looking for [id] => 21 it would return array(1,10). However, in numerous attempts I have failed. The set path should be set to the index id. However, I cannot figure it out. Any words of guidance are much appreciated.
This functions returns array(1,10) for OP example
(will leave that other answer just in case someone will look for normal "path searching")
function search_data($needle, $haystack) {
if (is_array($haystack)) {
foreach($haystack as $data) {
if ($data['id'] == $needle) return array();
if (isset($data['data'])) {
if (($path = search_data($needle, $data['data'])) !== false) return array_merge(array($data['id']), $path);
}
}
}
return false;
}
This functions returns array(0,'data',2,'data',1,'id') for OP example (i.e. full path to value)
Function which searches for $key => $value pair in array and returns the path:
function array_search_r($key, $value, $haystack, $strict = null) {
$strict = $strict ?: false;
if (is_array($haystack)) {
foreach($haystack as $k => $v) {
if ($strict ? ($k === $key && $v === $value) : ($k == $key && $v == $value)) return array($k);
if(($path = array_search_r($key, $value, $v, $strict)) !== false) return array_merge(array($k), $path);
}
}
return false;
}
Related
I have the following array:
Array
(
[documents] => Array
(
[0] => application/pdf
[1] => application/x-pdf
)
[images] => Array
(
[0] => image/cgm
[1] => image/g3fax
)
[videos] => Array
(
[0] => video/dl
[1] => video/fli
[2] => video/gl
[3] => video/mpeg
)
And I have a couple of tables called documents, images, videos. So what I would like to do is to see in which database the file should go.
I tried to do it with array_search() but without success. After that I found a function which I tried also no luck.
function array_search_multi( $value, array $array ) {
foreach( $array as $key => $val ) {
if( is_array( $val ) ) {
array_search_multi($value, $val); // Recursive in case array is deeper
} else {
if( $val === $value ) {
return $key;
}
}
}
return false;
}
I hope someone could help me with this
If I understand you're looking for something like this
function find($mimeType) {
$array = [
'documents' => ['application/pdf','application/x-pdf'],
'images' => ['image/cgm','image/g3fax'],
'videos' => ['video/dl','video/fli','video/gl','video/mpeg'],
];
$table = null;
foreach ($array as $type => $values) {
$table = $type;
if ( in_array ($mimeType, $values) ) break;
}
return $table;
}
$sample = 'image/g3fax';
echo find($sample);
I'd like to extract only up to a certain part of the name and test if it's set (isset)?
Specifically for largeImage_1, largeImage_2, etc..
This is what I've tried, but it doesn't seem to return or test.
if ($_POST['tutorial']) {
foreach ( $_POST['tutorial'] as $key => $value ) {
if ('largeImage' === substr($key, 0 , 10) )
{
echo $value;
}
}
}
Array
(
[title] => title
[tutorial] => Array
(
[1] => Array
(
[largeImage_1] => Array
(
[0] => image.png
[1] => image.png
)
)
[2] => Array
(
[largeImage_2] => Array
(
[0] => image.png
)
)
)
[name] => "";
)
You need a loop within another loop to access keys like 'largeImage_1', 'largeImage_2'.
if ($_POST['tutorial']) {
foreach ( $_POST['tutorial'] as $key => $value ) {
foreach ( $value as $k => $v ) {
if ('largeImage' === substr($k, 0 , 10) )
{
//your code here
}
}
}
}
How would I create a function that filters a two dimensional array by value?
Given the following array :
Array
(
[0] => Array
(
[interval] => 2014-10-26
[leads] => 0
[name] => CarEnquiry
[status] => NEW
[appointment] => 0
)
[1] => Array
(
[interval] => 2014-10-26
[leads] => 0
[name] => CarEnquiry
[status] => CALL1
[appointment] => 0
)
[2] => Array
(
[interval] => 2014-10-26
[leads] => 0
[name] => Finance
[status] => CALL2
[appointment] => 0
)
[3] => Array
(
[interval] => 2014-10-26
[leads] => 0
[name] => Partex
[status] => CALL3
[appointment] => 0
)
How would I filter the array to only show those arrays that contain a specific value in the name key? For example name = 'CarEnquiry'.
The resulting output would be:
Array
(
[0] => Array
(
[interval] => 2014-10-26
[leads] => 0
[name] => CarEnquiry
[status] => NEW
[appointment] => 0
)
[1] => Array
(
[interval] => 2014-10-26
[leads] => 0
[name] => CarEnquiry
[status] => CALL1
[appointment] => 0
)
)
EDIT
I forgot to mention that the search value should be interchangeable - i.e. name = 'CarEnquiry' or name = 'Finance'.
Use PHP's array_filter function with a callback.
$new = array_filter($arr, function ($var) {
return ($var['name'] == 'CarEnquiry');
});
Edit: If it needs to be interchangeable, you can modify the code slightly:
$filterBy = 'CarEnquiry'; // or Finance etc.
$new = array_filter($arr, function ($var) use ($filterBy) {
return ($var['name'] == $filterBy);
});
If you want to make this a generic function use this:
function filterArrayByKeyValue($array, $key, $keyValue)
{
return array_filter($array, function($value) use ($key, $keyValue) {
return $value[$key] == $keyValue;
});
}
<?php
function filter_array($array,$term){
$matches = array();
foreach($array as $a){
if($a['name'] == $term)
$matches[]=$a;
}
return $matches;
}
$new_array = filter_array($your_array,'CarEnquiry');
?>
Above examples are using the exact word match, here is a simple example for filtering array to find imprecise "name" match.
$options = array_filter($options, function ($option) use ($name) {
return strpos(strtolower($option['text']), strtolower($name)) !== FALSE;
});
array_filter is the function you need. http://php.net/manual/en/function.array-filter.php
Give it a filtering function like this:
function my_filter($elt) {
return $elt['name'] == 'something';
}
function multi_array_search_with_condition($array, $condition)
{
$foundItems = array();
foreach($array as $item)
{
$find = TRUE;
foreach($condition as $key => $value)
{
if(isset($item[$key]) && $item[$key] == $value)
{
$find = TRUE;
} else {
$find = FALSE;
}
}
if($find)
{
array_push($foundItems, $item);
}
}
return $foundItems;
}
This my function can use about this problem. You can use:
$filtered = multi_array_search_with_condition(
$array,
array('name' => 'CarEnquiry')
);
This will get your filtered items from your 2 dimensional array.
I have an array in the following format:
[8106] => Array (
[id1] => 210470
[id2] => 216298
)
[8107] => Array (
[id1] => 210470
[id2] => 187145
)
[8108] => Array (
[id1] => 187145
[id2] => 216298
)
[8109] => Array (
[id1] => 187145
[id2] => 210470
)
[8110] => Array (
[id1] => 266533
[id2] => 249612
)
[8111] => Array (
[id1] => 249612
[id2] => 266533
)
I need to get it into the following format:
[0] => Array (
[0] => 266533
[1] => 249612
)
[1] => Array (
[0] => 187145
[1] => 210470
[2] => 216298
)
Basically, I need to extract all the ids, keep the relationships, but group them all together. I have a function to do this, but it takes forever (I am up to 30+ minutes on the number of rows I have to run through). Keys and order are unimportant. The relationship is all that is important. I am looking for a faster method. The function(s) I am using are below:
function getMatchingIDs($filteredArray)
{
$result = array();
$resultCount = 0;
foreach ($filteredArray as $details) {
$imaId1 = inMultiArray($details['id1'], $result);
$imaId2 = inMultiArray($details['id2'], $result);
if ($imaId1 === false && $imaId2 === false) {
$result[$resultCount++] = array(
$details['id1'],
$details['id2'],
);
} elseif (is_numeric($imaId1) === true && $imaId2 === false) {
$result[$imaId1][] = $details['id2'];
} elseif ($imaId1 === false && is_numeric($imaId2) === true) {
$result[$imaId2][] = $details['id1'];
} elseif ($imaId2 != $imaId1) {
$result[$imaId1] = array_merge($result[$imaId1], $result[$imaId2]);
unset($result[$imaId2]);
}
}
return $result;
}
function inMultiArray($elem, $array)
{
if (is_array($array) === true) {
// if the variable $elem is in the variable $array return true
if (is_array($array) === true && in_array($elem, $array) === true) {
return true;
}
// if $elem isn't in $array, then check foreach element
foreach ($array as $key => $arrayElement) {
// if $arrayElement is an array call the inMultiArray function to this element
// if inMultiArray returns true, than return is in array, else check next element
if (is_array($arrayElement) === true) {
$value = inMultiArray($elem, $arrayElement);
if ($value === true) {
return $key;
}
}
}
}
// if isn't in array return false
return false;
}
$filtered = getMatchingIDs($unfiltered);
EDIT: The original array describes relations between pairs of ids (not shown in the array). The desired output is that the relations are further defined. If you look in the original array, elements 8106-8109 are simply paired combinations of three ids. I need those three grouped together. Elements 8110 and 8111 are a distinct pair, just in a different order.
$newArray = array();
foreach ($array as $k => $v) {
$newArray[0][] = $v['id1'];
$newArray[1][] = $v['id2'];
}
What I finally ended up doing was in essence creating an index array. This array held all the positions of each value in the primary array.
So the following array
[0] => Array (
[0] => 266533
[1] => 249612
)
[1] => Array (
[0] => 187145
[1] => 210470
[2] => 216298
)
has an index of:
[187145] => 1
[210470] => 1
[216298] => 1
[249612] => 0
[266533] => 0
So instead of looking for the value in the primary multidimensional array, I check to see if it exists in the index array and process the data based on that. The results are that it now runs the entire process in <5 seconds instead of > 1 hour.
Thank you for your help.
I have an array like
Array (
[0] => Array ( [0] => Array ( [SSN] => 123456789 [borrowerName] => Peter K ) )
[1] => Array ( [0] => Array ( [SSN] => 412516789 [borrowerName] => Jack P ) )
[2] => Array ( [0] => Array ( [SSN] => 999338999 [borrowerName] => Angel M ) )
)
I want to be able to search for an SSN and get back the index of where it is
for example search($array,'SSN','412516789') should return 1
Of course you need a foreach. Can't do otherwise.
function search($array,$key,$value) {
foreach($array as $k=>$v) {
if ($v[0][$key]==$value)
return $k;
}
}
Here's some function searching recursively in a multidim array, you may want to adapt it to your needs.
function findInMultiDim($array, $value){
if(in_array($value,$array)) return TRUE;
foreach($array as $v){
if(is_array($v)){
$result = findInMultiDim($v, $value);
if($result === TRUE){
return TRUE;
}
}
}
return FALSE;
}