連想配列に値を格納するための多くの回答を見てきましたが(重複とマークしないでください)、その配列を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
}
ここで私は自分の質問に答えたい
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);
}
//*********
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加