array

Detect and mark all duplicates in the same loop

I had a loop where an array with possible duplicates of 'code'. Did not want to loop the array twice and didn't want to extend the array with yet another item for each row. So I made this script keep track of the first occurrence of each 'code' and any duplicate 'code', and if a duplicate is found the script marks the first occurrence as number 1 and then mark up any additional duplicate with 2, 3, 4and so on.

PHP json_decode from POST variable produced by JavaScript

I had issues with perfectly looking JSON string received from a POST. Not in any way using Chrome devtools (F12) nor saving the source from the browser and using Notepad I could detect that the double quotes actually was not real quotes until I investigated the the ASCII value using ord() and substr() and it returned 38 as in "&", then I understood it was a HTML entity.

Converting the JSON with html_entity_decode() made the trick.

Forward unique combinations in multiple array

Create combinations in existing series with next coming series in a multiple array. For the first row in the array all values will be matched against all values in the next coming row, to be able to do some calculation or create a unique combination of values.

<?php
$cols
[] = array(1,2,3);
$cols[] = array(4,5,6);
$cols[] = array(7,8,9);

foreach (
$cols as $key => $col) {
    foreach (
$col as $key1 => $value1) {
        foreach (
$col[$key+1] as $key2 => $value2) {
                    echo
"<br>$value1 => $value2";
        }
    }
}
?>

will give:

1 => 4

1 => 5

1 => 6

How to split a text into chuncks containing a maximum number of characters and retain full sentences

This is how you can split a text into chuncks containing a maximum number of characters and retain full sentences.As an example, if you have a limit of 5000 characters, as in the case of Google Translate API, and would like to divide the text into chunks, but you do not want sentences or words to be divided in the middle.

PHP array first or last of key or value

Here's how to get the first key, the last key, the first value or the last value of a (hash) array without explicitly copying nor altering the original array:


<?php
  $array
= array('first'=>'111', 'second'=>'222', 'third'=>'333');

 
// get the first key: returns 'first'
 
print array_shift(array_keys($array));

 
// get the last key: returns 'third'
 
print array_pop(array_keys($array));

 
// get the first value: returns '111'
 
print array_shift(array_values($array));

 
// get the last value: returns '333'
 
print array_pop(array_values($array));
?>

Sort a multidimensional array based on given key value

How to sort a multidimensional array based on a given key value.


/**
* Sort an array based on a given field key
*
* @param unknown_type $named_recs
* @param unknown_type $order_by
* @param unknown_type $rev
* @param unknown_type $flags
* @return unknown
*/
function _named_records_sort($named_recs, $order_by, $reverse=false, $flags=0) {
// Create 1-dimensional named array with just sortfield (in stead of record) values
$named_hash = array();
foreach($named_recs as $key=>$fields)
$named_hash["$key"] = $fields[$order_by];

// Or

Pages