I want to select the "RSI" from the first element only.
Array (json file):
{
"Technical": {
"2019-01-11 15:30": {
"RSI": "123"
},
"2019-01-11 14:30": {
"RSI": "456"
}
"2019-01-11 14:30": {
"RSI": "789"
}
}
My php:
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123456789
}
I want only 123
I tried:
echo $value[0]['RSI']; // Gives NULL
Break the loop with break; and it will only return the first item.
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123
break;
}
If you want specific items then use a "$key" variable.
$key = 0;
foreach ($json['Technical'] as $field => $value) {
if($key == 0 || $key ==1){
echo $value['RSI'];
}
$key++;
}
// 123456
Change the if to suit your needs.
Related
I need to modify the data structure of json array list as per some key value using PHP. I am explaining my code below.
<?php
$output=array(
array(
"first_name"=>"robin",
"last_name"=>"sahoo",
"reg_no"=>12,
"paper_code"=>"BA001"
),array(
"first_name"=>"robin",
"last_name"=>"sahoo",
"reg_no"=>12,
"paper_code"=>"BA002"
),array(
"first_name"=>"Rama",
"last_name"=>"Nayidu",
"reg_no"=>13,
"paper_code"=>"BA001"
)
);
//echo json_encode($output);
$result=array();
foreach ($output as $key => $value) {
if (count($result)==0) {
$result[]=array(
"name"=>$value["first_name"].' '.$value['last_name'],
"reg_no"=>$value['reg_no'],
"paper1"=>$value['paper_code'],
"paper2"=>"",
"paper3"=>"",
"paper4"=>""
);
}
}
The output of the input array is given below.
// Output:
[
{
"first_name":"robin",
"last_name":"sahoo",
"reg_no":12,
"paper_code":"BA001"
},
{
"first_name":"robin",
"last_name":"sahoo",
"reg_no":12,
"paper_code":"BA002"
},
{
"first_name":"Rama",
"last_name":"Nayidu",
"reg_no":13,
"paper_code":"BA001"
}
];
The above is my array list. Here I need to modify the all row value by reg_no means if there are multiple rows including same reg_no then those will merge with joining the both name and my expected output should like below.
expected output:
[
{
'name':"robin sahoo",
"reg_no":12,
"paper1":"BA001",
"paper2":"BA002",
"paper3":"",
"paper4":""
},
{
'name':"Rama Nayidu",
"reg_no":13,
"paper1":"BA001",
"paper2":"",
"paper3":"",
"paper4":""
}
]
Here paper1,paper2,paper3,paper4 will be selected serially means suppose same reg_no=12 has first row paper_code= BA001 then it will be paper1=BA001 and second row paper_code=BA002 then it will be paper2=BA002 and so on. Here I am using PHP to map this array.
Try the following, Let me know..
$output=array(array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA001"),array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA002"),array("first_name"=>"Rama","last_name"=>"Nayidu","reg_no"=>13,"paper_code"=>"BA001"));
//echo json_encode($output);
$result=array();
$temp=array();
if(!empty($output)){
foreach ($output as $key => $value) {
if(isset($temp[$value['reg_no']])){
if(empty($temp[$value['reg_no']]["paper1"]) || $temp[$value['reg_no']]["paper1"] == ""){
$temp[$value['reg_no']]["paper1"] = $value['paper_code'];
}else if(empty($temp[$value['reg_no']]["paper2"]) || $temp[$value['reg_no']]["paper2"] == ""){
$temp[$value['reg_no']]["paper2"] = $value['paper_code'];
}else if(empty($temp[$value['reg_no']]["paper3"]) || $temp[$value['reg_no']]["paper3"] == ""){
$temp[$value['reg_no']]["paper3"] = $value['paper_code'];
}else if(empty($temp[$value['reg_no']]["paper4"]) || $temp[$value['reg_no']]["paper4"] == ""){
$temp[$value['reg_no']]["paper4"] = $value['paper_code'];
}
}else{
$temp[$value['reg_no']] = array("name"=>$value["first_name"].' '.$value['last_name'],"reg_no"=>$value['reg_no'],"paper1"=>$value['paper_code'],"paper2"=>"","paper3"=>"","paper4"=>"");
}
}
}
if(!empty($temp)){
foreach ($temp as $key => $value) {
$result[] = $value;
}
}
This Code May help you
<?php
$output=array(array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA001"),array("first_name"=>"robin","last_name"=>"sahoo","reg_no"=>12,"paper_code"=>"BA002"),array("first_name"=>"Rama","last_name"=>"Nayidu","reg_no"=>13,"paper_code"=>"BA001"));
//echo json_encode($output);
$result=array();
foreach ($output as $key => $value) {
if (count($result)==0) {
$output[$key]=array("name"=>$value["first_name"].' '.$value['last_name'],"reg_no"=>$value['reg_no'],"paper1"=>$value['paper_code'],"paper2"=>"","paper3"=>"","paper4"=>"");
}
}echo "<pre>";print_r($output);
?>
You can try with this
$result = []; // Initialize result array
foreach ($output as $key => $value) {
$name = $value['first_name'] . ' ' . $value['last_name'];
// check if same name already has entry, create one if not
if (!array_key_exists($name, $result)) {
$result[$name] = array(
'name' => $name,
'reg_no' => $value['reg_no'],
'paper1' => '',
'paper2' => '',
'paper3' => '',
'paper4' => ''
);
}
// count array elements with value, then set paper number and value
$paper = 'paper' . (count(array_filter($result[$name])) - 1);
$result[$name][$paper] = $value['paper_code'];
}
$result = array_values($result); // reindex result array
$result = json_encode($result); // Encode to json format
print_r($result); // print result
This assumes that first_name and last_name is always same for each reg_no
Hello i have a json array which contains an array structure. I am trying to fetch the value against a particular key. Like getting a key with name lower and value 9226. I have implemented for each loop but i cannot get into it. There is some issue in my comparison statement.
{
"tax_structure": [
{
"tax_data": {
"lower": 0,
"upper": 9225,
"tax_rate": 10,
"amount": 0
}
},
{
"tax_data": {
"lower": 9226,
"upper": 37450,
"tax_rate": 15,
"amount": 922.50
}
}
]
}
Php file:
<?php
$expense=10000;
$str_data = file_get_contents("data.json");
$data = json_decode($str_data,true);
foreach ($data["tax_structure"] as $tax_data) {
foreach ($tax_data["tax_data"] as $key => $value) {
if($key=="lower" && $expense>$value) & ($key=="upper" &&$expense<$value)
{
//do something if expenses value falls between the lower and upper limit values of keys
}
}
}
?>
I believe you are looking for something like the following.
There is no need for the second for loop.
$expense = 10000;
$str_data = file_get_contents("data.json");
$data = json_decode($str_data,true);
foreach ($data["tax_structure"] as $tax_data_object) {
if($tax_data_object['tax_data']['lower'] < $expense && $tax_data_object['tax_data']['upper'] > $expense){
// Do stuff
}
}
EDIT
You should try print_r($data);. This will show the structure of your data array.
If you examine the structure of your data the above code should be obvious.
If it isn't, here are a few sources for further learning:
PHP: Arrays
JSON introduction
<?php
$expense = 10000;
$str_data = file_get_contents("data.json");
$data = json_decode($str_data, true);
foreach ($data["tax_structure"] as $tax_data) {
foreach ($tax_data["tax_data"] as $key => $value) {
if ($key == "lower" && $expense > $value) {
echo "Key is lower and the value is " . $value . "<br>";
}
if ($key == "upper" && $expense < $value) {
echo "Key is upper and the value is " . $value . "<br>";
}
}
}
?>
You cant do this if($key=="lower" && $expense>$value) & ($key=="upper" &&$expense<$value)
I have problem with two arrays:
$pole1 = array(
array("klic"=>"banan", "jmeno"=>"Banán"),
array("klic"=>"pomeranc", "jmeno"=>"Pomeranč"),
);
$pole2 = array(
array("klic"=>"banan"),
);
Now I need foreach data:
foreach ($pole1 as $key => $val){
//all data from $pole
echo $val
//and here if "klic" from $pole1 == "klic" from $pole2
if ($pole2[$key]["klic"] == $pole1["klic"])
echo "YES"; // - **not working**
}
I need to check if data from $pole1 equals data from $pole2 and write some text but I need to write all data from $pole1.
You meant that?
foreach ($pole1 as $key => $val) {
if ( isset($pole2[$key]["klic"] &&
($pole2[$key]["klic"] == $pole1[$key]["klic"]) )
echo "YES";
}
try this
foreach($pole1 as $k1 => $arrays) {
foreach($arrays as $k2 => $val) {
if($pole2[$k1][$k2] == $val) {
// $pole1[$k1][$k2] is equal to $pole2[$k1][$k2]
}
}
This will echo every entry in pole1 and check every entry in pole1 with every entry in pole2.
foreach($pole1 as $val){
echo($val);
foreach($pole2 as $val2){
if($val['klic']==$val2['klic']) echo 'YES';
}
}
I have a SESSION array $_SESSION['cart']
for example it has value as
Array
(
[0] =>
[1] => Array
(
[item_id] => 1420
[item_qty] => 1
)
[2] => Array
(
[item_id] => 1522
[item_qty] => 1
)
)
Now let's say I have item_id = 1420
now I want to increase the item_qty for item_id = 1420 and also I have to set it in that SESSION array.
What I tried it :
foreach($_SESSION['Cartquantity'] as $key => $d)
{
if(isset($d)) {
if($d['item_id'] == $_GET['item_id'])
{
$d['item_quntity'] = $d['item_quantity']+1;
}
}
else{
}
}
It's not working ?
You're iterating over $_SESSION['Cartquantity'] but have told us that the array is stored in $_SESSION['cart'].
Also, this:
$d['tem_quntity'] = $d['item_quantity']+1;
Should be:
$d['item_qty'] = $d['item_qty']+1;
Finally, you'll need to make $d a reference by prepending an ampersand (&) to it in the foreach condition.
foreach($_SESSION['cart'] as $key => &$d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$d['item_qty'] = $d['item_qty']+1;
}
}
else{
}
}
Use reference &$d
foreach($_SESSION['Cartquantity'] as $key => &$d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$d['tem_quntity'] = $d['item_quantity']+1;
}
}
else{
}
}
or
foreach($_SESSION['Cartquantity'] as $key => $d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$_SESSION['Cartquantity'][$key]['item_quntity'] = $d['item_quantity']+1;
}
}
else{
}
}
change this
$d['tem_quntity'] = $d['item_quantity']+1;
to
$d['item_qty'] = $d['item_qty']+1;
you can call by
foreach ($_SESSION['Cartquantity'] as $value))
{
if(isset($value))
{
if($value['item_id'] == $_GET['item_id'])
{
$value['item_quntity'] = $value['item_quantity']+1;
}
}
else
{
}
}
Try to check for !empty($array)
if(!empty($d))
because it array so you need to check for if its do not have any elements inside of it.
If you want to know if the array is defined , then use isset($d).
If you want to know if a particular key is defined, use isset($d['item_id']).
If you want to know if an array has not empty and has key value pairs , use !empty($d).
use it:
foreach($_SESSION['cart'] as $key => $d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$d['tem_quntity'] = $d['item_qty']+1;
}
}
else{
}
}
foreach($_SESSION['cart'] as $key=>$val)
{
foreach($val as $subK)
{
if($_GET["item_id"]==$subk["item_id"])
{
$_SESSION['cart'][$key]["item_id"]=$_SESSION['cart'][$key]["item_quantity"]+1;
}
}
}
I have following array construction: $array[$certain_key][some_text_value]
And in a while loop, I want to print the data from the array, where $certain_key is a specific value.
I know how to loop through multidimensional arrays, which is not the complete solution to this problem:
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
I do not want to loop the whole array each time, but only when $certain_key is matched.
EDIT: to be more exact, this is what I'm trying to do:
$array[$array_key][some_text];
while reading from db {
//print array where a value returned from the db = $array_key
}
while ($row = fetch()) {
if (isset($array[$row['db_id']])) {
foreach ($array[$row['db_id']] as $some_text_value => $some_text_values_value) {
echo ...
}
}
}
foreach ($array as $certain_key => $value) {
if($certain_key == $row['db_id']) {
foreach ($value as $some_text_value) {
echo "$v2\n";
}
}
}
You mean like
foreach($array[$certain_key] as $k => $v)
{
do_stuff();
}
?
Maybe you're looking for array_key_exists? It works like this:
if(array_key_exists($certain_key, $array)) {
// do something
}
<?php
foreach ($a as $idx => $value) {
// replace [search_value] with whatever key you are looking for
if ('[search_value]' == $idx) {
// the key you are looking for is stored as $idx
// the row you are looking for is stored as $value
}
}