douying2243 2014-11-02 20:31
浏览 29
已采纳

PHP数组不显示所有项目

I have this code for an array, and the problem is that it shows only two last items of the array.

$svi= array(
        $WMN1,
        $LNG1,
        $SSWN1,
        $DT1,
        $OET1,
        $OW1,
        $NT1  
    );

    $imena_sajtova = array(
        'Prvi WMN',
        'Drugi LNG',
        'Treci SSWN',
        'Cetvrti DT',
        'Peti OET',
        'Sesti OW',
        'Sedmi NT'
    );

    $novi_svi =array_combine($svi, $imena_sajtova);

    echo '<pre>'; print_r($novi_svi); echo '</pre>';

And the result of this code is:

Array
(
    [2] => Sedmi NT
    [1] => Sesti OW
)

What could be a problem? Thanks!

var_dump($svi) displays this:

array(7) { [0]=> int(2) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(2) [5]=> int(1) [6]=> int(2) }

This $svi array has results from some functions:

$WMN1=RSS_Brojac($WMN);
$LNG1=RSS_Brojac($LNG);
$SSWN1=RSS_Brojac($SSWN);
$DT1=RSS_Brojac($DT);
$OET1=RSS_Brojac($OET);
$OW1=RSS_Brojac($OW);
$NT1=RSS_Brojac($NT);

I've changed order of arrays in the array_combine, and it works!

$novi_svi =array_combine($imena_sajtova, $svi);

    arsort($novi_svi);


    echo '<pre>'; print_r($novi_svi); echo '</pre>';

And the output of this code is:

Array
(
    [Prvi WMN] => 2
    [Sedmi NT] => 2
    [Peti OET] => 2
    [Sesti OW] => 1
    [Cetvrti DT] => 1
    [Drugi LNG] => 1
    [Treci SSWN] => 1
)

I'have sorted them from high to low, that's what I actually wanted to do with this array. Now, how to separate the results, to have them in separate divs inside html? So then, I'll be able to change their style and create some kind of table with these results.

The results are output from some RSS feeds, which I use to count number of published news.

Thank you for your time!

  • 写回答

2条回答 默认 最新

  • doulouxun6756 2014-11-02 21:03
    关注

    Well there you go, the exact reason why you only retrieve two values after the array combine is because you've only supplied two unique integers, 1,2 as the indexes of the arrays to be combined. You need at least 7 unique values for your 7 strings.

    Here's an example using your code and it doesn't work

    And...Here's an example using 7 unique indexes, that does work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应