Jump to content

Filtering out values in an array.


php_joe

Recommended Posts

I have a list of arrays like this:

 

<?
$msg[2008][105][7][45][18][0] = array("7:45:18", "Admin", "public", "Hello.");
$msg[2008][105][7][45][19][0] = array("7:45:19", "Admin", "public", "This is a test.");
$msg[2008][105][7][45][20][0] = array("7:45:20", "Admin", "public", "This is only a test.");
$msg[2008][105][7][45][21][0] = array("7:45:21", "Admin", "public", "If this were a real chat.");
$msg[2008][105][7][45][22][0] = array("7:45:22", "Admin", "public", "I wouldn't be talking to myself.");
?>

 

I want to process only the arrays that follow a specified key, such as $msg[2008][105][7][45][20][0].

 

I wrote this code, but it processes every array, not just the ones following $msg[2008][105][7][45][20][0].

 

<?
$last = "2008.105.7.45.20.0"; // <== the keys are year, day, hour, minute, second, and microsecond
$last2 = explode('.', $last);

if($msg){

if($msg) foreach($msg as $key_year => $value1){
if($key_year >= $last2[0]) foreach($msg[$key_year] as $key_day => $value2){
if($key_day >= $last2[1]) foreach($msg[$key_year][$key_day] as $key_hour => $value3){
if($key_hour >= $last2[2]) foreach($msg[$key_year][$key_day][$key_hour] as $key_min => $value4){
if($key_min >= $last2[3]) foreach($msg[$key_year][$key_day][$key_hour][$key_min] as $key_sec => $value5){
foreach($msg[$key_year][$key_day][$key_hour][$key_min][$key_sec] as $key_micro => $piece){

if($key_sec > $last2[4]) $text .= display_text($piece);
if($key_sec = $last2[4] && $key_micro >= $last2[5]) $text .= display_text($piece);

}
}
}
}
}
}

}
?>

 

What have I done wrong?

Link to comment
https://forums.phpfreaks.com/topic/101232-filtering-out-values-in-an-array/
Share on other sites

Why not just store as

<?php
$msg['2008.105.7.45.18.0'] = array("7:45:18", "Admin", "public", "Hello.");
$msg['2008.105.7.45.19.0'] = array("7:45:19", "Admin", "public", "This is a test.");
$msg['2008.105.7.45.20.0'] = array("7:45:20", "Admin", "public", "This is only a test.");
$msg['2008.105.7.45.21.0'] = array("7:45:21", "Admin", "public", "If this were a real chat.");
$msg['2008.105.7.45.22.0'] = array("7:45:22", "Admin", "public", "I wouldn't be talking to myself.");
?>

Why not just store as

<?php
$msg['2008.105.7.45.18.0'] = array("7:45:18", "Admin", "public", "Hello.");
$msg['2008.105.7.45.19.0'] = array("7:45:19", "Admin", "public", "This is a test.");
$msg['2008.105.7.45.20.0'] = array("7:45:20", "Admin", "public", "This is only a test.");
$msg['2008.105.7.45.21.0'] = array("7:45:21", "Admin", "public", "If this were a real chat.");
$msg['2008.105.7.45.22.0'] = array("7:45:22", "Admin", "public", "I wouldn't be talking to myself.");
?>

 

I thought that keys couldn't be longer than 10 digits. I seem to remember trying a single key at first, but getting errors.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.