I'd like to take an array with this structure:
array
'Alabama' =>
array
0 =>
array
'id' => string '11' (length=2)
'region_name' => string 'Alabama' (length=7)
'city' => string 'Birmingham' (length=10)
1 =>
array
'id' => string '12' (length=2)
'region_name' => string 'Alabama' (length=7)
'city' => string 'Huntsville' (length=10)
2 =>
array
'id' => string '13' (length=2)
'region_name' => string 'Alabama' (length=7)
'city' => string 'Mobile' (length=6)
3 =>
array
'id' => string '14' (length=2)
'region_name' => string 'Alabama' (length=7)
'city' => string 'Montgomery' (length=10)
'Alaska' =>
array
0 =>
array
'id' => string '15' (length=2)
'region_name' => string 'Alaska' (length=6)
'city' => string 'Anchorage' (length=9)
And create unordered lists in html, like so:
<ul id="A">
<li class="state">Alabama</li>
<li>Birmingham</li>
<li>Huntsville</li>
<li>Mobile</li>
<li>Montgomery</li>
<li class="state">Alaska</li>
<li>Anchorage</li>
</ul>
<ul id="C">
<li class="state">California</li>
<li>Bakersfield</li>
<li>Fresno</li>
<li>Los Angeles</li>
</ul>
<ul id="D">
<li class="state">DC</li>
<li>Washington</li>
</ul>
The idea is an alphabetically ordered and grouped series of unordered lists, which I can show and hide easily using javascript. That part is easy... This part, I'm lost.
I've tried a sort of nested foreach loop, but the framework I'm using refused to do it citing OutputEscaper errors, which I believe made sense - I really am not sure how to do this properly.
I'd appreciate any help!
edit: Here's how the array is initially formatted:
$this->cityGroups = array();
foreach($this->USCities as $city)
{
$this->cityGroups[$city['region_name']][] = $city;
}
This is simple and it doesn't need a framework. If you data was formatted as I mentioned in my comment...
$data = array('Alabama' => array('Birmingham', 'Huntsville', 'Mobile', 'Montgomery'),
'Alaska' => array('Anchorage'));
ksort($data);
$formatted = array();
foreach($data as $state => $cities) {
$formatted[$state{0}][$state] = $cities;
}
foreach($formatted as $letter => $states) {
echo '<ul id="'.$letter.'">';
foreach($states as $state => $cities) {
sort($cities);
echo '<li class="state">'.$state.'</li>'
foreach($cities as $city) {
echo '<li>'.$city.'</li>';
}
}
echo '</ul>';
}
This solution should achieve what you're looking for:
$lastLetter = "A";
print( '<ul id="A">' );
foreach( $myArray as $key => $array ){
if( strtoupper( substr( $key, 0, 1 ) ) != $lastLetter ){
print( '</ul>' );
$lastLetter = strtoupper( substr( $key, 0, 1 ) );
print( '<ul id="' . $lastLetter . '">' );
}
print( '<li class="state">' . $key . '</li>' );
foreach( $array as $subArr ){
print( '<li>' . $subArr['city'] . '</li>' );
}
}
print( '</ul>' );
If you need an explanation on any specific part, please let me know.
Alternate Solution, in the event your starting letter is variable (may or may not be A)
Change:
$lastLetter = "A";
print( '<ul id="A">' );
to:
$temp = array_keys( $myArray );
$lastLetter = strtoupper( substr( $temp[0], 0, 1 ) );
print( '<ul id="' . $lastLetter . '">' );
Related
Over the past few days, I've been thinking about how to deal with iterating over keys in a multidimensional array, and I just cannot figure it out. The problem is, I don't know how deep the array might be - I want my code to be able to handle arrays of any depth.
The array itself comes from Advanced Custom Fields, but that's not too important. I need to iterate over the array, run a function on every array key which starts with field_ (to convert it from field_* to its proper name like post_title or something), and reconstruct the array with the same structure and values (although the order is not important). The array looks like this:
array (size=12)
'field_5b23d04fef8a6' => string '' (length=0)
'field_5b23d04fefa99' => string '' (length=0)
'field_5b23d04fefe85' => string '' (length=0)
'field_5b23d04ff0077' => string '' (length=0)
'field_5b23d04ff026c' => string '' (length=0)
'field_5b23d0bdb3c1a' => string 'Version 1' (length=9)
'field_5b23d0f48538b' => string '' (length=0)
'field_5b23d0f485772' => string '' (length=0)
'field_5b23d0d52be2d' => string '' (length=0)
'field_5b5ed10a6a7bc' => string '' (length=0)
'field_5b5ed10a6bcf5' =>
array (size=1)
0 =>
array (size=1)
'field_5b5ed10acd264' =>
array (size=1)
0 =>
array (size=6)
'field_5b5ed10b0c9ca' => string '0' (length=1)
'field_5b5ed10b0cce2' => string 'TEST1234' (length=8)
'field_5b5ed10b0d0fd' => string 'Download title' (length=14)
'field_5b5ed10b0d4e2' => string 'EN' (length=2)
'field_5b5ed10b0d72e' => string 'A00' (length=3)
'field_5b5ed10b0df27' => string '887' (length=3)
'field_5b23d088500a4' => string '' (length=0)
What would be the best way to handle this? I've looked at recursive functions and ResursiveArrayIterator already, but none of the examples I found were close enough to let me figure out what I need.
You can recursively call the same function if it finds a nested array like this:
$input = array(
'field_5b23d04fef8a6' => '',
'field_5b23d04fefa99' => '',
'field_5b23d04fefe85' => '',
'field_5b23d04ff0077' => '',
'field_5b23d04ff026c' => '',
'field_5b23d0bdb3c1a' => 'Version 1',
'field_5b23d0f48538b' => '',
'field_5b23d0f485772' => '',
'field_5b23d0d52be2d' => '',
'field_5b5ed10a6a7bc' => '',
'field_5b5ed10a6bcf5' => array(
array(
'field_5b5ed10acd264' => array(
array(
'field_5b5ed10b0c9ca' => '0',
'field_5b5ed10b0cce2' => 'TEST1234',
'field_5b5ed10b0d0fd' => 'Download title',
'field_5b5ed10b0d4e2' => 'EN',
'field_5b5ed10b0d72e' => 'A00',
'field_5b5ed10b0df27' => '887',
),
),
),
),
'field_5b23d088500a4' => '',
);
// recursively re-key array
function dostuff($input){
// always refer to self, even if you rename the function
$thisfunction = __function__;
$output = array();
foreach($input as $key => $value){
// change key
$newkey = (is_string($key) ? preg_replace('/^field_/', 'post_title_', $key) : $key);
// iterate on arrays
if(is_array($value)){
$value = $thisfunction($value);
}
$output[$newkey] = $value;
}
return $output;
}
var_dump(dostuff($input));
So I was looking at this and to my knowledge there is no wrapper function for recursion with callbacks, so here it is:
// general function for recursively doing something
// $input -> array() / the array you wan to process
// $valuefunction -> callable | null / function to run on all values *
// $keyfunction -> callable | null / function to run on all keys *
// * at least one has to defined or there is nothing to do
// callable has two inputs
// $input -> current branch
// $depth -> (int) how deep in the structure are we
// i.e: recursion($some_array, function($branch, $depth){something..}, 'trim');
function recursion($input, $valuefunction = false, $keyfunction = false){
if(!is_array($input)){
trigger_error('Input is '.gettype($input).'. Array expected', E_USER_ERROR);
return null;
}
if(!is_callable($valuefunction)){$valuefunction = false;}
if(!is_callable($keyfunction)){$keyfunction = false;}
if(!$valuefunction && !$keyfunction){
trigger_error('Input is unchanged!', E_USER_WARNING);
return $input;
}
// use recursion internally, so I can pass stuff by reference
// and do the above checks only once.
$recurse = function(&$branch, $depth = 0) use (&$recurse, &$valuefunction, &$keyfunction){
$output = array();
foreach($branch as $key => $value){
$key = $keyfunction ? $keyfunction($key, $depth) : $key;
$output[$key] = (is_array($value) ?
$recurse($value, $depth + 1) :
($valuefunction ?
$valuefunction($value, $depth) :
$value
)
);
}
return $output;
};
return $recurse($input);
}
$valuefunction = function($value, $depth){
return is_string($value) ? $depth.'_'.$value : $value;
};
function keyfunction($key){
return is_string($key) ? preg_replace('/^field_/', 'post_title_', $key) : $key;
}
var_dump(recursion($input, $valuefunction, 'keyfunction'));
Or for your example:
var_dump(recursion($input, 0, function($key){
return is_string($key) ? preg_replace('/^field_/', 'post_title_', $key) : $key;
}));
You could do something like this:
$arr = [
'a',
'b',
'c',
[
'd',
'e',
'f',
[
'g',
'h',
'i',
],
],
];
class MyIterator
{
public function iterate( $array )
{
foreach ( $array as $a ) {
if ( is_array( $a ) ) {
$this->iterate($a);
} else {
echo $a;
}
}
}
}
$iterator = new MyIterator();
$iterator->iterate($arr);
It prints this:
abcdefghi
You can iterate over array recursively like this
function recursiveWalk($array, callable $x)
{
$result = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$result[$key] = recursiveWalk($value, $x);
} else {
$result[$key] = $x($value);
}
}
return $result;
}
Here example:
$array = [
"aaa" => 1,
"sub1" => [
"xxx" => 2,
"sub2" => [
"yyy" => 3,
"ttt" => 4
]
]
];
print_r(recursiveWalk($array, function ($x) {
return $x + 1;
}));
Array
(
[aaa] => 2
[sub1] => Array
(
[xxx] => 3
[sub2] => Array
(
[yyy] => 4
[ttt] => 5
)
)
)
I'm trying to run through an object. I'm having an issue with my initial for each only running through the first iteration. So everything is great, but I'm only getting one iteration while their are 3. Any ideas why this may be happening?
<?php
global $wpdb;
/*Begin*/
$itemsTable = $wpdb->prefix . "HFW_portfolio_items";
$catTable = $wpdb->prefix . "HFW_portfolio_categories";
$tagTable = $wpdb->prefix . "HFW_portfolio_tags";
$rowsItemA = $wpdb->get_results("SELECT * FROM"." $itemsTable"."", ARRAY_A);
exit(var_dump($rowsItemA));
$numItems = count($rowsItem);
$i = 0;
//exit(var_dump($rowsItem));
foreach ($rowsItemA as $rowsItem ){
//$id = $rowsItem[id];
$port_item = "";
$id = stripslashes ($rowsItem[id]);
//exit(var_dump($id));
$portfolio_category = stripslashes ($rowsItem[portfolio_category]);
$sql = "SELECT * FROM"." $catTable"." WHERE id="."$portfolio_category"." LIMIT 1";
$catNameDB = $wpdb->get_results($sql);
/*from Above Select for CATEGORY*/
foreach ($catNameDB as $catNameDBs ){
$portfolio_category = stripslashes ($catNameDBs->cat_name);
}/**/
$portfolio_name = stripslashes ($rowsItem[portfolio_name]);
$portfolio_active = stripslashes ($rowsItem[portfolio_active]);
$portfolio_tags = stripslashes ($rowsItem[portfolio_tags]);
$portfolio_main_image = stripslashes ($rowsItem[main_image]);
$portfolio_desc = stripslashes ($rowsItem[portfolio_desc]);
$image_2 = stripslashes ($rowsItem[image_2]);
$image_3 = stripslashes ($rowsItem[image_3]);
$portfolio_website = stripslashes ($rowsItem[portfolio_website]);
//exit(var_dump($image_2,$image_3));
if($image_2 !== '' || $image_2 = null)
{
$image_2 = ",'".$image_2."',";
}
else
{
$image_2 = '';
}
if($image_3 !== '' || $image_3 = null)
{
$image_3 = ",'".$image_3."'";
}
else
{
$image_3 = '';
}
$port_item .= "
{
'title' : '".$portfolio_name."',
'description' : '".$portfolio_desc."',
'thumbnail' : ['".$portfolio_main_image."' ".$image_2." ".$image_3."],
'large' : ['".$portfolio_main_image."' ".$image_2." ".$image_3."],
'tags' : ['".$portfolio_category."']
}
";
if(++$i === $numItems) {
$port_item .= "";
}
else
$port_item .=",";
//exit(var_dump($i,$numItems));
}
?>
exit(var_dump($rowsItemA));
array (size=3)
0 =>
array (size=10)
'id' => string '9' (length=1)
'portfolio_name' => string 'Da' (length=26)
'main_image' => string 'elicate-dashley-1.png' (length=101)
'image_2' => string 'n-and-mn.png' (length=107)
'image_3' => string '' (length=0)
'portfolio_active' => string '0' (length=1)
'portfolio_category' => string '1' (length=1)
'portfolio_desc' => string 'test1' (length=246)
'portfolio_tags' => string '["1","2","3","4","5","6","10"]' (length=30)
'portfolio_website' => string 'http://www.test.com/' (length=26)
1 =>
array (size=10)
'id' => string '10' (length=2)
'portfolio_name' => string 'Sage' (length=29)
'main_image' => string 'purs-er.png' (length=99)
'image_2' => string '' (length=0)
'image_3' => string '' (length=0)
'portfolio_active' => string '0' (length=1)
'portfolio_category' => string '1' (length=1)
'portfolio_desc' => string 'test 2' (length=249)
'portfolio_tags' => string '["1","2","5","6","9","10"]' (length=26)
'portfolio_website' => string 'http://www.test.com/test' (length=27)
2 =>
array (size=10)
'id' => string '11' (length=2)
'portfolio_name' => string 'Scap' (length=20)
'main_image' => string 's-day-cap.png' (length=93)
'image_2' => string '' (length=0)
'image_3' => string '' (length=0)
'portfolio_active' => string '0' (length=1)
'portfolio_category' => string '1' (length=1)
'portfolio_desc' => string 'test 3' (length=155)
'portfolio_tags' => string '["1","2","5","6","9","10"]' (length=26)
'portfolio_website' => string 'http://www.test.com/test/test' (length=44)
This is because, you have made $port_item = "" in brginning of the loop.
foreach ($rowsItemA as $rowsItem ){
$port_item = "";// declaring as null here(remove this.)
So in every loop your value is reinitialized, and you are getting only 1 element(last element of your array)
How combine this array and remove duplicate values?
Thank you!
array (size=17)
0 => string 'Black' (length=5)
1 => string 'Blue' (length=4)
2 => string 'Burgundy' (length=8)
3 => string 'Glow' (length=4)
4 => string 'Granite' (length=7)
5 => string 'Green' (length=5)
6 => string 'Lime' (length=4)
7 => string 'Natural' (length=7)
8 => string 'Orange' (length=6)
9 => string 'Pink' (length=4)
10 => string 'Purple' (length=6)
11 => string 'Red' (length=3)
12 => string 'Silver' (length=6)
13 => string 'Teal' (length=4)
14 => string 'Violet' (length=6)
15 => string 'White' (length=5)
16 => string 'Yellow' (length=6)
Thank you, your code worked perfectly.
I'm still missing something because I don't get what I want.
The values (color names) are in the select form, but when a value other than 'Filter by Color' is selected, the 'id' is what pass instead of the 'text'. To make things easy, here is the complete code.
Thank you.
$Sql_product_colors = ("SELECT COUNT(p.products_id) AS id, pia.product_filter_colors FROM products p LEFT JOIN products_imprint_areas pia on p.products_id = pia.products_id
WHERE p.products_id > '1'
AND pia.product_filter_colors IS NOT NULL
GROUP BY product_filter_colors
ORDER BY product_filter_colors asc");
$sort_list_colors = array();
$product_filter_colors = tep_db_query($Sql_product_colors) or die(mysql_error());
while ($row = mysql_fetch_array($product_filter_colors)) {
$arrColor = explode(',', $row[1]);
$arrColors=array_filter(array_map('trim', $arrColor));
$sort_list_color = array_merge($sort_list_colors,$arrColors);
$sort_list_colors = array_unique($sort_list_color);
sort($sort_list_colors);
}
$sort_list_colors[0] ='Filter by Color';
$sort_list_colors[] =' '.$row[$sort_list_colors].' (' .$sort_list_colors['count'].')';
if(count($sort_list_colors)>0)
{
foreach($sort_list_colors as $id=>$text) {
$sort_range_colors[] = array('id' => $id, 'text' => $text);
}
}
// -------------------------------------- Select form
echo '<td align="center" width="25%" valign="middle"><div class="styled-select">' . tep_draw_form('colors_sort', htmlentities($_SERVER['PHP_SELF']), 'get') . '';
echo tep_draw_pull_down_menu('colors_sort', $sort_range_colors, (isset($HTTP_GET_VARS['colors_sort']) ? $HTTP_GET_VARS['colors_sort'] : ''), 'onchange="this.form.submit()"');
echo '</form></div></td>';
This is the function tep_draw_pull_down_menu. Thank you.
function tep_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
$field = '<select name="' . tep_output_string($name) . '"';
if (tep_not_null($parameters)) $field .= ' ' . $parameters;
$field .= '>';
if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
for ($i=0, $n=sizeof($values); $i<$n; $i++) {
$field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
if ($default == $values[$i]['id']) {
$field .= ' SELECTED';
}
$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</option>';
}
$field .= '</select>';
if ($required == true) $field .= TEXT_FIELD_REQUIRED;
return $field;
}
Use array_merge then array_unique,
$colors = array();
while ($row = mysql_fetch_array($filter_colors)) {
$arrColors = explode(',', $row[1]);
$colors = array_merge($colors,$arrColors);
}
$colors = array_unique($colors);
Warning: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Thsi is my array structure.
$arr = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
I want to create an array like this.
array
0 =>
array
'Tablet' => string 'test1' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode1' (length=9)
'Since' => string 'mng' (length=3)
1 =>
array
'Tablet' => string 'test2' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode2' (length=9)
'Since' => string 'mng' (length=3)
Can someone pls help?
Sorry for bad english.
Thanks
$new = [];
foreach ($arr as $key => $sub) {
$i = 0;
foreach ($sub as $value) {
$new[$i][$key] = $value;
$i++;
}
}
That should do the trick :)
$newArr = array();
foreach ( $arr as $key => $subArr ){
for ($i= 0; $i < count($subArr); $i++){
if (!array_key_exists($i, $newArr)){
$newArr[$i] = array();
}
$newArr[$i][$key] = $subArr[$i];
}
}
$arr = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1','abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$na=array();
foreach($arr as $k=>$v){
foreach($v as $kk=>$vv){
$na[$kk][$k]=$vv;
}
}
echo "<pre>";print_r($na);
Easy you can append each one to normal array like this
$arr1 = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$arr2 = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$arr3 = array(
'Tablet'=>array('test1','test2'),
'Medicine'=>array('abc1'.'abc2'),
'Dosage'=>array('1','1'),
'Mode'=>array('testmode1','testmodel2'),
'Since'=>array('mng','mng')
);
$a=array();
array_push($a,arr1,arr2,arr3);
print_r($a);
output will look
array
0 =>
array
'Tablet' => string 'test1' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode1' (length=9)
'Since' => string 'mng' (length=3)
1 =>
array
'Tablet' => string 'test2' (length=5)
'Medicine' => string 'abc1' (length=4)
'Dosage' => string '1' (length=1)
'Mode' => string 'testmode2' (length=9)
'Since' => string 'mng' (length=3)
$myLength = count($arr['Tablet'])
$newArray = array();
for (i=0; i < $myLength ; i++)
{
$newArray[i]['Tablet'] = $arr['Tablet'][i];
$newArray[i]['Medicine'] = $arr['Medicine'][i];
$newArray[i]['Dosage'] = $arr['Dosage'][i];
$newArray[i]['Mode'] = $arr['Mode'][i];
$newArray[i]['Since'] = $arr['Since'][i];
}
What do you meant adding the (lentgh = x) in your question?
How do I display the following using the array shown below?
Pastors
key=>0, member_id, member_name
Deacons
key=>1, member_id, member_name
key=>2, member_id, member_name
Here is the array.
array (size=3)
0 =>
array (size=4)
'category_name' => string 'Pastors' (length=7)
'member_id' => string '3' (length=1)
'member_name' => string 'Tiny Marshall' (length=13)
'member_email' => string 'jconley#nowhere.com' (length=19)
1 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '1' (length=1)
'member_name' => string 'Jeremiah Conley' (length=15)
'member_email' => string 'jconley#nowhere.com' (length=19)
2 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '2' (length=1)
'member_name' => string 'Marcy Conley' (length=12)
'member_email' => string 'jconley#nowhere.com' (length=19)
Here is the code that I used to build the array:
while( $row = mysql_fetch_assoc( $result ) )
{
$staff[$i] = array
(
'category_name' => $row['category_name'],
'member_id' => $row['member_id'],
'member_name' => $row['member_name'],
'member_email' => $row['member_email'],
);
$i++;
}
This is the final solution:
$category_name = array();
foreach ($staff as $member) {
if (!in_array( $member['category_name'], $category_name ))
{
echo '<h1>' . $member['category_name'] . '</h1>';
$category_name[] = $member['category_name'];
}
echo $member['member_id'] . ', ' . $member['member_name'] . '<br />';
}
Use foreach to loop over your $staff array.
$pastors = array();
$deacons = array();
foreach ($staff as $member) {
if ($member['category_name'] == 'Pastors') {
$pastors[] = $member['member_id'] . ', ' . $member['member_name'];
} else if ($member['category_name'] == 'Deacons') {
$deacons[] = $member['member_id'] . ', ' . $member['member_name'];
}
}
documentation