dongqing4774 2010-12-18 03:39
浏览 42
已采纳

如何将此数组存储到多个变量?

Hi how do I store this array to two different variables instead of echo??

$countries = array();
foreach ($my_data as $node)
{
    foreach($node->getElementsByTagName('a') as $href)
    {
        preg_match('/([0-9\.\%]+)/',$node->nodeValue, $match);
        $countries[trim($href->nodeValue)] = $match[0]; 
    }
}    

foreach ($countries as $country => $percent) echo str_replace("Â","",(strip_tags($country))) . ' - ' . str_replace("Â","",(strip_tags($percent)));

This will output

USA - 75%
UK - 65%
AU - 56%
UAE - 52%

and so on What I am looking is I need this array to store in multiple variable for example

$datac = USA,UK,AU,UAE

$datap = 75%,65%,56%,52%

like that any idea?

  • 写回答

1条回答 默认 最新

  • dounieliang4712 2010-12-18 03:40
    关注
    $datac = array();
    $datap = array();
    
    foreach($countries as $country => $percent) {
        $datac[] = str_replace("Â","",(strip_tags($country)));
        $datap[] = str_replace("Â","",(strip_tags($percent)));
    }
    

    If you want them as strings, you can just do:

    $datac = implode(',', $datac);
    $datap = implode(',', $datap);
    

    Reference: implode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题