PHPの連想配列で要素数が1つしかない時の対処法

PHPPHP


Notice: Trying to access array offset on value of type bool in /var/www/html/wordpress/wp-content/plugins/internal-link-building-plugin/internal_link_building.php on line 86

Notice: Trying to access array offset on value of type bool in /var/www/html/wordpress/wp-content/plugins/internal-link-building-plugin/internal_link_building.php on line 105

foreachで回す

<?php 
$userList = array(array('name'=> name1,
'age' => 20,),
);

foreach ($userList as $user){}
print_r($user);
?>

array_shift()を使用

array_shift()では、配列の先頭要素のみを取得するメソッドになります。

<?php 
$userList = array(array('name' => name1,
'age' => 20,),
);
$user = array_shift($userList);
print_r($user);