doucuyu2259 2016-08-11 13:51
浏览 26
已采纳

如何计算具有相同属性的数组

I have an array like below. What I want is to echo a word according to number of $my[]['title'] items. in this case the word must be repeated 4 times. the sudo code is sth like this:

<?php
$my[0]['title']='first title';
$my[0]['description']='description';
$my[0]['date']='date';
$my[1]['title']='second title';
$my[1]['description']='description';
$my[1]['date']='date';
$my[2]['title']='third title';
$my[3]['title']='forth title';

for($i=0;$i<count($my[]['title'];$i++)
    echo 'this is test';
?>
  • 写回答

7条回答 默认 最新

  • douhui1957 2016-08-11 13:57
    关注

    Your test data would work fine with a foreach loop, but assuming that your array can contains rows that don't have a title key, you can use array_column:

    $count = count(array_column($my, 'title'));
    
    for($i=0; $i<$count; $i++) {
        echo 'this is test';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度