First, let met say that instead of echo
, you should use var_dump
or print_r
for debugging. At first view, I see you are building a sequential array $current_history_array
but you are trying to use it as associative array.
$current_history_array[] = array ( current_time( "timestamp" ), $lastIP, $field, $value );
The above code generates a sequential array, for example:
array(
'1424680884',
'77.77.777.777',
$field,
$value
);
After, you are trying to access to lastIP
key using $array['lastIP']
but such key doesn’t exists. Instead, the last IP is stored in the index 2: $array[2]
. If you want ot use associative arrays you have to build it as follow:
$current_history_array[] = array (
//The structure is key => value
'time' => current_time( "timestamp" ),
'lastIP' => $lastIP,
'field' => $field,
'value' => $value
);
The above code generates an array like this:
array(
//The structure is key => value
'timestamp' => '1424680884',
'lastIP' => '77.77.777.777',
'field' => $field,
'value' => $value
);
Now you can access to 'lastIP'
key as $array['lastIP']
.
Also, note that you store serialized data, so you have to unserialize it before use it as array:
$hasIP = false;
$histMeta = get_post_meta($post->ID, 'history', false);
foreach($histMeta as $array) {
$array = unserialize( $array );
if(isset($array['lastIP'])) {
$hasIP = $array['lastIP'];
break;
}
}
echo $hasIP;