0 レビュー
2 回答
wordpress-有効な配列のように見えるものでphpのカウント可能なエラーを回避する方法
php count()エラーが発生します。'警告:count():パラメータは、明らかに配列であるものにCountableを実装する配列またはオブジェクトである必要があります。コードは引き続き機能しますが、警告メッセージを回避するために再コード化する方法を知りたいです。
まず、多次元配列(print_fダンプ)があります:
$icons Array
(
[0] => Array
(
[image] => 12811
[label] => Chemical
[categories] => Array
(
[0] => 209
)
)
[1] => Array
(
[image] => 12812
[label] => Cut
[categories] => Array
(
[0] => 236
)
)
[2] => Array
(
[image] => 12813
[label] => Flame
[categories] => Array
(
[0] => 256
[1] => 252
)
)
)
そして私はWordpressの用語を画像に一致させています:
<?php
$terms = wp_get_post_terms( get_the_ID(), 'product_categories', array("fields" => "ids"));
if($icons) {
foreach($icons as $row) {
for($i=0; $i<count($row['categories']); $i++) {
for($j=0; $j<count($terms); $j++) {
if($row['categories'][$i]==$terms[$j]) {
array_push($icon_img_ary,$row['image']);
$icon_img_ary_unq=wg_unique_array($icon_img_ary);
}
}
}
}
}
} ?>
ネストされた配列をカウントしているときに、最初のfor()ループでエラーが発生します。私は実際にこの同じコードを何ヶ月も使用しており、2つの別々のドキュメントに2つのインスタンスがあります。このエラーは、ドキュメントの1つでのみ発生します。配列が配列として入力されない理由を理解しようと、髪の毛を抜いてきました。
条件付きで配列変数&&count($ array)を使用するいくつかのソリューションについて説明しましたか?これは、後続の';'でエラーをスローするまったく新しい構文のようなものです。または{}文字。非常に紛らわしいです、私は理解を得ようとしています。どんな助けでも大歓迎です、ありがとう!
わからない
0
レビュー
答え :
解決策:
PHP 7.3
を使用している場合は、 is_countable()
を使用できます。それ以外の場合は、 is_array()
。
PHP 7.3以降の場合:
<?php
$terms = wp_get_post_terms( get_the_ID(), 'product_categories', array("fields" => "ids"));
if($icons) {
foreach($icons as $row) {
if ( is_countable( $row['categories'] ) ) {
for($i=0; $i<count($row['categories']); $i++) {
for($j=0; $j<count($terms); $j++) {
if($row['categories'][$i]==$terms[$j]) {
array_push($icon_img_ary,$row['image']);
$icon_img_ary_unq=wg_unique_array($icon_img_ary);
}
}
}
}
}
}
?>
以下のPHP7.3の場合:
<?php
$terms = wp_get_post_terms( get_the_ID(), 'product_categories', array("fields" => "ids"));
if($icons) {
foreach($icons as $row) {
if ( is_array( $row['categories'] ) ) {
for($i=0; $i<count($row['categories']); $i++) {
for($j=0; $j<count($terms); $j++) {
if($row['categories'][$i]==$terms[$j]) {
array_push($icon_img_ary,$row['image']);
$icon_img_ary_unq=wg_unique_array($icon_img_ary);
}
}
}
}
}
}
?>
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。