Reputation: 8405
Hey guys I'm trying to see where my pairs of keys stop, I have arrays built like this
EDIT People are getting really confused so I'm using a real array instead of an example
array (
'key' => '',
'po' => '',
'label' => '',
'report_key' => '',
'shipper' => '',
'status' => '',
'location' => '',
'inspector' => '',
'commodity' => '',
'brand' => '',
'case_count' => '',
'variety' => '',
'style' => '',
'grower_lot' => '',
'pack_date' => '',
// grouping 4 items
'berry_size1' => '',
'berry_size2' => '',
'berry_size3' => '',
'berry_size4' => '',
// grouping 3 items
'bunch_color1' => '',
'bunch_color2' => '',
'bunch_color3' => '',
// grouping 2 items
'color1' => '',
'color2' => '',
// grouping 3 items
'stem1' => '',
'stem2' => '',
'stem3' => '',
// grouping 2 items
'shatter1' => '',
'shatter2' => '',
// grouping 2 items
'splits1' => '',
'splits2' => '',
// grouping 2 items
'wet_sticky1' => '',
'wet_sticky2' => '',
'overall_quality' => '',
// grouping 2 items
'sugar_brix1' => '',
'sugar_brix2' => '',
'rating' => '',
'comments' => '',
)
I came up with some stupid way that really doesn't work to try and sort things out, its extremely backwards, honestly I'm pretty embarrassed by my attempt.
foreach($obj as $key=>$val) {
if(strpos( preg_replace('/[^a-z]/i', '', $key),
preg_replace('/[^a-z]/i', '', $all_keys[$key+$b+1])
) !== false) { echo "<p>$key</p>"; // items 1-3 will show
} elseif(strpos(preg_replace('/[^a-z]/i', '', $key),
preg_replace('/[^a-z]/i', '', $all_keys[$key+$b-1])
) !== false) { echo "<p>$key</p>"; // show last item
} else {
$in.='<aside class="left">';
$in .= "<label for='$key'>". ucwords(strtolower(str_replace('_',' ',$key))) ."</label><br/>";
$in .= ($key=='key') ? "<input type='text' value='". $objLastId ."' id='$key' class='disabled' disabled='disabled'>" : "<input type='text' value='' name='$key' id='$key'>";
$in.='</aside>';
$b++;
}
}
Anyway what I'm really trying to achieve is something like this, could someone steer me in the right direction please?
<style>
.row2 input {width: 50px !important;}
.row3 input {width: 27px !important;}
.row4 input {width: 15px !important;}
</style>
// stem was a 2 item group, so should have the row4 class
// and should have the second item appended by a
// all be inside the same grouping, like below ...
<aside class="left row2">
<label for="color1">Color</label>
<br/><input type="text" value="" name="color1" id="color1">
<input type="text" value="" name="color2" id="color2">
</aside>
// stem was a 3 item group, so should have the row4 class
// and should have items 2-3 appended by a all be inside
// the same grouping, like below ...
<aside class="left row3">
<label for="stem1">Stem</label>
<br><input type="text" id="stem1" name="stem1" value="">
<input type="text" id="stem2" name="stem2" value="">
<input type="text" id="stem3" name="stem3" value="">
</aside>
// berry_size was a 4 item group, so should have the row4 class
// and should have items 2-4 appended by a all be inside
// the same grouping, like below ...
<aside class="left row4">
<label for="berry_size1">Berry Size</label>
<br/><input type="text" id="berry_size1" name="berry_size1" value="">
<input type="text" id="berry_size2" name="berry_size2" value="">
<input type="text" id="berry_size3" name="berry_size3" value="">
<input type="text" id="berry_size4" name="berry_size4" value="">
</aside>
... or ...
// this is a single, so no extra class and ....
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" id="other_item" name="other_item" value="">
</aside>
What I see this really boiling down to is reading the next array keys name (I stripped the name and used the integer in my version), atleast I think that's the right way to do it?
Upvotes: 0
Views: 76
Reputation: 16214
$arr = array(
'other_item' => 'value',
// this one ranges 1-3
'first_name1' => 'value',
'first_name2' => 'value',
'first_name3' => 'value',
// this one ranges 1-4
'next_name1' => 'value',
'next_name2' => 'value',
'next_name3' => 'value',
'next_name4' => 'value',
'other_item' => 'value',
// this one ranges 1-4
'last_name1' => 'value',
'last_name2' => 'value',
'last_name3' => 'value',
'last_name4' => 'value',
'other_item' => 'value'
);
$newarr = array();
foreach($arr as $key=>$value)
{
if (preg_match('#^([^\d]+)#', $key, $matches)===1)
$newarr[$matches[1]][] = $value;
}
print_r($newarr);
Output:
Array
(
[other_item] => Array
(
[0] => value
)
[first_name] => Array
(
[0] => value
[1] => value
[2] => value
)
[next_name] => Array
(
[0] => value
[1] => value
[2] => value
[3] => value
)
[last_name] => Array
(
[0] => value
[1] => value
[2] => value
[3] => value
)
)
And do whatever you want to do with it. Like that code (just an example, not a very nice one)
foreach($newarr as $name => $block)
{
$cnt = count($block);
echo '<aside class="left'.($cnt>1?' row' . $cnt:'').'">
<label for="' . $name . ($cnt>1?'1':''). '">' .
ucwords(str_replace('_', ' ', $name)) . '</label>
<br/>';
foreach($block as $key=>$element)
{
echo ($key>0?' ':'') . '<input type="text" value="" name="' . $name .
($cnt>1?($key+1):'') . '" id="' . $name .
($cnt>1?($key+1):'') . '">' . "\n";
}
echo '</aside>' . "\n";
}
It gives:
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" value="" name="other_item" id="other_item">
</aside>
<aside class="left row3">
<label for="first_name1">First Name</label>
<br/><input type="text" value="" name="first_name1" id="first_name1">
<input type="text" value="" name="first_name2" id="first_name2">
<input type="text" value="" name="first_name3" id="first_name3">
</aside>
<aside class="left row4">
<label for="next_name1">Next Name</label>
<br/><input type="text" value="" name="next_name1" id="next_name1">
<input type="text" value="" name="next_name2" id="next_name2">
<input type="text" value="" name="next_name3" id="next_name3">
<input type="text" value="" name="next_name4" id="next_name4">
</aside>
<aside class="left row4">
<label for="last_name1">Last Name</label>
<br/><input type="text" value="" name="last_name1" id="last_name1">
<input type="text" value="" name="last_name2" id="last_name2">
<input type="text" value="" name="last_name3" id="last_name3">
<input type="text" value="" name="last_name4" id="last_name4">
</aside>
Upvotes: 2
Reputation: 20469
As far as I can tell you just want to modify your output based on your 'key type'. I am thinking something like this:
foreach($obj as $key => $val)
{
$parts = explode('_', $key);
switch($parts[0])
{
case 'first':
// Do something here
break;
case 'other':
// Do something here
break;
case 'next':
// Do something here
break;
case 'last':
// Do something here
break;
}
}
I think I might be missing something however, as you appear to be checking 'next' elements...
Upvotes: 0