how to replace value with spesific column with searching id on array - php

i have some problem with data array, how to replace spesific array column with searching in column id in array
data json array like this :
$json ='
[
{"id":1,"name":"Eko","approved":"n"},
{"id":2,"name":"Rudi","approved":"n"},
{"id":3,"name":"Yanto","approved":"n"}
]
';
ihave tried many tutorial but not spesific replace array
iwant output like this :
$json =
'
[
{"id":1,"name":"Eko","approved":"n"},
{"id":2,"name":"Rudi","approved":"y"},
{"id":3,"name":"Yanto","approved":"n"}
]
';
i apreciate with your help
defferent is "approved":"y" in json2

$array = json_decode($json);
foreach ($array as $key => $item) {
if($item->id == $your_serach_id){
$array[$key]->approved = "y";
}
}
I hope this helps.

You can tirverse the JSON object using for loop or foreach look and fetch the key value pair in the array and for getting your desired output you will need to know the value of which key you have to change so in the loop you can array[i] ->key = new_valuethis line should be thier after if condition in for loop

Related

how to update table from array multidimension with php

how can i update data from array multidimension in php, but its from key and value array like this :
$array1=array(
array('data1'=>'name','data2'=>'age'),
array('data1'=>'names','data2'=>'ages')
);
how can i get result from array like this
UPDATE tablename SET data1='name',data2='age' WHERE 1;
i tried this but the result not like above
foreach($array1 as $arrays) {
foreach($arrays as $key => $value){
echo $key."="."'".$value."'".", ";
}
}
result:
data1='name',data2='age', data1='name',data2='age'
i want result like this :
data1='name',data2='age'
i hope can help me.
Because you only want to get content of first array, you should loop through first array only
foreach($array1[0] as $key => $value){
echo $key."="."'".$value."'".", ";
}
// data1='name', data2='age',

merge array inside foreach loop

I want to merge array result inside foreach loop. Is there any solution about this or other best way. Thanks in advance.
PHP code
include('Config.php');
$data = json_decode(file_get_contents("php://input"));
foreach($data->dataVal as $key => $value){
echo json_encode($config->query($key,$value));}
//this is the response code above --> how to merge this 2,3 array result into one only
[{"name":"Roi"}][{"name":"Tom"}][{"name":"Chad"}]
//result to be expected like this
[{"name":"Roi","name":"Tom","name":"Chad"}] // this is i want
The nearest you will get is by building up an array of the data in the foreach loop and JSON encoding the result...
$result = array();
foreach($data->dataVal as $key => $value){
$result[] = $config->query($key,$value);
}
echo json_encode($result);
This stops having repeated values for the name.
In your original attempt, you were encoding each row separately, so the JSON was invalid.

Multidimensional array - how to get specific values from array

I have converted Excel data into Php array using toArray()..
actually my result is
My question how to get value from this Multidimensional array? and also how to i get header and value from array in separately?
you can access value, by following the path of the array. so in your case :
$yourArrayName['Worksheet'][0][0], will return SNo
Get one value:
print_r($your_array['Worksheet'][0]);
Get values with loop:
foreach ($your_array as $key => $value)
{
echo $key; // 'Worksheet'
print_r $value;
}
If you are trying to extract all the values of specific key then you should use for or foreach or while loop...
its something like
<?php
$array = YOUR ARRAY;
$c=count($array);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['StudentName'];
}
?>
else if you want to get a specific value manually You can easily traverse to your value as
$array = YOUR ARRAY
echo $ara['Worksheet'][0]['StudentName']

how to Access to nested items in JSON with foreach PHP?

I have the following JSON structure
{"Id":"1","Persons":[{"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""}, {"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""}{"Name":"Luis","Time":"00:00:08","info":"","Timeext":"","Timeout":"","Timein":""}]}
How I can have acces or read the item inside the nest? For example if I just want the value of time for Carl or all information about Carl. Till now I just can get without a problem the single item in the collection 'Id'. The rst nested items not.
I tryed with json_decode like this:
if( $_POST ) {
$arr['Id'] = $_POST['Id'];
$arr['NP'] = $_POST['NP'];
$jsdecode = json_decode($arr);
foreach ($jsdecode as $values){
echo $values->Time;
}
Can PLEASE somebody help me?
if( $_POST ) {
$arr['Id'] = $_POST['Id'];
$arr['NP'] = $_POST['NP'];
$jsdecode = json_decode($arr,true);
foreach ($jsdecode as $values){
echo $values->Time;
}
Adding 'true' to json_decode converts it to array
You are processing it correct , Just add true as the second parameter to json_decode which will be converted to an Array like
$jsdecode = json_decode($arr,true);

php remove element from array without add key

I want to remove an element from an array (converted from json), but with unset, and reconvert in json, the array become indexed.
Source array:
{"rows":
[{"c":[{"v":"Date(1409052482000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
]}
Result:
{"rows":
"2":{"c":[{"v":"Date(1409052614000)"},{"v":22},{"v":22},{"v":22},{"v":null}]},
"3":{"c":[{"v":"Date(1409052782000)"},{"v":22},{"v":22},{"v":22},{"v":null}]}
}}
the problem is the "2" and "3" keys. I don't want this keys, because I use the data for google chart, and is sensible for this index key.
PHP code:
$tempdata = json_decode($jsonTempLog, TRUE);
foreach ($tempdata['rows'] as $key => $row) {
if ( $logtime < $showtime) {
unset($tempdata['rows'][$key]);
}
}
echo json_encode($tempdata);
How can I remove element from array, keep the original json syntax?
Just do this:
$tempdata["rows"] = array_values($tempdata["rows"]);
echo json_encode($tempdata);
Otherwise JSON thinks you're sending an associative array rather a numeric one
this is how i work with :
unset($infos[$i]);
$infos = array_values($infos);
maybe like this:
foreach($tempdata as $row){
$tempdata[$rows['keyfield']] = $row;
}

Categories