如何仅从自定义公共函数中获取特定值 [英] How to get only specific values out of an custom public function

查看:51
本文介绍了如何仅从自定义公共函数中获取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能是:

public function getData($symbol='', $stat='') {

    if (is_array($this->symbol)) {
        $symbol = implode("+", $this->symbol); //The Yahoo! API will take multiple symbols
    }

    if($symbol) $this->_setParam('symbol', $symbol);
    if($stat) $this->_setParam('stat', $stat);

    $data = $this->_request();

    if(!$this->history) {
        if ($this->stat === 'all') { 
            foreach ($data as $item) {

                //Add to $return[$symbol] array. Indice 23 is the symbol.
                $return[$item[23]] = array(
                    'name'                      =>  strip_tags($item[20]),
                    'price'                       =>  strip_tags($item[0]),
                    'change'                      =>  strip_tags($item[1]),
                    'volume'                      =>  strip_tags($item[2])

                    //'avg_daily_volume'            =>  strip_tags($item[3]),
                    //'stock_exchange'              =>  strip_tags($item[4]),
                    //'market_cap'                  =>  strip_tags($item[5]),
                    //'book_value'                  =>  strip_tags($item[6]),
                    //'ebitda'                      =>  strip_tags($item[7]),
                    //'dividend_per_share'          =>  strip_tags($item[8]),
                    //'dividend_yield'              =>  strip_tags($item[9]),
                    //'earnings_per_share'          =>  strip_tags($item[10]),
                    //'fiftytwo_week_high'          =>  strip_tags($item[11]),
                    //'fiftytwo_week_low'           =>  strip_tags($item[12]),
                    //'fiftyday_moving_avg'         =>  strip_tags($item[13]),
                    //'twohundredday_moving_avg'    =>  strip_tags($item[14]),
                    //'price_earnings_ratio'        =>  strip_tags($item[15]),
                    //'price_earnings_growth_ratio' =>  strip_tags($item[16]),
                    //'price_sales_ratio'           =>  strip_tags($item[17]),
                    //'price_book_ratio'            =>  strip_tags($item[18]),
                    //'short_ratio'                 =>  strip_tags($item[19]),
                    //'name'                        =>  strip_tags($item[20])
                );
            }
        } else {
            foreach ($data as $item)
                $return[] = array($this->stat => $item);
        }
    } elseif(is_array($this->history)) {
        $return = $data;
    }

    return $return;
}

在 index.php 上使用的是:

what is used on index.php is:

<?php $test3 = ($StockMarketAPI2->getData());
print_r($StockMarketAPI2->getData());
?>

输出为:

Array
(
    [-0.12 - -0.15%] => Array
    (
         [name] => Alibaba Group Holding Limited A
        [price] => 81.17
        [change] => -0.12
        [volume] => 17911579
     )

 [-0.10 - -0.20%] => Array
    (
        [name] => GoPro, Inc.
        [price] => 49.98
        [change] => -0.10
        [volume] => 4560642
    )

[+0.53 - +0.10%] => Array
    (
        [name] => Netflix, Inc.
        [price] => 557.03
        [change] => +0.53
        [volume] => 1272298
      )

)

但我唯一需要的是:

阿里巴巴、81、-0.12、gopro、49……等等……

alibaba, 81 , -0.12, gopro, 49 .... and so on...

所以,

我需要的不是某种形式的树形...

what i need is not some form of tree form...

||/\ \

喜欢当前的输出.但只有 1 行...

like the ouput currently. but just 1 line...

不是 ['s ]'s ('s )'s 'array's .... 或其他类似的符号...

not the ['s ]'s ('s )'s 'array's .... or other symbols like that...

我该怎么做?

是这样的吗:

print_r($StockMarketAPI2->getData([$item][0]));

print_r($StockMarketAPI2->getData( [$item][0] ));

推荐答案

尝试:

<?php $test3 = ($StockMarketAPI2->getData());
foreach($test3 as $t){
echo "New subarray!";
echo $t[name];
echo $t[price];
echo $t[change];
echo $t[volume];
}
?>

未测试

这篇关于如何仅从自定义公共函数中获取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆