Creating associative arrays php for dynamic accordian - php

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

Related

How to Fetch data from Array in WordPress

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

Dynamically put condition with the array as per the array value

I have the below array. I want to put condition dynamically as per the sections are coming.
stdClass Object
(
[content_form] => Array
(
[0] => genre
[1] => cast
[2] => category
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
[category] => Array
(
[0] => 4119
[1] => 4118
[2] => 4081
)
)
[conditions] => Array
(
[0] => OR
[1] => AND
)
)
In the above array for content_form there are 3 array values and in 0th index genre, 1st index cast and 2nd index category present.
Similarly for content_type there are 3 array values present and in the last array conditions present.
My requirement is that as per the section coming I want to put condition with them.
Example- ((genre OR cast) AND category)
similarly if my output is like below then the condition will be
Example - (genre OR cast)
[content_form] => Array
(
[0] => genre
[1] => cast
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
[0] => OR
)
)
And if my output is like the below then there will no condition with any other key and it will return the simple result.
stdClass Object
(
[content_form] => Array
(
[0] => cast
)
[content_type] => stdClass Object
(
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
)
)
How I can do this dynamically as per the sections are coming as per sequence with the array of content_type and conditions?
We have done this. Please check:
$jsonData = '{"content_form":["genre","cast","category", "prakash"],"content_type":{"genre":["History","ACTION","ROMANTIC"],"cast":["13128","13127"],"category":["4119","4118","4081"]},"conditions":["OR","AND","OR"]}';
$contentIds = array(
'genre'=>array(1,2,3),
'cast'=>array(1,2,4),
'category'=>array(3,2,7),
'prakash'=>array(1,2,3,8,9)
);
$arrayDecode = json_decode($jsonData,true);
$result = array();
foreach($arrayDecode['conditions'] as $key=>$val){
if($result){
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($result, $secondArr);
} else {
$firstArr = $contentIds[$arrayDecode['content_form'][$key]];
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($firstArr, $secondArr);
}
if($val=='OR'){
$result = array_unique($result);
} else {
$result = array_diff_assoc($result, array_unique($result));
}
}
$result = array_values($result);
print_r($result);
Let me know this is your requirement or not?

php formatting array results

I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}

add elements inside elements in associative array

I have an array that looks like this:
array
(
[0] => personA
[1] => personB
)
and I want to add elements to each person like this:
array
(
[0] => personA
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => personB
)
I'm using this code:
foreach($proj as $key => $cat)
{
$proj[$key] = $this->ReturnFolders(WWW_ROOT . "img/proyectos/" . $cat);
}
That function returns an array that looks like this:
array
(
[0] => elemA
[1] => elemB
)
But obviously is not working, I get this result:
array
(
[0] => Array
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => Array
)
Your "like this" structure is not possible. You cannot have a single array key have two different values like that (personA and the sub-array).
You'd have to build a more complex structure:
[0] => array(
'name' => 'personA'
'values' => array('elemA', 'elemB', 'elemC')
)

PHP - Populate new array from search result of array of objects

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

Categories