All,
I have a Widget Config array like this:
Array
(
[0] => Array ( [0] => 1 [1] => apple )
[1] => Array ( [0] => 1 [1] => orange )
[2] => Array ( [0] => 2 [1] => banana)
)
I have an array of Widget Objects like this:
Array
(
[0] => XYZ_Widget Object (
[position:XYZ_Widget:private] => 1
[widgetId:XYZ_Widget:private] => apple
)
[1] => XYZ_Widget Object (
[position:XYZ_Widget:private] => 2
[widgetId:XYZ_Widget:private] => banana
)
[2] => XYZ_Widget Object (
[position:XYZ_Widget:private] => 3
[widgetId:XYZ_Widget:private] => orange
)
)
For each array Item in Widget Config array, I need to search the array of Widget Objects for widgetId and if it's found, I need to create a new Array of Widget Objects with the found items.
Ex: The new array of Widget Objects created after searching for items in Widget Config array will look like:
Array
(
[0] => XYZ_Widget Object (
[position:XYZ_Widget:private] => 1
[widgetId:XYZ_Widget:private] => apple
)
[1] => XYZ_Widget Object (
[position:XYZ_Widget:private] => 3
[widgetId:XYZ_Widget:private] => orange
)
[2] => XYZ_Widget Object (
[position:XYZ_Widget:private] => 2
[widgetId:XYZ_Widget:private] => banana
)
)
How do I do this through PHP?
$result = array();
foreach ($configuration as $widgetConf) {
foreach ($widgets as $widget) {
if ($widgetConf[1] == $widget->widgetId) {
$result[] = $widget;
continue 2;
}
}
}
Related
Array
(
[0] => stdClass Object
(
[meta_id] => 23233
[post_id] => 4467
[meta_key] => first_name
[meta_value] => Daud
)
)
How can I echo post_id from this array for all posts using while or foreach statement?
Array
(
[classic-editor-remember] => Array
(
[0] => classic-editor
)
[_edit_lock] => Array
(
[0] => 1582905950:5
)
[_edit_last] => Array
(
[0] => 5
)
[_thumbnail_id] => Array
(
[0] => 4376
)
[slide_template] => Array
(
[0] => default
)
[_yoast_wpseo_content_score] => Array
(
[0] => 30
)
[_yoast_wpseo_primary_advisor_category] => Array
(
[0] =>
)
[title] => Array
(
[0] => Demo Daniel Wrenne, CFP, ChFC
)
[designation] => Array
(
[0] => Wrenne Financial Planing, LLC Lexington, KY
)
[client_specialities] => Array
(
[0] => Gen Y/Millennials, Medical Professionals
)
[address] => Array
(
[0] => 3223 S LEHI DR
)
[phone_number] => Array
(
[0] => 64646446486
)
[email_address] => Array
(
[0] => demo#demo.com
)
[website_url] => Array
(
[0] => a:3:{s:3:"url";s:23:"https://www.google.com/";s:4:"text";s:20:"View Advisor Profile";s:6:"target";s:4:"none";}
)
[first_name] => Array
(
[0] => Daud
)
[last_name] => Array
(
[0] => Yahya
)
)
And how can I get las_name, first_name, email, address, website url, specialities, designation and title from the above array using and loop like while or foreach loop.
This is less a WordPress question and a basic PHP foreach question.
The first example you have is an Object, so you need to access the properties, e.g. meta_id, post_id like:
// THIS IS JUST AN EXAMPLE. YOUR VARIABLE WILL CHANGE BASED ON HOW YOU GOT THE DATA. `$object_array` is how you got the data to begin with.
foreach( $object_array as $object ) {
$post_id = $object->post_id;
echo $post_id;
}
For your second example, since there is only one array key inside each array key, you would set it up like this:
// Example. you would use whatever you used to get the array to begin with as the `$array`.
foreach ($array as $item ) {
$last_name = $item['last_name'][0];
$first_name = $item['first_name'][0];
....
}
Hello I am creating a dynamic accordian with the values from database.
I am stuck in looping the array . Please help.
I have an array similar to this
Array
(
[0] => Array
(
[0] => Array
(
[0] => real estate
)
[1] => Array
(
[0] => flatvcvjh
[1] => villayuiuyidd
)
)
[1] => Array
(
[0] => Array
(
[0] => real estate
[1] => iooi
)
[1] => Array
(
[0] => y
)
)
[2] => Array
(
[0] => Array
(
[0] => real estate
[1] => iooi
[2] => painting
)
[1] => Array
(
[0] => interior
[1] => exterior
)
)
[3] => Array
(
[0] => Array
(
[0] => real estate
[1] => iooi
[2] => painting
[3] => contruction
)
[1] => Array
(
[0] => house
[1] => compound
)
)
)
Following is the loop m using to get the above array
foreach($parent_categories as $parent_category) {
$sub_categories=//array of sub cat;
$arr[]=$parent_category->name;
$scat_name="";
foreach($sub_categories as $sub_category) {
//get sub categories
$scat_name[]=$sub_category->name;
}
$subcat_name[]=$arr;
$subcat_name[]=$scat_name;
$project_categories[] = $subcat_name;
$subcat_name="";
}
echo "<pre>";
print_r($project_categories);exit;
The above loop i tried is for 2 levels.
I need a 3 dimensional array with 3 levels of categories
like
category1
subcat1
subcat2
subsubcat1
subsubcat2
subcat3
what changes will i have to make in the above loop I tried number of ways the above is what I get. Please help
Use recursive function
$menusList = array();
function dynamic_menus($menus) {
foreach($menus as $index => $subMenu) {
menusList[$index] = is_array($subMenu) ? dynamic_menus($subMenu) : $subMenu;
}
return menusList;
}
you need to convert array to object first beacause you use
$sub_category->name is to read object type
Multidimensional array to object, specific way
http://php.net/manual/en/language.types.object.php
In my PHP application, get the results from DB. After processing the results I need to convert the results like below using foreach
Array
(
[1] => Array -----> This is intent 1, this key indicates all intent values which is equal to 1, should belongs to here.
(
[0] => Array
(
[name] => A
[indent] => 1
)
[1] => Array
(
[name] => B
[indent] => 1
)
)
[2] => Array
(
[0] => Array
(
[name] => B
[indent] => 2
)
[1] => Array
(
[name] => A
[indent] => 2
)
)
[3] => Array
(
[0] => Array
(
[name] => A
[indent] => 3
)
)
)
That I have some intent value common, common intent values are stored in array like array('1'=> array(array[0],array[1]));.
What I tried is
foreach($results as $data){
$root_array[$data['intent']] = array($data);
}
This will replace the old array and insert the last intent value which is common.
I get result like below, the intent 1 and intent 2 are replaced with last data
Array
(
[1] => Array
(
[0] => Array
(
[name] => B
[indent] => 1
)
)
[2] => Array
(
[0] => Array
(
[name] => A
[indent] => 2
)
)
[3] => Array
(
[0] => Array
(
[name] => A
[indent] => 3
)
)
)
In the loop you must check if the current indent has been initialized. If not then create it, else just append the new data to it.
foreach($results as $data) {
if (!isset($root_array[$data['indent']])) {
$root_array[$data['indent']] = array($data);
} else {
$root_array[$data['indent']][] = $data;
}
}
I have two arrays and looking for the way to merge them. Standard array_merge() function don't work.
Do you know any nice solution without foreach iteration?
My first array:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] =>
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] =>
)
)
My second array:
Array
(
[0] => 2
[1] => 7
)
And as a result I would like to get:*
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
This should work for you:
Just simply loop through both arrays with array_map() and pass the argument from array one as reference. Then you can simply assign the value to the count property.
<?php
array_map(function(&$v1, $v2){
$v1->count = $v2;
}, $arr1, $arr2);
print_r($arr1);
?>
output:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
[akshay#localhost tmp]$ cat test.php
<?php
$first_array = array(
(object)array("field_value"=>"green","count"=>null),
(object)array("field_value"=>"yellow","count"=>null)
);
$second_array = array(2,7);
function simple_merge($arr1, $arr2)
{
return array_map(function($a,$b){ $a->count = $b; return $a; },$arr1,$arr2);
}
print_r($first_array);
print_r($second_array);
print_r(simple_merge($first_array,$second_array));
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => stdClass Object
(
[field_value] => green
[count] =>
)
[1] => stdClass Object
(
[field_value] => yellow
[count] =>
)
)
Array
(
[0] => 2
[1] => 7
)
Array
(
[0] => stdClass Object
(
[field_value] => green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => yellow
[count] => 7
)
)
it is simple
code:
$i = 0;
foreach($firstarrays as $firstarr)
{
$firstarr['count'] = $secondarray[$i];
$i++;
}
Another option:
$a1 = Array(
(object) Array('field_value' => 'Green', 'count' => null),
(object) Array('field_value' => 'Yellow', 'count' => null)
);
$a2 = Array(2, 7);
for ($i=0; $i<sizeof($a1); $i++) {
$a1[$i]->count=$a2[$i];
}
I've spent the day playing with deceze's answer but I'm no closer to making it work. I may have part of it, but not sure how to get recursion in array_filter.
My Array looks like this (sample):
Array
(
[name] => root
[ChildCats] => Array
(
[0] => Array
(
[name] => Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Ducted Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
)
)
[1] => Array
(
[name] => Supply Only
[ChildCats] => Array
(
[0] => Array
(
[name] => Mitsubishi
[S] => 6026
)
)
)
)
)
[1] => Array
(
[name] => Split System Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
[1] => Array
(
[name] => Fujitsu Split Air Conditioning Systems
[S] => 6464
)
[2] => Array
(
[name] => Mitsubishi Electric Split Air Conditioning Systems
[S] => 6464
)
)
)
)
)
)
)
[1] => Array
(
[name] => Appliance / White Goods
[ChildCats] => Array
(
[0] => Array
(
[name] => Clearance
[S] => 6239
)
[1] => Array
(
[name] => Cooktops
[ChildCats] => Array
(
[0] => Array
(
[name] => Ceramic Cooktops
[S] => 6239
)
[1] => Array
(
[name] => Element Cooktops
[S] => 6067
)
[2] => Array
(
[name] => Gas Cooktops
[S] => 6239
)
[3] => Array
(
[name] => Induction Cooktops
[S] => 6239
)
)
)
)
)
Now lets say I try to extract just the parts of the array relevent to the following keypair:
S => 6067.
I'd like the result to look like:
Array
(
[name] => root
[ChildCats] => Array
(
[0] => Array
(
[name] => Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Ducted Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
)
)
)
)
[1] => Array
(
[name] => Split System Air Conditioning
[ChildCats] => Array
(
[0] => Array
(
[name] => Supply & Install
[ChildCats] => Array
(
[0] => Array
(
[name] => Daiken
[S] => 6067
)
)
)
)
)
)
)
[1] => Array
(
[name] => Appliance / White Goods
[ChildCats] => Array
(
[0] => Array
(
[name] => Cooktops
[ChildCats] => Array
(
[0] => Array
(
[name] => Element Cooktops
[S] => 6067
)
)
)
)
)
)
)
What I cannot get my head arround is should I be creating a new array or using array filter.
Playing with deceze code I've got the search working using the following:
function recursive_assoc_in_array(array $haystack, array $needle, $childKey = 'ChildCats') {
if (array_intersect_assoc($haystack, $needle)) {
echo "Found via array_intersect_assoc ".$haystack[name]."\n";
return true;
}
foreach ($haystack[$childKey] as $child) {
if (recursive_assoc_in_array($child, $needle, $childKey)) return true;
}
return false;
}
But if I try to process with,
$array = array_filter($array, function (array $values) {
return recursive_assoc_in_array($values, array('S' => '6067'));
});
I get the original array which leads me to think I have to get recursion running on the array_filter query.
At this point I just go blank.
Additionally, the array keys will need to be reindexed on the produced new array. Any ideas?
--Additional 7/7/14
How about if I try to build a new array from the old one?
I'm trying:
function exploreArrayandAdd($Array) {
if ($Array['ChildCats']) {
foreach ($Array['ChildCats'] as $key => $value) {
$NewArray['ChildCats'][] = exploreArrayandAdd($value);
}
} else {
if ($Array['S'] == 6026) {
//echo "1";
return $Array;
}
}
}
But cannot work out how to pass the new array out of the function?
Tried removing branches that don't match using:
function exploreArray(&$Array) {
if ($Array['ChildCats']) {
foreach ($Array['ChildCats'] as $key => $value) {
$result = exploreArray($Array['ChildCats'][$key]);
if ($result === false)
unset($Array['ChildCats'][$key]);
}
} else {
// print_r($Array);
if ($Array['S'] == 6026) {
return true;
} else {
unset($Array);
return false;
}
}
//if ($NoChildCat==true) print_r($Array);
}
But I believe it is the wrong way as it does work at the bottom of the array but not back up towards the top as siblings make result true.
Also this won't reindex the array keys.
function recursive_array_filter(array $array, $childKey, callable $test) {
if (isset($array[$childKey]) && is_array($array[$childKey])) {
foreach ($array[$childKey] as $key => &$child) {
if (!$child = recursive_array_filter($child, $childKey, $test)) {
unset($array[$childKey][$key]);
}
}
if (!$array[$childKey]) {
unset($array[$childKey]);
}
}
return !empty($array[$childKey]) || $test($array) ? $array : [];
}
$array = recursive_array_filter($array, 'ChildCats', function (array $array) {
return array_intersect_assoc($array, ['S' => 6026]);
});
To express the algorithm in words: you descend down into the array first, following all ChildCats branches to their end. In each level you return the values as they are back to the caller if they match your test or if they have children, or you return an emptied array (you could also return false if you prefer). If some child turns out empty, you prune it with unset.
I have implemented the test as a callback function here for best reusability of the code.