I have this form of json :{"1":"7.00","2":"8.00"}
How can i transform it in this type of json?
{{"id":"1","qty":"7.00"},{"id":"2","qty":"8.00"}}
Thank you!
First is to json_decode() your JSON.
Now, you have an array with keys as IDs and values as quantities.
Iterate over the array and insert into the result with id and qty as keys.
Snippet:
<?php
$str = '{"1":"7.00","2":"8.00"}';
$data = json_decode($str,true);
$result = [];
foreach($data as $key => $val){
$result[] = [
'id' => $key,
'qty' => $val
];
}
echo json_encode($result);
Demo: https://3v4l.org/8VS4t
Related
My result is that the appended data look like this on form request in laravel
I want this type of array in my result the first data of append block store in single index and then vice versa all data
name=>basit
email=>kashif#gmail.com
contact=>5454
])
sub_contact => array([
name=>basit
email=>hamza12433#gmail.com
contact=>53543
])
You don't need to change your request array structure. Just loop through an element and insert values into database.
foreach($request->name as $key => $value){
Model::create([
'name' => $request->name[$key],
'email' => $request->email[$key],
'contact' => $request->contact[$key],
]);
}
You can loop one of the subarrays and use the key in a array_column to get all the values.
$keys = array_keys($arr);
foreach($arr[$keys[0]] as $index => $value){
$result[] = array_combine($keys, array_column($arr, $index));
}
var_dump($result);
https://3v4l.org/kUPi4
Or nest the foreach to get the values that way.
I believe this method is faster than array_column one.
foreach($arr as $key => $value){
foreach($value as $index => $item){
$result[$index][$key] = $item;
}
}
var_dump($result);
https://3v4l.org/DCP5W
Suppose I have this array,
$cast = [
'jon' => [
'fullname' => 'Jon Snow',
'class' => 'warrior',
],
'margery' => [
'fullname' => 'Margery Tyell',
'class' => 'politician'
]
];
How do I get the key and it's certain value only? like this,
$name = ['jon'=>'Jon Snow', 'margery'=>'Margery Tyrell'];
Is there any function that support this, so It doesn't have to be loop ?
Any answer will be appreciated!
You can iterate through the multidimensional array, and add the key and the value at index fullname of the inner array in a new one-dimensional array like this:
$names = [];
foreach ($cast as $character => $character_details) {
$names[$character] = $character_details['fullname'];
}
EDIT Alternatively, if you don't want to loop through the elements, you could use array_map function which takes a function as an argument where you can specify how to map each element of the array. In your case, you would simply return the fullname of an element.
$names = array_map(
function ($value) {
return $value['fullname'];
},
$cast
);
I guess that you have iterate over it and extract only interesting you keys. Here you have an example:
function getValuesForKey($array, $key){
$result = [];
foreach($array as $k => $subarray){
if(isset($subarray[$key])){
$result[$k] = $subarray[$key];
}
}
return $result;
}
I have searched for many places for an answer I couldn't find one, so please help me sort out this. - Thanks in advance
I have an Json String
{"first":{"val":100},"second":{"val":200},"third":{"val":300}}
which has been decoded and that has been saved in an php array
$arr = json_decode($json, true);
Now I tried to obtain those first, second, etc... values to concatenate with an sql query, but I couldn't get this sorted, so far I have tried like below,
foreach ($arr as $assoc) {
foreach ($assoc as $value) {
$val=$value+0;
$sqlI = "UPDATE tblName SET fieldName = ".$val." WHERE fieldName1=".$assoc;
$conn->query($sqlI);
}
}
*Note: the fieldName1 will have unique values..
I am getting Notice: Array to string conversion error I understand this due to $assoc is an array type but how to get the key from this array?
Your array would look like this:
$arr = Array(
'first' => array(
'val' => 100
),
'second' => array(
'val' => 200
),
'thirth' => array(
'val' => 300
),
);
You can access them like so:
echo $arr['first']['val']; // 100
Loop trough them like so:
foreach($arr as $val){
echo $val['val']; // 100, 200, 300.
}
Or with key:
foreach($arr as $key => $val){
echo "current key: '$key' with val '". $val['val'] ."'";
}
i had a array to each its item was object , i've converted that array with following code :
json_decode(json_encode($array), true)
that the result of code was a array like this :
[
'1'=>[
'slug'=>'a'
'title'=>'foo'
],
'2'=>[
'slug'=>'b'
'title'=>'bar'
],
'3'=>[
'slug'=>'c'
'title'=>'foo'
],
]
now i want to covert this array to somethings like this
[
'a'=>'foom',
'b'=>'bar',
'c'=>'foo',
]
how can i do it ??
Use foreach and array_combine()
foreach ($your_array as $key => $value) {
// get all the keys in $slug array
$slug[] = $value['slug'];
// get all the values in $title array
$title[] = $value['title'];
}
// finally combine and get your required array
$required_array = array_combine($slug, $title);
I think it can also be acheived with -
$requiredArray = array_combine(
array_column($your_array, 'slug'),
array_column($your_array, 'title')
);
You have to iterate over the initial array and create the new one like this:
$array = [
'1'=>[
'slug'=>'a'
'title'=>'foo'
],
'2'=>[
'slug'=>'b'
'title'=>'bar'
],
'3'=>[
'slug'=>'c'
'title'=>'foo'
],
];
$result = [];
foreach($array as $elem){
$index = $elem["slug"];
$value= $elem["title"];
$result[$index] = $value;
}
foreach($array as $elem){
$result[$elem["slug"]] = $elem["title"];
}
I can't deal with this. Please help me. This is array:
$arr = array("data" => array(
array("id" => "5451"),
array("id" => "45346346")
));
for example how can i find the key for id 45346346 ?
$key = array_search($arr['data'], 45346346);
I have tried this but its not working.
I'm trying to delete that array line. I'm guessing I can do that with unset($key)
You have an array of array of arrays. $arr['data'] is an array with 2 values. These values are both arrays. array_search doesn't work, as 45346346 doesn't match an array.
You'd have you cook your own search, something like this:
function find_in_array($arr, $val){
$found = false;
foreach($arr as $k=>$x){
if(array_search($val, $x) !== FALSE){
$found = $k;
break;
}
}
return $found;
}
Then you can do: $key = find_in_array($arr['data'], 45346346);. This will return 1, the index of the array containing 'id' => 45346346 inside $arr['data'].
DEMO: http://codepad.org/pSxaBT9g