スキップしてメイン コンテンツに移動

投稿

ラベル(array)が付いた投稿を表示しています

Json形式の文字列をPHP形式の配列表示に変換するプログラム

json形式の文字列をPHPの配列形式で、そのまま貼り付けて利用するためのちょっとしたコードです。 とりあえず開発中に楽をするためのプログラムなので、厳密さよりも簡便さ重視で書きました。 json形式の文字列をPHPの配列に変換 PHPの配列をJSON_PRETTY_PRINTで整形してjson形式の文字列に再変換 jsonの括弧やコロンをPHPの配列の形式に合うように変換 <?php echo strtr(json_encode(json_decode($jsonString), JSON_PRETTY_PRINT), [':' => '=>', '{' => '[', '}' => ']']);

PHPで与えられた配列の次元数を取得する方法

PHPの多次元配列で次元数を推定する関数の紹介です。 [注意] 配列の最初の要素だけをチェックしていくので、すべての配列の次元が同じであること前提としています。 そもそも配列の次元数がバラバラ(C#でいうjagged array)であれば、今回紹介する関数は使えません。 <?php function getDimension(array $source) { if(is_array($source)) { return getDimension(reset($source)) + 1; } else { return 0; } } // 下記のような配列を引数で与えます。 $source[1]['A']['a'] = true; $source[2]['B']['a'] = true; $source[3]['A']['a'] = true; $source[4]['B']['a'] = true; // 結果は3になります。 getDimension($source);

PHPの配列(array)のメモリ使用量の考察

はじめに 最近PHP上に大量のデータをメモリ上に展開していたのですが、配列(array)の形式(連想配列 or 単純な配列)や配列の要素のデータ構造(数字、配列、文字列など)で大きくメモリ使用量に差が出てくることに気づき、簡単なプログラムを組んで調べてみました。 あくまで筆者の環境での結果なので、細かい数値は参考程度に見てください。 測定環境と方法 OS: Windows 10 PHP 7.4.5 (php-7.4.5-nts-Win32-vc15-x64) 配列に要素を追加するプログラムを書いて、PHPのmemory_get_usage(true)関数を使って実メモリ使用量を計測しました。 計測結果 No. 方式 1MB当たり作成できる 要素数 プログラム 補足 1 キーも値も整数の配列 (整数IDを想定) 28571 // 2,000,000 / 70MB $row = []; for($i = 0; $i < 2000000; $i++) { $row[] = $i; } No.2~6でテストしたプログラム中の要素数は200,000。これだけ一桁多い! 2 キーが文字列、値が整数の連想配列 8333 // 200,000 / 24MB $row = []; for($i = 0; $i < 200000; $i++) { $row[$i.'_key_string'] = $i; } キーの文字列が長い方がメモリ使用量多くなる。 3 キーが整数、値が連想配列の配列 DBから取得してきたデータを想定 2325 // 200,000 / 86MB $row = []; for($i = 0; $i < 200000; $i++) { row[] = ['id' => $i]; } 4 キーが整数、値が連想配列の配列(配列に複数の値を保持) DBから取得してきたデータを想定 2127 // 200,000 /

Java: Get Sorted Array Index

As you know, if you would like to sort array, you should use Arrays.sort method. But if you would like to get sorted index, you should use your a brain a bit. In this post, I will show you the code snippet for getting sorted array index. The following is the simplest way to get sorted index. The disadvantages of the following code are autoboxing in compare process and returned array type is not int[] but Integer[]. /** * The most common & general way. But this method use autoboxing and need Integer[] not int[]. */ public static final <T>void sort(T[] array, Comparator<T> comparator, Integer[] sorted) { Arrays.sort(sorted, (i1, i2) -> comparator.compare(array[i1], array[i2])); } To solve these advantage, I wrote following array sort helper class which can get sorted array index as int[] type. You can call getSortedIndex method with source array and comparator as the arguments. The code uses lambda expression which was introduced in Java 8. packa

Java: Shuffling Elements in Array

I will show you java code for shuffling order of elements in array randomly. Only one thing you should notice is generating random is quite sensitive problem in a precise sense. I use java Random class which is provided by JDK. However if you need to use more proper randomness, you may implement class for generate random number sequence yourself. Anyway, the code is below. package com.dukesoftware.utils.math; import java.util.Random; public class Shuffler { private final Random random; public Shuffler(Random random) { this.random = random; } public void shuffle(int[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i int swap = a[i]; a[i] = a[j]; a[j] = swap; } } public void shuffle(double[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i double swap =

PHP: Extract Values on Specific Key in Array

I wrote tiny utility method - which is for extracting values on specific key in array. Code function extractValuesForSpecificKey(array& $sourceArray, $key){ $retArray = array(); foreach ($sourceArray as $sourceElem){ $retArray[] = $sourceElem[$key]; } return $retArray; } Example $source = array( array('id' => 1, 'name' =>'Joe'), array('id' => 2, 'name' =>'Chris'), ); $result = extractValuesForSpecificKey($source, 'id'); var_dump($result); The result should be: array(2) { [0]=> int(1) [1]=> int(2) }

PHP: Grouping Elements in Array by Specific Key Field

This is the code for grouping elements in array by specific key field of the element. Code <?php function groupBySpecificKey(array& $source, $key){ $map = array(); foreach($source as $elem){ $groupKey = $elem[$key]; if(is_null($groupKey)) continue; $map[$groupKey][] = $elem; } return $map; } Example $result = groupBySpecificKey($source, 'country'); var_dump($result); $source = array( array('id' => 1, 'name' =>'Joe', 'country' => 'China'), array('id' => 2, 'name' =>'Chris', 'country' => 'USA'), array('id' => 3, 'name' =>'Tod', 'country' => 'USA'), ); The result is below. array(2) { ["China"]=> array(1) { [0]=> array(3) { ["id"]=> int(1) ["name"]=> string(3) "Joe"

Ruby, Python, JavaScript, Perl, C++ 比較

Ruby, Python, JavaScript, Perl, C++間の比較 bkブログ さんによくまとまっています。 配列操作の比較表 文字列操作の比較表 JavaとC#のGenericsの比較 C#とJavaのGenericsに関する比較は まじかんと さんの ジェネリック: Java vs C# によくまとまっています。ちなみにJava 5.0のGenericsに関する説明のpdfが ここ からダウンロードできます。