-3

I have created a Soap client to request data from web services and the returned data is displayed like this with my CodeIgniter function"

{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}

but I want to format them like this by removing the getCodeResult

{"selling":"1000.67114","Buying":"9000.65789"}
Danish Ali
  • 2,354
  • 3
  • 15
  • 26

2 Answers2

1

I hope this may help you.

<?php
$jsonData = '{"GetCodeResult":
                {
                    "Selling":"1000.67114",
                    "Buying":"9000.65789"
                }
            }';
$data = json_decode($jsonData, true);

$arr_index = array();
foreach ($data as $key => $value) {    
    $arr_index = $value;    
}

echo json_encode($arr_index, true);
?>

Thanks.

If you want to get only the values of selling and buying means, try like below.

<?php
$jsonData = '{"GetCodeResult":
                {
                    "Selling":"1000.67114",
                    "Buying":"9000.65789"
                }
            }';
$data = json_decode($jsonData, true);

$arr_index = array();

foreach ($data as $key => $value) { 
    if($key == 'GetCodeResult'){
        $arr_index = $value;
    }    
}
foreach($arr_index as $values){
    $finalVal[] = $values;
}

echo json_encode($finalVal, true);
?>
Ramya
  • 199
  • 10
0

Decode to array and return only the 'GetCodeResult' then json_encode it.

echo json_encode(json_decode($json, true)['GetCodeResult']);

//{"Selling":"1000.67114","Buying":"9000.65789"}

https://3v4l.org/59RSc


To only echo the values you can assign them to variables using list and array_values.
list($selling, $buying) = array_values(json_decode($json, true)['GetCodeResult']);

echo $selling .PHP_EOL;
echo $buying .PHP_EOL;
//1000.67114
//9000.65789

https://3v4l.org/T5GNg

Andreas
  • 23,610
  • 6
  • 30
  • 62