PHP splitting arrays into groups based on one field's value - php

I have an array containing arrays of names and other details, in alphabetical order. Each array includes the first letter associated with the name.
Array
(
[0] => Array
(
[0] => a
[1] => Alanis Morissette
)
[1] => Array
(
[0] => a
[1] => Alesha Dixon
)
[2] => Array
(
[0] => a
[1] => Alexandra Burke
)
[3] => Array
(
[0] => b
[1] => Britney Spears
)
[4] => Array
(
[0] => b
[1] => Bryan Adams
)
)
I'd like to display them grouped by that first initial, eg:
A
-
Alanis Morissette
Alesha Dixon
Alexandra Burke
B
-
Britney Spears
Bryan Adams
etc...
Is this at all possible?

You can group them easily, even if they aren't sorted:
$groups=array();
foreach ($names as $name) {
$groups[$name[0]][] = $name[1];
}
You don't even need to store the first initial to group them:
$names = array(
'Alanis Morissette',
'Alesha Dixon',
'Alexandra Burke',
'Britney Spears',
'Bryan Adams',
...
);
$groups=array();
foreach ($names as $name) {
$groups[$name[0]][] = $name;
}

Since your array is already sorted, you could just loop through and track the last letter shown. When it changes, you know you're on the next letter.
$lastChar = '';
foreach($singers as $s) {
if ($s[0] != $lastChar) echo "\n".$s[0]."\n - \n";
echo $s[1]."\n";
$lastChar = $s[0];
}

Related

Taking out just one value from an array

I have an array which is
Array ( [0] => Array ( [picture] => 5a55ed8d8a5c8910913.jpeg
[id] => 1284
[price_range] => Rs 12000 - 9000
[name] => Brown Beauty Office Chair )
[1] => Array ( [picture] => 5a55eefeb9a8e255836.jpeg
[id] => 1285
[price_range] => Rs 8989 - 7000
[name] => Chang Series Office Chair (Grey)
)
)
Now I am fetching the value of id on clicking a remove button, the value I fetch is 1284.
I want to take out just [id]=> 1284 from the above array and then display it using a foreach loop. How I can delete just the [id]=> 1284 without disturbing the other id values and other element.
In the above array I would like to delete one particular id value say just the [id]=> 1284 and keep all other elements intact and as it is.
Any help is welcome.
Use array_search and array_column, to find by id and remove by unset method,
<?php
$array = [
["id"=>123,"desc"=>"test1"],
["id"=>456,"desc"=>"test2"],
["id"=>789,"desc"=>"test3"],
];
$id = 456;
$index = array_search($id, array_column($array, 'id'));
unset($array[$index]);
print_r($array);
?>
Live Demo
Array
(
[0] => Array
(
[id] => 123
[desc] => test1
)
[2] => Array
(
[id] => 789
[desc] => test3
)
)
Since you asked how to achieve it using foreach, I came up with this.
$array = Array (Array ( 'picture' => '5a55ed8d8a5c8910913.jpeg','id' => 1284,'price_range' => 'Rs 12000 - 9000', 'name' => 'Brown Beauty Office Chair'),
Array ( 'picture' => '5a55eefeb9a8e255836.jpeg','id' => 1285,'price_range' => 'Rs 8989 - 7000','name' => 'Chang Series Office Chair (Grey)')
);
foreach($array as $key => $val) {
$id = $array[$key]['id'];
if($id === 1284){
unset($array[$key]['id']);
}
}
print_r($array)
?>
You can also use this too:
<?php
$element_to_remove = 1284;
$i = 0;
foreach($array as $this_arr){
$index = array_search($element_to_remove, $this_arr);
//unset($this_arr[$index]); this formate does not remove element from array
//but below works fine
if(isset($array[$i][$index])){
unset($array[$i][$index]);
}
}
print_r($array);
?>

Organize or group data from multidimensional array

I have a multidimensional array that I'm having difficulty trying to group and sort for a particular need. Here is the array:
Array (
[0] => Array (
[0] => Joe Smith
[1] => Array (
[0] => 3
[1] => 9
)
)
[1] => Array (
[0] => John Doe
[1] => Array (
[0] => 6
[1] => 12
)
)
[2] => Array (
[0] => Jack Frost
[1] => Array (
[0] => 2
[1] => 4
)
)
)
What I want to do is sort the numbers from smallest to shortest (i.e. 2,3,4,6,9,12), but also keep the names associated with those numbers. For example:
2 (Jack Frost),
3 (Joe Smith),
4 (Jack Frost),
6 (John Doe),
9 (Joe Smith),
12 (John Doe)
Any ideas how to sort by number and keep the names together? Thanks
UPDATE 1
Here is the PHP code I've used to list the numbers in order:
$users = get_users();
$names = array();
$days = array();
foreach( $users as $user ) {
$names[] = $user->display_name;
$days[] = $user->member_day;
}
$result = array_map( null, $names, $days );
$mdays = array();
foreach( $days as $d ) {
foreach( $d as $d2) {
$mdays[] = $d2;
}
}
for( $i; $i<=31; $i++ ) {
if( in_array($i, $mdays) ) {
echo $i . '<br>';
}
}
In the above code, $result prints out the above Array. Also, the for loop sorts the "days".
The end goal is to have 31 blocks and fill in the block by number with the name.
You can do something like as
$result = [];
foreach ($arr as $key => $value) {
foreach ($value[1] as $v) {
$result[$v] = $value[0];
}
}
ksort($result);
print_r($result);
Output:
Array
(
[2] => Jack Frost
[3] => Joe Smith
[4] => Jack Frost
[6] => John Doe
[9] => Joe Smith
[12] => John Doe
)
Note: This'll work fine till none of the array array contains the same key
something like this should work, and at the end you can soert you array
$final_array=array();
foreach (array as $arr){
foreach($arr as $aaa){
$final_array[]=array($aaa,$arr[0])
}
}

Parsing array values into multidimensional array

I have some data I retrieve from a JSON feed that currently is being parsed into an array like this: (simplifying for demonstration purposes)
So pretty much an array returns a movie theater name, with the showtimes associated with that particular theater.
[0] => American Theater
[1] => 2014-06-04T13:10
[2] => 2014-06-04T15:10
[3] => Grand Theater
[4] => 2014-06-04T15:30
[5] => 2014-06-04T19:10
How would I parse this array to be multidimensional, for instance:
Array
(
[0] => Array
(
[theater] => Array
(
[name] => American Theater
)
[showtimes] => Array
(
[1] => 2014-06-04T13:10
[2] => 2014-06-04T15:10
)
)
[1] => Array
(
[theater] => Array
(
[name] => Grand Theater
)
[showtimes] => Array
(
[1] => 2014-06-04T15:30
[2] => 2014-06-04T19:10
)
)
)
I'm assuming you're trying to access some api and have no control over how the data is passed back to you? If you do then the API should be responsible for returning a sensible schema.
But if you're forced to work with this array and the amount of showtimes are unknown to you, then you can do something like this:
$array = array(
'American Theater',
'2014-06-04T13:10',
'2014-06-04T15:10',
'Grand Theater',
'2014-06-04T15:30',
'2014-06-04T19:10'
);
$i = 0;
foreach ($array as $key => $value) {
if (strtotime($value)) {
$theaters[$i - 1]['showtimes'][] = $value;
}
else {
$theaters[$i]['theater']['name'] = $value;
$i++;
}
}
Outcome
To walk you through it, $array is whatever the returned dataset is. We set an index in the $i value and want to only increment it if we determine we've detected a theater name. Within the loop we first try to determine if the string can be converted to a php time value. If it cannot we add the theater name to our new schema structure, and increment our index value. Since times are always added to theater names, we are expecting the first index number to always be one higher than what we want to add the showtime to.
This will fail to be accurate in cases when a theater name is convertible to a time value in such cases like Next Month. There are a couple of other ways to solve this with regex or by inspecting the string for certain characters and their position since the time format will remain the same.
You could replace the strtotime() with:
$str = str_split($value);
if (($str[4] && $str[7]) == '-' && $str[10] == 'T' && $str[13] == ':' ) {
$theaters[$i - 1]['showtimes'][] = $value;
}
If you want such structure, you need to create a new copy of it. You may also need to chunk/group them by three's using array_chunk first, and then, from there, you can loop it now and start creating the desired format.
Consider this example:
$old_values = array('American Theater', '2014-06-04T13:10', '2014-06-04T15:10', 'Grand Theater', '2014-06-04T15:30', '2014-06-04T19:10');
$old_values = array_chunk($old_values, 3);
$new_values = array();
foreach($old_values as $key => $value) {
$new_values[] = array(
'theater' => array('name' => $value[0]),
'showtimes' => array(1 => $value[1], 2 => $value[2]),
);
}
Edit: As mentioned, one theater can have many showtimes, therefore this current solution will fail. This may be an alternative (you may need to check each element if its a theater name or a date). Consider this example:
$old_values = array(
'American Theater',
'2014-06-04T13:10',
'2014-06-04T15:10',
'Grand Theater',
'2014-06-04T15:30',
'2014-06-04T19:10',
'Magic Johnson Theater',
'2014-06-04T19:10',
'2014-06-04T19:10',
'2014-06-04T19:10',
'Mall of America Theater',
'2014-06-04T19:10',
'2014-06-04T19:10',
'2014-06-04T19:10',
'2014-06-04T19:10',
);
$new_values = array();
$current_key = 0;
foreach($old_values as $key => $value) {
$current_value = $value;
$pieces = explode('T', $current_value);
$dates = explode('-', $pieces[0]);
if(count($dates) == 3) {
$new_values[$current_key]['showtimes'][] = $current_value;
} else {
$current_key++;
$new_values[$current_key]['theater']['name'] = $current_value;
}
}
Sample Output:
Array
(
[1] => Array
(
[theater] => Array
(
[name] => American Theater
)
[showtimes] => Array
(
[0] => 2014-06-04T13:10
[1] => 2014-06-04T15:10
)
)
[2] => Array
(
[theater] => Array
(
[name] => Grand Theater
)
[showtimes] => Array
(
[0] => 2014-06-04T15:30
[1] => 2014-06-04T19:10
)
)
[3] => Array
(
[theater] => Array
(
[name] => Magic Johnson Theater
)
[showtimes] => Array
(
[0] => 2014-06-04T19:10
[1] => 2014-06-04T19:10
[2] => 2014-06-04T19:10
)
)
[4] => Array
(
[theater] => Array
(
[name] => Mall of America Theater
)
[showtimes] => Array
(
[0] => 2014-06-04T19:10
[1] => 2014-06-04T19:10
[2] => 2014-06-04T19:10
[3] => 2014-06-04T19:10
)
)
)
Sample Fiddle

How to print out Multi-Dimensional Arrays in php

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!

Convert complex numerical array to associative array

I have an array data that look like this :
Array (
[0] => Array (
[0] => Name:
[1] => John W.
[2] => Registration ID:
[3] => 36
)
[1] => Array (
[0] =>Age:
[1] => 35
[2] => Height:
[3] => 5'11"
)
[3] => Array (
[0] => Sex:
[1] => M
[2] => Weight:
[3] => 200lbs
)
[4] => Array (
[0] => Address
)
[5] => Array (
[0] => 6824 crestwood dr delphi, IN 46923
))
And I want to convert it to associative array like this :
Array(
['Name']=> John W.
['Registration ID']=> 36
['Age']=> 35
['Height'] => 5'11''
['Sex']=>M
['Weight']=>200lbs
['Address']=>6824 crestwood dr delphi, IN 46923
)
I have no idea at all how to do this, since the supposed to be array column header were also in sequence, so it makes difficult to convert this array.
Any help I appreciate, thx.
Given your origin array is called $origin , you can do it like this:
$merged = array();
foreach($origin as $val) {
$merged = array_merge($merged, $val);
}
$tot = count($merged) - 1;
for ($i=0;$i<$tot;$i+=2) {
$result[$merged[$i]] = $merged[$i+1];
}
var_dump($result); // To test the resulting array
Firstly, I use array_merge() to flatten the $origin array to only one dimension/depth, so we later iterate it (stepping each 2 items per iteration) and assigning each pair of items ($i and $i+1) to the resulting array.
Looks like, for the first 3 children, you can just assign the even value to the previous element as key. Then, assign the fourth one as key for fifth element.
$result = array();
foreach ($array as $key => $value)
{
if ($key < 4) {
$elements = array_values($value);
$result[$elements[0]] = $elements[1];
$result[$elements[2]] = $elements[3];
}
if ($key == 4)
$fifthkey = $value;
if ($key == 5)
$result[$fifthkey] = $value;
}
Also, note that you have to escape your height string quotes.

Categories