在关联数组中存储多个值并在php中返回该数组

时间:2017-11-17 03:36:18

标签: php arrays associative-array

我已经看到很多答案(所以请不要将其复制)以将值存储在关联数组中,但我想在PHP中返回该数组。这是我的代码。它会打印所有值,但只返回第一个值。我希望返回的整个数组用于另一个函数。

请帮忙

function xml_parsing($response,$size,$array)
{
    for($k=0;$k<$size;$k++)
    {
        $price=(string)$response->Items->Item[$k]->ItemAttributes->ListPrice->FormattedPrice; 
        $myarray[$k]=explode(',',$array["ItemId"]);
        $update_fields=array('sku','price');  
        if($price=='') 
        {
            $Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>"-1");
        }
        else
        {
            $price_trimed=ltrim($price,'$');
            $Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>$price_trimed); 
            // I store the values here using a loop
        }
    }
    print_r($Col_array);
    return $col_array; //but here it return only first value
                       // But I want to return the whole array**
                       // I can't return it inside loop because it terminates 
                       // the loop and the function                               
}  

3 个答案:

答案 0 :(得分:0)

一些伪代码:

//first, initialize empty array:
$Col_array = [];

//do the loop thing.
while(...){

    //inside the loop:

    //add to array:
    $Col_array []= array('sku'=>"".$myarray[$k][$k]."",'price'=>$price_trimed); 


}//loop ends here
print_r($Col_array); //<----- all values.
return $Col_array;

注意如何使用[] =附加到数组。

答案 1 :(得分:0)

实际上,这里的错误是你没有存储循环中遍历的数据。您需要将$Col_array推送到主阵列以获得所需的结果。这是你的代码

function xml_parsing($response,$size,$array)
{
    //create an empty array before entering the for loop.
    $main_array = array();

    for($k=0;$k<$size;$k++)
    {
        $price=(string)$response->Items->Item[$k]->ItemAttributes->ListPrice->FormattedPrice; 
        $myarray[$k]=explode(',',$array["ItemId"]);
        $update_fields=array('sku','price');  
        if($price=='') 
        {
            $Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>"-1");
        }
        else
        {
            $price_trimed=ltrim($price,'$');
            $Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>$price_trimed); 
            // I store the values here using a loop
        }

        //Here you push the $col_array to main_array
        array_push($main_array,$Col_array);
        //This will store whole bunch of data as multi dimensional array which you can use it anywhere.
    }
    print_r($main_array);
    return $main_array;                               
}  

我想你会得到你想要的。

答案 2 :(得分:0)

我想回答我自己的问题

     function xml_parsing($response,$size,$array)
       {



  for($k=0;$k<$size;$k++)
          {


  $price=(string)$response->Items->Item[$k]->ItemAttributes->ListPrice-
  >FormattedPrice; 



    //echo "PKkkkkkkkk".$array["ItemId"]."llllll";
   $myarray[$k]=explode(',',$array["ItemId"]);
   //print_r($myarray[$k]);
   $update_fields=array('sku','price');


   if($price=='') 
   {
   // $Col_array=array('sku'=>"".$myarray[$k][$k]."",'price'=>"-1");


    /*Here is the solution we have to index it inside the loop like a 2D 
      array now it contain an array inside which array of key value pair is 
      present (associative array) which i wanted */


     //**********
      $Col_array[$k]['sku']=$myarray[$k][$k];
      $Col_array[$k]['price']="-1";
     //*********
    }
    else
    {
     $price_trimed=ltrim($price,'$');
     // final array to be stored in database
    // $Col_array=array('sku'=>"".$myarray[$k]
[$k]."",'price'=>$price_trimed);
    $Col_array[$k]['sku']=$myarray[$k][$k];
    $Col_array[$k]['price']=$price_trimed;

     }


 }

      return $Col_array;
    }



    //*******
     //To Print the returned array use


      foreach ($Col_array as $value) {
    print_r($value);
       }

     //*********