I try to sort an array by name. I want to create a structure similar to a menu. First, I allow a function to write values into the array. Then I would like to assign all subpages to the parent (resulting from the structure, such as a URL).
Out
array
(
[0] => array
(
[Pages] => coreViewSites
)
[1] => array
(
[Pages / Create] => create coreView
)
[2] => array
(
[Pages / Duplicate] => coreViewSites
)
[3] => array
(
[Pages / Anarchy] => coreViewSites
)
[4] => array
(
[User] => coreViewUser
)
)
should an array like
array
(
[Pages] => Array
(
[0] => ABC
[Create] => ABC
[Duplicate] => ABC
[Anarchy] => ABC
)
[User] => ABC
)
become.
Do you have an idea how I could solve this?
Assuming the menu items are ordered with parents coming before children, here is how you could do that:
$menu = [];
foreach($input as $path) {
$keys = explode(" / ", key($path)); // Extract the individual menu titles
$last = array_pop($keys); // Pull the last one from it
$loc = &$menu;
foreach($keys as $key) {
// Create menu item if it does not exist yet
if (!isset($loc[$key])) $loc[$key] = [];
// When a menu gets sub-menu, move the title in index 0 of a new array
if (!is_array($loc[$key])) $loc[$key] = [$loc[$key]];
$loc = &$loc[$key]; // Set the pointer to that submenu
}
$loc[$last] = reset($path); // At the deepest level assign the menu title.
}
// Print result:
var_export($menu);
Output:
array (
'Pages' => array (
0 => 'coreViewSites',
'Create' => 'create coreView',
'Duplicate' => 'coreViewSites',
'Anarchy' => 'coreViewSites',
),
'User' => 'coreViewUser',
)
Related
I am trying to create an array(if it does not already exist) and then push values to it.
foreach($playlist->items as $item) {
$str = $item->snippet->title;
$id = $item->snippet->resourceId->videoId;
$substring = substr($str, 0, 5);
$substring = strtolower($substring);
if (is_array($substring)) {
array_push($substring, $id);
}
else {
$substring = array();
array_push($substring, $id);
}
array_push($artists, $substring);
}
I am iterating through data retrieved from a Youtube playlist, so I go through each item with foreach which holds a 'title' - the artist and an 'id' - the video Id . I substring each title and try to use this to group artists into specific arrays.
If an array already exists for that artist, I try to push the 'id' onto the end of that array. If an array does not exist, I create one and then push the 'id' onto that array.
At the end I try to push each artist array into the 'artists' array.
What I get when I print out $artists array is something like this
Array
(
[0] => Array
(
[0] => 1_YUrdjLyAU
)
[1] => Array
(
[0] => Gp8lDW2LUM0
)
...
[543] => Array
(
[0] => Exa0CzlCb3Y
)
Every single $id is in it's own array when they should be grouped together based on $substring. e.g
Array
(
[0] => Array
(
[0] => 1_YUrdjLyAU
[1] => 1_YUrdjLyAU
[2] => 1_YUrdjLyAU
[3] => 1_YUrdjLyAU
[4] => 1_YUrdjLyAU
)
[1] => Array
(
[0] => Gp8lDW2LUM0
[1] => 1_YUrdjLyAU
[2] => 1_YUrdjLyAU
[3] => 1_YUrdjLyAU
)
What am I not understanding?
Here is a simpler solution to your problem:
$artists = array();
foreach($playlist->items as $item) {
$artist = $item->snippet->artist; // however the artist name is fetched..
$id = $item->snippet->resourceId->videoId;
$artists[$artist][] = $id
}
This way, you don't need to check if an artist is already in the array, it will do that automatically and append the video id to the artist.
The $artists array will be assosiative, I don't think you can do it with a numeric array.
The array will look like this:
Array
(
['Jon Lajoie'] => Array
(
[0] => 1_YUrdjLyAU
[1] => lf3hflkap39
[2] => 1vt1455zzbe
[3] => 6dthg3drgjb
[4] => jfop3ifjf3p
)
['Lonely Island'] => Array
(
[0] => Gp8lDW2LUM0
[1] => 5he5hj67j7r
[2] => krt7tkktzk8
[3] => we54w4ggsrg
)
)
Use substring as array key and do following:
**Remove**
if (is_array($substring)) {
array_push($substring, $id);
}
else {
$substring = array();
array_push($substring, $id);
}
array_push($artists, $substring);
**Replace**
$artist[$substring][]=$id;
Consider the array is:
Array
(
[Page-1] => Array
(
[0] => Array
(
[0] => Cat-1
[1] => Item-1
)
)
[Page-2] => Array
(
[0] => Array
(
[0] => Cat-2
[1] => Item-2
)
[1] => Array
(
[0] => Cat-3
[1] => Item-3
)
[2] => Array
(
[0] => Cat-4
[1] => Item-4
)
)
[Page-3] => Array
(
[0] => Array
(
[0] => Cat-5
[1] => Item-5
)
)
[Page-4] => Array
(
[0] => Array
(
[0] => Cat-6
[1] => Item-6
)
)
[Page-5] => Array
(
[0] => Array
(
[0] => Cat-7
[1] => Item-7
)
[1] => Array
(
[0] => Cat-9
[1] => Item-9
)
)
[Page-6] => Array
(
[0] => Array
(
[0] => Cat-8
[1] => Item-8
)
)
)
Where, the first keys [Page-x] from array will be Main-Links in the navigation menu.
Some of the main links may have Sub-Links, some not.
Sub-links are the values of the key [0] of the 3rd sub-array.
And finally the URL for each and every link will be the value of key [1] of the 3rd sub-Array.
Only Pages that have more than one category will show its categories as sub-links
The navigation bar i would like to have:
1. Page-1
2. Page-2
Cat-2
Cat-3
Cat-4
3. Page-3
4. Page-4
5. Page-5
Cat-7
Cat-9
6. Page-6
the PHP code
$records = $p->main_links();
foreach ($records as $key => $value) {
$return[$value['page']][] = array($value['child'], $value['item']);
}
foreach ($return as $key2 => $value2) {
$count = 0;
/* Select a specific value within the Array */
$main_links = $value2[$count][1]; /* URL of the main Pages */
$count = count($return[$key2]);
if($count > 1) {
foreach ($value2 as $key3 => $value3)
{
$link_name = $value3[0]; /* Child Link Names */
$link_url = $value3[1]; /* URL of Child Links */
/* addedd htmlspecialchars() function to $variables that will be echoed into HTML. It provides some XSS protection */
$cat_link .= '<li>'.htmlspecialchars($link_name).'</li>';
}
$result .= '
<li '.htmlspecialchars($li_class).'><span>'.htmlspecialchars($key2).'</span>
<ul>
'.$cat_link.'
</ul>
</li>';
}else {
$result .= '
<li><span>'.htmlspecialchars($key2).'</span></li>';
}
}
Unfortunately i can't get it work... the output is not what i am expecting :(
current Output (wrong one):
1. Page-1
2. Page-2
Cat-2
Cat-3
Cat-4
3. Page-3
4. Page-4
5. Page-5
Cat-2
Cat-3
Cat-4
Cat-7
Cat-9
6. Page-6
Any help would be appreciated!
Your current code is close to working. This line will always produce a count of 1.
$count = count($value);
What you're looking for there, I believe, is:
$count = count($return[$key]);
I've found another way around which is way better than the one i was trying to do. This solved my case.
http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu
Thank you for your support!
I have got this array
Array
(
[0] => Array
(
[category_name] => Dessert1
[totalOrders] => 3
)
[1] => Array
(
[category_name] => Dessert1
[totalOrders] => 1
)
[2] => Array
(
[category_name] => Category 3
[totalOrders] => 1
)
)
and I want to convert it into this array
Array
(
[0] => Array
(
[category_name] => Dessert1
[totalOrders] => 4
)
[1] => Array
(
[category_name] => Category 3
[totalOrders] => 1
)
)
It is really rather simple. You just loop over your data and pick out the unique categories. When there are duplicates add the orders to the category's total.
// The stuff from your post
$data = array(
array('category_name' => 'Dessert1', 'totalOrders' => 3),
array('category_name' => 'Dessert1', 'totalOrders' => 1),
array('category_name' => 'Category 3', 'totalOrders' => 1),
);
// Auxiliary variable
$result = array();
// Go over the data one by one
foreach ($data as $item)
{
// Use the category name to identify unique categories
$name = $item['category_name'];
// If the category appears in the auxiliary variable
if (isset($result[$name]))
{
// Then add the orders total to it
$result[$name]['totalOrders'] += $item['totalOrders'];
}
else // Otherwise
{
// Add the category to the auxiliary variable
$result[$name] = $item;
}
}
// Get the values from the auxiliary variable and override the
// old $data array. This is not strictly necessary, but if you
// want the indices to be numeric and in order then do this.
$data = array_values($result);
// Take a look at the result
var_dump($data);
I'm trying to experiment with array_splice and I get an output like this (from $match)
Array
(
[Keep me Updated] => Array
(
[winner] => winnerl.jpg
[0] => value0.jpg
)
[0] => valuel.jpg //this should really be inside [Leep me Updated] array
[1] => value2.jpg //this should really be inside [Leep me Updated] array
[2] => value3.jpg //this should really be inside [Leep me Updated] array
}
from (this foreach creates puts in the values into $match)
foreach($data as $d)
{
if (isset($match[$d['data']['name']])) {
$match_loser = array($d['loser']['lrg_img']);
array_splice($match,1,0,$match_loser);
}else{
$match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
$d['loser']['lrg_img']);
}
}
What I'm trying to get is bring [0],[1],[2] into the [Keep me Updated] $match array:
Array
(
[Keep me Updated] => Array
(
[winner] => winnerl.jpg
[0] => value0.jpg
[1] => value1.jpg // old one: [0] => valuel.jpg
[2] => value2.jpg // old one: [1] => value2.jpg
[3] => value3.jpg // old one: [2] => value3.jpg
)
}
This is what $data looks like
$data[] = array(
"data"=>array
(
"name"=>$name,
),
"winner"=>array
(
"lrg_img"=>$img_url_winner
),
"loser"=>array
(
"lrg_img"=>$img_url_loser
)
$data has array values, and $match is where I'm trying to sort the data. So if my values match, it'll consolidate.
Thanks!
Use the inner array as the argument to array_splice
foreach($data as $d)
{
if (isset($match[$d['data']['name']])) {
$match_loser = array($d['loser']['lrg_img']);
array_splice($match[$d['data']['name']],1,0,$match_loser);
}else{
$match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
$d['loser']['lrg_img']);
}
}
I've got an multi-array (currently with objects) that I want to reorder based on a specific key/value.
Array
(
[0] => stdClass Object
(
[task_id] => 1
[task_title] => Title
[users_username] => John
)
[1] => stdClass Object
(
[task_id] => 2
[task_title] => Title
[users_username] => John
)
[2] => stdClass Object
(
[task_id] => 3
[task_title] => Title
[users_username] => Mike
)
)
I'd like to reorder it to get multi-arrays by user_name, so I can cycle through the task by username.
Array
(
[John] => Array
(
[0] => Array
(
[task_id] => 1
[title] => Title
)
[1] => Array
(
[task_id] => 2
[title] => Title
)
)
[Mike] => Array
(
[0] => Array
(
[task_id] => 3
[title] => Title
)
)
)
Is it possible to recreate my array to an array like that above?
Updated version of the code
<?php
$it0 = (object) array('task_id' => 1,'task_title' => 'Title','users_username' => 'John');
$it1 = (object) array('task_id' => 2,'task_title' => 'Title','users_username' => 'John');
$it2 = (object) array('task_id' => 3,'task_title' => 'Title','users_username' => 'Mike');
$array = array($it0,$it1,$it2);
$return = array();
foreach($array as $id => $value){
$return[$value->users_username][] = array('task_id' => $value->task_id,'title' => $value->task_title);
}
var_dump($return);
Yes, it is possible.
You'll have to loop through your current array and create a new array to do it.
example:
$new_array = array();
foreach ($array as $row)
{
$new_row = array(
'task_id' => $row->task_id,
'title' => $row->task_title,
);
$name = $row->users_username;
if (isset($new_array[$name]))
{
$new_array[$name][] = $new_row;
}
else
{
$new_array[$name] = array($new_row);
}
}
Now $new_array contains the new array exactly like the one you're asking for.
Then you can sort it with
ksort($new_array);
There may be another way to do this, with some built-in function, but sometimes I'd rather just do it myself, and know how it is working, without having to look up the documentation.
The approach:
Iterate through all of the first array, looking at [users_username] and putting them into a new array.
Code:
$dst_array = array();
foreach ($src_array as $val)
{
$user = $val->users_username;
// TODO: This check may be unnecessary. Have to test to find out.
// If this username doesn't already have an array in the destination...
if (!array_key_exists($user, $dst_array))
$dst_array[$user] = array(); // Create a new array for that username
// Now add a new task_id and title entry in that username's array
$dst_array[$user][] = array(
'task_id' => $val->task_id
'title' => $val->title
);
}
Just something like this (maybe not 100% PHP code):
foreach ( $obj : $oldArray ) {
$newTask['task_id'] = $obj->task_id;
$newTask['title'] = $obj->title;
$newArray[$oldName][] = $newTask;
}
If you want to order it; you can just call a order function afterwards.