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);
}
Related
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
I have this variable:
$families = array(
array(
'expand' => '',
'family_id' => 'AAER',
'active' => true,
'description' => 'Wall Art',
'group_id' => 5
),
array(
'expand' => '',
'family_id' => 'EERR',
'active' => true,
'description' => 'Personalised Mugs',
'group_id' => 4
),
);
And I want add to my $families items a field called 'href', like this:
$families = array(
array(
'href' => 'http://mipage/wall-art/AAER',
'expand' => '',
'family_id' => 'AAER',
'active' => true,
'description' => 'Wall Art',
'group_id' => 5
),
array(
'href' => 'http://mipage/personalised-mug/EEER',
'expand' => '',
'family_id' => 'EERR',
'active' => true,
'description' => 'Personalised Mugs',
'group_id' => 4
),
);
To do this I iterate $families in a foreach loop:
foreach($cat['families'] as $cat_fam) {
$cat['families'][]['href'] = 'http//mysite/'.str_slug($cat_fam).'/'.$cat_fam['family_id'];
}
But this not works for me.
How can I make this?
You've to repalce empty [] with the specific key. For this update foreach block to get key of the element and use that inside foreach loop.
$cat['families'][$key] which points to individual element of the families array.
Like this,
foreach($cat['families'] as $key=>$cat_fam) {
$cat['families'][$key]['href'] = 'http//mysite/'.str_slug($cat_fam).'/'.$cat_fam['family_id'];
}
Demo: https://eval.in/636898
just iterate over the array, and add a key ahref
$newArray= array();
foreach($families as $innerArray){
$innerArray['ahref']='YOUR LINK HERE';
$newArray[] = $innerArray;
}
$families = $newArray ;//if you want to update families array
Do something like:
$href = array('href'=>'http://mipage/wall-art/AAER');
$combined_array = array_combine($families[0],$href);
Don't tested but you can try or modify as per your use
Please try this:
I think you also forgot to add index description in your str_slug call.
foreach($cat['families'] as &$cat_fam) {
$cat_fam['href'] = 'http://mysite/'.str_slug($cat_fam['description']).'/'.$cat_fam['family_id'];
}
You can use the php function array_walk
Liek this :
array_walk($cat['families'], function(&$family){
$family['href'] = 'http//mysite/'.str_slug($family).'/'.$family['family_id'];
});
note the $family variable is passed by reference.
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
I'm trying to add a key and value (associative) from an array to another array, where one specific key and value match. Here are the two arrays:
$array1 = array(
1 => array(
'walgreens' => 'location',
'apples' => 'product1',
'oranges' => 'product2'
),
2 => array(
'walmart' => 'location',
'apples' => 'product1',
'oranges' => 'product2',
'milk' => 'product3'
)
);
$array2 = array(
1 => array(
'walgreens' => 'location',
'apples' => 'product1',
'oranges' => 'product2',
'bananas' => 'product3',
)
);
Here is the attempt I made at modifying $array1 to have key 'bananas' and value 'product3':
$dataCJ = getCJItem($isbn);
foreach ($array1 as $subKey => $subArray) {
foreach($subArray as $dkey => $dval){
foreach($array2 as $cjk => $cjv){
foreach($cjv as $cjkey => $cjval){
if($dval['walgreens'] == $cjval['walgreens']){
$dval['bananas'] = $cjval['bananas'];
}
}
}
}
}
This doesn't work. How can I fix this?
Change => $dval to => &$dval. Currently you are creating and writing to a new variable and the update will not work in-place.
I would look at array_merge() function!
Here is a start with the PHP doc.
For your specific case, you could do the following :
foreach($array1 as $key1 => $values1){
foreach($array2 as $key2 => $values2){
if($values1[0] == $values2[0]){
$array1[$key1] = array_merge($values1, $values2);
}
}
}
Note to simplify the problem you should inverse the first key=> value pair of the array.
Having an array this way would be a lot simper :
array(
'location' => "The location (eg:walgreens)",
//...
);
This way you could change the comparison to the following instead :
$values1['location'] == $values2['location']
Which would be safer in the case the array is not built with the location as the first pair.
Official PHP documentation states that filter_var_array() supports array filtering in the following format:
$data = array(
'testarray' => array('2', '23', '10', '12')
);
$args = array(
'testarray' => array('filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_FORCE_ARRAY
)
);
$myinputs = filter_var_array($data, $args);
However, if the array in question is multi-dimensional and requires different filters for different parts, how would you approach defining filtering options?
As an example:
$data = array(
'testhash' => array('level1'=>'email',
'level2'=> array('23', '10', '12'))
);
Idea 1
Consider using FILTER_CALLBACK. In this way, you can write a callback function that itself uses the filter extension, thus providing a recursive ability.
function validate_array($args) {
return function ($data) use ($args) {
return filter_input_array($data, $args);
};
}
This will generate the callback functions.
$args = array(
'user' => array(
'filter' => FILTER_CALLBACK,
'options' => validate_array(array(
'age' => array('filter' => FILTER_INPUT_INT),
'email' => array('filter' => FILTER_INPUT_EMAIL)
))
)
);
This is what the config array would then look like.
Idea 2
Do not hesitate to pat me on the back for this one because I am quite proud of it.
Take an arg array that looks like this. Slashes indicate depth.
$args = array(
'user/age' => array('filter' => FILTER_INPUT_INT),
'user/email' => array('filter' => FILTER_INPUT_EMAIL),
'user/parent/age' => array('filter' => FILTER_INPUT_INT),
'foo' => array('filter' => FILTER_INPUT_INT)
);
Assume your data looks something like this.
$data = array(
'user' => array(
'age' => 15,
'email' => 'foo#gmail.com',
'parent' => array(
'age' => 38
)
),
'foo' => 5
);
Then, you can generate an array of references that map keys such as 'user/age' to $data['user']['age']. In final production, you get something like this:
function my_filter_array($data, $args) {
$ref_map = array();
foreach ($args as $key => $a) {
$parts = explode('/', $key);
$ref =& $data;
foreach ($parts as $p) $ref =& $ref[$p];
$ref_map[$key] =& $ref;
}
return filter_var_array($ref_map, $args);
}
var_dump(my_filter_array($data, $args));
Now the only question is how you deal with the mismatch between the validation record and the original data set. This I cannot answer without knowing how you need to use them.