im using array_intersect for comparing 2 array
$myArray = array(
array(
'site_id' => 'S6407',
'tssr_id' => 'TSSRBOQ-200204-0145-59'
),
array(
'site_id' => 'S5910',
'tssr_id' => 'TSSRBOQ-200204-0145-8'
),
);
// $items_tssr is get from another variable
foreach ($items_tssr as $key => $value) {
$array_validate[] = array(
'site_id' => $value['site_id'],
'tssr_id' => $value['no_tssr_boq_site']
);
}
$result = array_map('unserialize',
array_intersect(
array_map('serialize', $myArray), array_map('serialize', $array_validate)));
// if there are same
if(array_key_exists(0,$result)){
echo 'process this';
}else{
echo 'dont process this';
}
my problem is, the original $myArray is more than 'site_id' and 'tssr_id'
$myArray_origin = array(
'site_id' => 'S6407',
'tssr_id' => 'TSSRBOQ-200204-0145-59'
'site_name' => 'Site S6407',
'id_site_doc'=> '127361,
'implementation_id' => 'imp4121',
'status' => "implementation_created",
"endstate" => false
),
...
how do i process the $myArray_origin without throw away a few of the value? because $array_validate is only have 2 value 'site_id' and 'tssr_id'
You could make use of array_filter + in_array instead. This will only keep the entries whose site_id and tssr_id are present in one of array_validate's own entries:
$result = array_filter($myArray, function (array $entry) use ($array_validate): bool {
return in_array([
'site_id' => $entry['site_id'],
'tssr_id' => $entry['tssr_id']
], $array_validate, true);
});
Demo: https://3v4l.org/4Qhmr
Related
I have 2 dimensional array like
$events = array(
array(
'desc' => 'Cancer Webinar',
'date' => '20201219'
),
array(
'desc' => 'CSR Management',
'date' => '20200812'
),
array(
'desc' => 'Company Anniversary',
'date' => '20200309'
)
);
$look = array('20201219','20200309');
result: array('Cancer Webinar','Company Anniversary');
The function call search from array list ($look) then find it to $events.
From the code above shoud return:
array('Cancer Webinar','Company Anniversary');
If the second array will just search by date use the following code
function search_from_array($array, $events) {
$result = [];
foreach($events as $event) {
if(in_array($event['date'], $array)) {
array_push($result, $event['desc']);
}
}
echo json_encode($result);
}
search_from_array($look, $events);
you can develop it more to can search with any key in the multidimensional array.
in case you want to search but many keys just add OR in the condition and make the same code for other keys like this
if(in_array($event['date'], $array) || in_array($event['desc'], $array))
You can make $look a dict, then check if the date key exist.
$events = array(
array(
'desc' => 'Cancer Webinar',
'date' => '20201219'
),
array(
'desc' => 'CSR Management',
'date' => '20200812'
),
array(
'desc' => 'Company Anniversary',
'date' => '20200309'
)
);
$look = array('20201219','20200309');
$look_dic = array_flip($look);
foreach($events as $event){
if(isset($look_dic[$event["date"]])){
$result[] = $event["desc"];
}
}
var_dump($result);
how to check whether 4 exist or not in array at id key position
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
),
)
The array you provided is not a valid multi-dimensional array. You need to add commas after each array in the array. Below i use array_column and in_array without using foreach
<?php
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),//add comma
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
),
);
$filtered = array_column($arr, 'id');//return the id column in this array
if(in_array(4, $filtered)){//check if 4 exists
echo '4 exists';
} else {
echo '4 does not exist';
}
?>
Quite simple, loop through the array and check if the value exists:
$value = 4;
$exists = false;
foreach($arr as $innerArr){
if($innerArr['id'] == $value){
$exists = true;
break;
}
}
If $exists is now true, the value exists within the array.
Try this one, and let me know if you face any problem.
<?php
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
)
);
foreach ($arr as $key => $value) {
if (in_array("4", $value))
{
$sub_index = $value['id'];
echo "Match found at $sub_index";
break;
}
}
Just gotta loop through your array and check existence through in_array() function.
you need something like this
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
)
);
$exists_flag = false;
foreach($arr as $inside_arr)
{
if($inside_arr['other_data'] == 4) {
$exists_flag = true;
break;
}
}
print($exists_flag);
Hope this helps!
As it stand, your array is wrong, you need to separate multi array with commas, you need to not that in_array() will not work with multi array, however you may create a recursive function that will check a provided key does exists or not in an array try code below, hope it helps ,
<?php
$arr = array(
array(
'id' => 1,
'other_data' => 'ganesh'
),
array(
'id' => 2,
'other_data' => 'ramesh'
),
array(
'id' => 3,
'other_data' => '4'
)
);
function search_items($search_value, $array)
{
foreach ($array as $item) {
if (($item == $search_value) || (is_array($item) && search_items($search_value, $item))) {
return true;
}
}
return false;
}
echo search_items("4", $arr) ? 'item found' : 'item not found';
?>
$result = array_search(4, array_column($arr, 'id'));
If we split this into steps then it would be the following:
$allColumnsNamedId = array_column($arr, 'id'); // find all columns with key 'id'
$resultBoolean = array_search(4, $allColumnsNamedId); //search the array for value 4
if($resultBoolean) {
echo 'Exists';
} else {
echo 'Does not exist';
}
I have an associative array with lots of elements and want to get a list of all elements that have a key name with a certain prefix.
Example:
$arr = array(
'store:key' => 1,
'user' => 'demo',
'store:foo' => 'bar',
'login' => true,
);
// this is where I need help with:
// the function should return only elements with a key that starts with "store:"
$res = list_values_by_key( $arr, 'store:' );
// Desired output:
$res = array(
'store:key' => 1,
'store:foo' => 'bar',
);
You could simply do :
$arr = array(
'store:key' => 1,
'user' => 'demo',
'store:foo' => 'bar',
'login' => true,
);
$arr2 = array();
foreach ($arr as $array => $value) {
if (strpos($array, 'store:') === 0) {
$arr2[$array] = $value;
}
}
var_dump($arr2);
Returns :
array (size=2)
'store:key' => int 1
'store:foo' => string 'bar' (length=3)
This should work for you:
Just grab all keys which starts with store: with preg_grep() from your array. And then do a simple array_intersect_key() call to get the intersect of both arrays.
<?php
$arr = array(
'store:key' => 1,
'user' => 'demo',
'store:foo' => 'bar',
'login' => true,
);
$result = array_intersect_key($arr, array_flip(preg_grep("/^store:/", array_keys($arr))));
print_r($result);
?>
output:
Array
(
[store:key] => 1
[store:foo] => bar
)
From php 5.6 you can use array_filter:
$return = array_filter($array, function ($e) {
return strpos($e, 'store:') === 0;
}, ARRAY_FILTER_USE_KEY);
var_dump($return);
for earlier versions, you can use
$return = array_intersect_key($array, array_flip(array_filter(array_keys($array), function ($e) {
return strpos($e, 'store:') === 0;
})));
Demo.
I have the following multidimensional array.
$arr = array(
0 => array(
'id' => 1,
'title' => 'title1',
'url' => 'http://www.foo.bar/',
'blurb' => 'blurb1',
'custodian' => 'custodia1',
'tags' => 'tag1',
'active' => 'Y',
),
1 => array(
'id' => '2',
'title' => 'title2',
'url' => 'http://www.foo.bar/',
'blurb' => 'blurb2',
'custodian' => 'custodia2',
'tags' => 'tag1,tag2',
'active' => 'Y',
),
2 => array(
'id' => '3',
'title' => 'title3',
'url' => 'http://www.foo.bar/',
'blurb' => 'blurb3',
'custodian' => 'custodia3',
'tags' => 'tag1,tag2,tag3',
'active' => 'Y',
),
);
I need to filter the array so that only the arrays with "tag2" in the tags value are displayed.
I've looked at array_filter but just can't get my head around it.
Here is my attempt but it doesn't work at all. not sure what I'm doing wrong.
$filterArr = array_filter($arr, function($tag) {
return ($tag['tags'] == 'tag2');
});
The simplest way is to use foreach loop and in the body of loop check for 'tag2'
If you need to delete all rows without tag2 in tags you can use next loop:
foreach ($arr as $key => $value) {
if (!preg_match('/\btag2\b/',$value['tags'])) {
unset($arr[$key]);
}
}
you could use array_filter and provide the right callback
$result = array_filter($arr,function($t){
return in_array('tag2',explode(',',$t['tags']));
});
array_filter takes each element of the array and passes it to the specified function, in that function you need to return true or false, true if it should stay in and false if it should be filtered out
Use explode and in_array to check for tag2
$filteredArray = array_filter($arr, "filterTag");
function filterTag($arrayElement) {
return in_array("tag2",explode(",",$arrayElement["tags"]));
}
your attempt does not work because some of your "tags" contain other words than tags2, like tag1,tag2,tag3 doing a simple == comparison does not search a string for another string
$resultArr = array();
foreach($arr as $curr)
{
$tagsData = explode(',',$curr['tags']);
if(in_array('tag1',$tagsData)
$resultArr[] = $curr;
}
$resultArr is the resulting array containing the arrays with tags having tag1
So I am building off of this question about multidimensional associative array of arrays. and what I came up with is a really simple and easy way to solve my problem. The following array:
$options = array(
'navigation' => array(
'page_title' => __('Aisis', 'aisis'),
'menu_title' => __('Aisis', 'aisis'),
'capabillity' => 'edit_themes',
'menu_slug' => 'aisis-core-options',
'function' => 'some_function',
'icon_url' => '',
'position' => '',
'sub_menues' => array(
array(
'page_title' => __('Aisis', 'aisis'),
'menu_title' => __('Aisis', 'aisis'),
'capabillity' => 'edit_themes',
'menu_slug' => 'aisis-core-options',
'function' => 'some_function',
),
array(
'page_title' => __('Aisis', 'aisis'),
'menu_title' => __('Aisis', 'aisis'),
'capabillity' => 'edit_themes',
'menu_slug' => 'aisis-core-options',
'function' => 'some_function',
),
)
),
'settings' => array(
array(
'option_group' => 'bla',
'option_name' => '',
'sanitize_call_back' => ''
)
),
'core_template' => 'path/to/admin/template.phtml'
);
Is then processed as such:
foreach($options as $settings=>$option){
if($setting = 'navigation' && is_array($option)){
foreach($option as $option_key=>$option_value){
var_dump($option);
if(!is_array($option_value)){
echo implode(',', $option);
}
}
}
}
The problem is:
in the last if statement I am stating, or at least I think I am, as long as the value for the key in the $options['navigation'] is NOT an array, implode the array and return the values. It all works as expected, accept it gives me "Array to string conversion" which it "shouldn't" due to the if statement.
So my simple question is:
How do I implode $options['navigation'] as long as the value of a key is not an array?
I thought I was on the right track....
Also, on that note, when I var_dump($option_values) I see the other arrays, so not only do I see 'sub_menues' but I also see 'settings' array
I thought my logic was sound with:
if the key is navigation, do this.
Essentially I am having a scope issue with arrays in this case, so how do I make it ONLY look at the key => values and any additional arrays with in the $options['navigation'] instead of with in $options?
Use == instead of = to compare values.
EDIT: That said, why are you using a foreach if you're only interested in one key?
if( is_array($options['navigation'])) {
foreach($options['navigation'] as $value) {
if( is_array($value)) echo implode(",",$value);
}
}
Correct solution building off of Kolink's solution:
$temp_array = array();
if( is_array($options['navigation'])) {
foreach($options['navigation'] as $value) {
if(!is_array($value)){
$temp_array[] = $value;
}
}
echo implode(',', $temp_array);
}