現在の言語: 日本語 |
連想配列(2) |
配列関連 |
class test1
{
function test1()
{
$userAll=[];
$user=[];
$user["name"]="yamada";
$user["age"]=20;
$userAll[]= $user;
$user=[];//追加するため配列初期化します
$user["name"]="tanaka";
$user["age"]=21;
$userAll[] = $user;
print_r($userAll);
/*
Array
(
[0] => Array
(
[name] => yamada
[age] => 20
)
[1] => Array
(
[name] => tanaka
[age] => 21
)
)
*/
//件数の取得
//countを使用したケース
echo "count:".count($userAll).PHP_EOL;//count:2
//sizeofを使用したケース
echo "sizeof:".sizeof($userAll).PHP_EOL;//sizeof:2
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
echo "</pre>";
class test1
{
function test1()
{
$userAll=[];
$user=[];
$user["name"]="yamada";
$user["age"]=20;
$userAll[]= $user;
$user=[];//Initialize the array for addition
$user["name"]="tanaka";
$user["age"]=21;
$userAll[] = $user;
print_r($userAll);
/*
Array
(
[0] => Array
(
[name] => yamada
[age] => 20
)
[1] => Array
(
[name] => tanaka
[age] => 21
)
)
*/
/Get the number of items
//Using count
echo "count:".count($userAll).PHP_EOL;//count:2
//Using sizeof
echo "sizeof:".sizeof($userAll).PHP_EOL;//sizeof:2
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
echo "</pre>";
class test2
{
function test1()
{
$user1=[
"name"=>"yamada",
"age"=>20
];
//参照渡し(&)によるデータの共有を舌ケース
$user2 = &$user1;
echo "user1配列".PHP_EOL;
print_r($user1);
echo "user2配列".PHP_EOL;
print_r($user2);
//[結果]
/*
user1配列
Array
(
[name] => yamada
[age] => 20
)
user2配列
Array
(
[name] => yamada
[age] => 20
)
*/
//ageキーの値を$user2のみ変更してみます
$user2["age"]=21;
//変更後の結果
echo "user1配列".PHP_EOL;
print_r($user1);
echo "user2配列".PHP_EOL;
print_r($user2);
//[結果]
/*
user1配列
Array
(
[name] => yamada
[age] => 21
)
user2配列
Array
(
[name] => yamada
[age] => 21
)
*/
}
function test2()
{
$user=[
"name"=>"yamada",
"age"=>20
];
$this->testArray1($user);
//Fatal error: Uncaught TypeError: test2::testArray1(): Argument #1 ($ary) must be of type array, null given, called
//$this->testArray1(null);
$this->testArray2($user);
/*
Array
(
[name] => yamada
[age] => 20
)
*/
$this->testArray2(null);//ary is null
echo $this->checkArray(null).PHP_EOL;
$user_age_none=[
"name"=>"yamada"
];
$user_name_none=[
"age"=>20
];
$this->checkArray($user_age_none).PHP_EOL;
$this->checkArray($user_name_none).PHP_EOL;
$this->checkArray($user).PHP_EOL;
}
//引数の型(array)
private function testArray1(array $ary)
{
print_r($ary);
}
//nullable宣言
private function testArray2(?array $ary)
{
if($ary === null)
{
echo "array is null".PHP_EOL;
return;
}
print_r($ary);
}
private function checkArray(?array $ary)
{
if($ary === null)
{
echo "array is null".PHP_EOL;
return;
}
if(empty($ary))
{
echo "array is empty.".PHP_EOL;
return;
}
if(!is_array($ary))
{
echo "it is not array.".PHP_EOL;
return;
}
//nameキーの存在チェック
if (!isset($ary['name']))
{
echo "'name' キーがありません。".PHP_EOL;
return;
}
// ageキーの存在チェック
if (!isset($ary['age']))
{
echo "'age' キーがありません。".PHP_EOL;
return;
}
//nameの文字列判定
if (!is_string($ary['name']))
{
echo "'name' は文字列ではありません。".PHP_EOL;
return;
}
//ageの整数判定
if (!is_int($ary['age']))
{
echo "'age' は整数ではありません。".PHP_EOL;
return;
}
echo "連想配列のチェックが終わりました。";
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->test1();
$cls2->test2();
echo "</pre>";
class test2
{
function test1()
{
$user1=[
"name"=>"yamada",
"age"=>20
];
// Sharing data by reference (&)
$user2 = &$user1;
echo "user1Array".PHP_EOL;
print_r($user1);
echo "user2Array".PHP_EOL;
print_r($user2);
// [Result]
/*
user1Array
Array
(
[name] => yamada
[age] => 20
)
user2Array
Array
(
[name] => yamada
[age] => 20
)
*/
// Change the value of the age key for $user2 only
$user2["age"]=21;
// Result after change
echo "user1Array".PHP_EOL;
print_r($user1);
echo "user2Array".PHP_EOL;
print_r($user2);
//[result]
/*
user1 array
Array
(
[name] => yamada
[age] => 21
)
user2 array
Array
(
[name] => yamada
[age] => 21
)
*/
}
function test2()
{
$user=[
"name"=>"yamada",
"age"=>20
];
$this->testArray1($user);
//Fatal error: Uncaught TypeError: test2::testArray1(): Argument #1 ($ary) must be of type array, null given, called
//$this->testArray1(null);
$this->testArray2($user);
/*
Array
(
[name] => yamada
[age] => 20
)
*/
$this->testArray2(null);//ary is null
echo $this->checkArray(null).PHP_EOL;
$user_age_none=[
"name"=>"yamada"
];
$user_name_none=[
"age"=>20
];
$this->checkArray($user_age_none).PHP_EOL;
$this->checkArray($user_name_none).PHP_EOL;
$this->checkArray($user).PHP_EOL;
}
//Argument type (array)
private function testArray1(array $ary)
{
print_r($ary);
}
//nullable declaration
private function testArray2(?array $ary)
{
if($ary === null)
{
echo "array is null".PHP_EOL;
return;
}
print_r($ary);
}
private function checkArray(?array $ary)
{
if($ary === null)
{ echo "array is null".PHP_EOL;
return;
}
if(empty($ary))
{
echo "array is empty.".PHP_EOL;
return;
}
if(!is_array($ary))
{
echo "it is not an array.".PHP_EOL;
return;
}
//Check for existence of name key
if (!isset($ary['name']))
{
echo "'name' key does not exist.".PHP_EOL;
return;
}
//Check for existence of age key
if (!isset($ary['age']))
{
echo "'age' key does not exist.".PHP_EOL;
return;
}
//Check for string value of name
if (!is_string($ary['name']))
{
echo "'name' is not a string.".PHP_EOL;
return;
}
//Check for integer value of age
if (!is_int($ary['age']))
{
echo "'age' is not an integer.".PHP_EOL;
return;
}
echo "Associative array check completed.";
}
}
echo "";
$cls2 = new test2();
$cls2->test1();
$cls2->test2();
echo "";class test2
{
function test1()
{
$user1=[
"name"=>"yamada",
"age"=>20
];
// Sharing data by reference (&)
$user2 = &$user1;
echo "user1Array".PHP_EOL;
print_r($user1);
echo "user2Array".PHP_EOL;
print_r($user2);
// [Result]
/*
user1Array
Array
(
[name] => yamada
[age] => 20
)
user2Array
Array
(
[name] => yamada
[age] => 20
)
*/
// Change the value of the age key for $user2 only
$user2["age"]=21;
// Result after change
echo "user1Array".PHP_EOL;
print_r($user1);
echo "user2Array".PHP_EOL;
print_r($user2);
//[result]
/*
user1 array
Array
(
[name] => yamada
[age] => 21
)
user2 array
Array
(
[name] => yamada
[age] => 21
)
*/
}
function test2()
{
$user=[
"name"=>"yamada",
"age"=>20
];
$this->testArray1($user);
//Fatal error: Uncaught TypeError: test2::testArray1(): Argument #1 ($ary) must be of type array, null given, called
//$this->testArray1(null);
$this->testArray2($user);
/*
Array
(
[name] => yamada
[age] => 20
)
*/
$this->testArray2(null);//ary is null
echo $this->checkArray(null).PHP_EOL;
$user_age_none=[
"name"=>"yamada"
];
$user_name_none=[
"age"=>20
];
$this->checkArray($user_age_none).PHP_EOL;
$this->checkArray($user_name_none).PHP_EOL;
$this->checkArray($user).PHP_EOL;
}
//Argument type (array)
private function testArray1(array $ary)
{
print_r($ary);
}
//nullable declaration
private function testArray2(?array $ary)
{
if($ary === null)
{
echo "array is null".PHP_EOL;
return;
}
print_r($ary);
}
private function checkArray(?array $ary)
{
if($ary === null)
{ echo "array is null".PHP_EOL;
return;
}
if(empty($ary))
{
echo "array is empty.".PHP_EOL;
return;
}
if(!is_array($ary))
{
echo "it is not an array.".PHP_EOL;
return;
}
//Check for existence of name key
if (!isset($ary['name']))
{
echo "'name' key does not exist.".PHP_EOL;
return;
}
//Check for existence of age key
if (!isset($ary['age']))
{
echo "'age' key does not exist.".PHP_EOL;
return;
}
//Check for string value of name
if (!is_string($ary['name']))
{
echo "'name' is not a string.".PHP_EOL;
return;
}
//Check for integer value of age
if (!is_int($ary['age']))
{
echo "'age' is not an integer.".PHP_EOL;
return;
}
echo "Associative array check completed.";
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->test1();
$cls2->test2();
echo "</pre>";
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |