Search multidimensional array value to get key - php

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);

Related

How to read nested array values without knowing the keys?

I'm new in array. I wish to print out the values of all possible [type] [label] [value]. I wish to do like "if there's a type with select, then display all labels with values"
Below is my array.
Array
(
[product_id] => 8928
[title] => Example of a Product
[groups] => Array
(
[8929] => Array
(
[8932] => Array
(
[type] => select
[label] => Section
[id] => pewc_group_8929_8932
[group_id] => 8929
[field_id] => 8932
[value] => Section 200
[flat_rate] => Array
(
)
)
)
)
[price_with_extras] => 0
[products] => Array
(
[field_id] => pewc_group_8929_9028
[child_products] => Array
(
[8945] => Array
(
[child_product_id] => Array
(
[0] => 8945
)
[field_id] => pewc_group_8929_9028
[quantities] => one-only
[allow_none] => 0
)
)
[pewc_parent_product] => 8928
[parent_field_id] => pewc_5d678156d81c6
)
)
The site is saying "
It looks like your post is mostly code; please add some more details." So here's my so-called lorem ipsum :D
You can use array_walk_recursive for the same,
array_walk_recursive($arr, function ($value, $key) {
// check if key matches with type, label or value
if (count(array_intersect([$key], ['type', 'label', 'value'])) > 0) {
echo "[$value] ";
}
});
Demo
Output
[select] [Section] [Section 200]
EDIT
You can modify the condition as,
if($key === 'type' && $value == 'select')
EDIT 1
array_walk_recursive($arr, function ($value, $key) {
if($key === 'type' && $value == 'select'){
echo "[$value] ";
}
});
EDIT 2
function search_by_key_value($arr, $key, $value)
{
$result = [];
if (is_array($arr)) {
if (isset($arr[$key]) && $arr[$key] == $value) {
$result[] = $arr;
}
foreach ($arr as $value1) {
$result = array_merge($result, search_by_key_value($value1, $key, $value));
}
}
return $result;
}
// find array by key = type and value = select
$temp = search_by_key_value($arr, 'type', 'select');
$temp = array_shift($temp);
print_r($temp);
echo $temp['label'];
echo $temp['value'];
Demo

PHP extract elements from an array which have a common name

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
}
}
}
}

unset row from multidimension array

I have two arrays:
array one is named as $input that output:
Array
(
[0] => Array
(
[reference_no] => 306334510
[archive_no] => 20140102900152506
)
[1] => Array
(
[reference_no] => 22936619
[archive_no] => 20140104900153643
)
[2] => Array
(
[reference_no] => 30211132
[archive_no] => 20140109001536461
)
[3] => Array
(
[reference_no] => 3890623027301
[archive_no] => 201401029001949791
)
)
and second array named as $active that will output:
Array
(
[0] => Array
(
[archive_no] => 20140102900152506
)
[1] => Array
(
[archive_no] => 20140104900153643
)
[2] => Array
(
[archive_no] => 2014010900133107
)
[3] => Array
(
[archive_no] => 2014010900152506
)
[4] => Array
(
[archive_no] => 2014010900153643
)
)
So first I need to check for duplicated rows and I check it by 'archive_no'.
So far this works fine, but now I need to remove those duplicated rows from $input array.
My unset does not seem to work.
code so far:
public function RemoveDublicatedRows($input, $active){
$output = array();
foreach ($input as $inp) {
foreach ($active as $act) {
if ($act['archive_no'] == $inp['archive_no']) {
$output[] = $act;
foreach ($output as $out){
if($inp['archive_no'] == $out['archive_no']){
$inp = array($inp);
unset($input[$inp]);
}
}
}
}
}
return $output;
}
You need to have an index instead of an array ($inp) here:
unset($input[$inp]);
Should be:
if($inp['archive_no'] == $out['archive_no'])
{
$index = array_search($inp, $input);
unset($input[$index]);
}
EDIT: some parts are useless in your code, here is a cleaned code:
public function RemoveDublicatedRows($input, $active)
{
$output = array();
foreach ($input as $inp)
{
foreach ($active as $act)
{
if ($inp['archive_no'] == $act['archive_no'])
{
$output[] = $act;
// maybe you should consider storing $act['archive_no'] instead ?
// It's up to you, depends on what you need...
$index = array_search($inp, $input);
unset($input[$index]);
}
}
}
return $output;
}

Splitting keys from values into another array

I have a function to convert a .json file to an array:
function jsonToArray($file) {
$json = json_decode(file_get_contents($file), true);
print_r($json); }
This yields an array like this:
Array (
[field1] => value1
[field2] => Array
(
[subfield1] => subvalue1
[subfield2] => subvalue2
[subfield3] => subvalue3
)
)
To interface with existing code, I need these arrays with the fields and values split, like this:
Array (
[0] => Array
(
[0] => field1
[1] => Array
(
[0] => subfield1
[1] => subfield2
[2] => subfield3
)
)
[1] => Array
(
[0] => value1
[1] => Array
(
[0] => subvalue1
[1] => subvalue2
[2] => subvalue3
)
)
)
The code I came up with works if this structure is maintained for all usage but as that can't be guaranteed I need another solution. I'm sure it's something relatively simple, I just can't crack it. Any hints or insight would be much appreciated.
try this code
$arr = array ('field1' => 'value1',
'field2' => array(
'subfield1' => 'subvalue1',
'subfield2' => 'subvalue2',
'subfield3' => 'subvalue3'));
function array_values_recursive($ary) {
$lst = array();
foreach( $ary as $k => $v ) {
if (is_scalar($v)) {
$lst[] = $v;
} elseif (is_array($v)) {
$lst[] = array_values_recursive($v);
}
}
return array_values($lst);
}
function array_keys_recursive($ary) {
$lst = array();
foreach( $ary as $k => $v ) {
if (is_scalar($v)) {
$lst[] = ($k);
} elseif (is_array($v)) {
$lst[] = array_keys_recursive($v);
}
}
return $lst;
}
echo '<pre>';
$arr1 = array();
$arr1[] = array_values_recursive($arr);
$arr1[] = array_keys_recursive($arr);
print_r($arr1);
This might be useful to you: array_values() and array_keys() that and a little of foreach would do the magic.

Searching for array index in a multi dimentional array

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;
}

Categories