This is my db result,
Array ([0] => Array ( [shopname] => Shop name [fueltype] => Pertol [amount] => 1000 )
[1] => Array ( [shopname] => dfsdfsd [fueltype] => Pertol [amount] => 54456 )
[2] => Array ( [shopname] => dfsdfsd [fueltype] => Disel [amount] => 54456 )
)
I need result like
[["Shop name", "Pertol", 1000],["dfsdfsd", "Pertol", 54456],["Shop name", "Disel", 54456]]
How to get like this, I have no idea?
array_map() along with array_values() will work for you:-
<?php
$array = Array ( '0' => Array ( 'shopname' => 'Shop name','fueltype' => 'Pertol','amount' => 1000 ),
'1' => Array ( 'shopname' => 'dfsdfsd' ,'fueltype' => 'Pertol','amount' => 54456 ),
'2' => Array ( 'shopname' => 'dfsdfsd','fueltype' => 'Disel','amount' => 54456 )
);
$values_data_only = array_map('array_values', $array);
$desire_result = json_encode($values_data_only);
echo $desire_result;
?>
Output:- https://eval.in/395344
Also via simple foreach() it is possible:-
<?php
$array = Array ( '0' => Array ( 'shopname' => 'Shop name','fueltype' => 'Pertol','amount' => 1000 ),
'1' => Array ( 'shopname' => 'dfsdfsd' ,'fueltype' => 'Pertol','amount' => 54456 ),
'2' => Array ( 'shopname' => 'dfsdfsd','fueltype' => 'Disel','amount' => 54456 )
);
$new_array = array();
foreach ($array as $k=> $arr){
$new_array[$k][] = $arr['shopname'];
$new_array[$k][] = $arr['fueltype'];
$new_array[$k][] = $arr['amount'];
}
echo "<pre/>";print_r($new_array);
$desired_result_2 = json_encode($new_array);
echo $desired_result_2;
?>
Output:-https://eval.in/395354
$mapped = array_map('array_values', $input_array); // apply filter so we dont get the keys
$json = json_encode($mapped);
Just try with:
$input = array( /* your input data */ );
$output = array();
foreach ($input as $data) {
$output[] = array_values($data);
}
Related
I want to remove key 0 from parent array and set child array as parent.
Here I will get single value so one array is ok for me.
My current array looks like this
Array
(
[0] => Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
)
I want it like below. expected result
Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
For this I tried like below but no success.
$new = array();
foreach ($data as $v){
$new = array_merge($new , array_values($v)) ;
}
but in my code it's removed key e.g id,api_key, etc....
I need key name also in my new array. please suggest
Remove the array_values
Solution
<?php
$test = array(
array
(
'id' => 3,
'api_key' => 'acount266'
)
);
$new = array();
foreach($test as $v){
$new = array_merge($new, $v);
}
var_dump($new);
Result
array(2) {
["id"]=>
int(3)
["api_key"]=>
string(9) "acount266"
}
According to documentation of PHP as mentioned
reset() function returns the value of the first array element, or
FALSE if the array is empty.
$array = array(
array(
'id' => 3,
'api_key' => 'acount266',
'auth_domain' => 'Tester26',
'database_url' => 'vcc.test.acc+27#gmail.com',
'project_id' => '12345',
'storage_bucket' => '',
'secret_key_path' => '',
'fcm_server_key' => 1,
'messaging_sender_id' => 0,
'key_phrase' => '',
'disable' => 0,
'created' => '',
'updated' => ''
)
);
print_r(reset($test));
I tried this:
$arr = array();
foreach ($examples as $example) {
foreach ($example as $e) {
array_push($arr, $e);
}
}
Don't overcomplicate this, reassigning the first element to the parent array is quick and easy:
<?php
$array =
array (
0 =>
array (
'first' => 'Michael',
'last' => 'Thompson'
)
);
$array = $array[0];
var_export($array);
Output:
array (
'first' => 'Michael',
'last' => 'Thompson',
)
Or:
$array = array_shift($array);
I have an array that contains two values, but i need upper value as key of lower value like value of name replace with value and also remove name from array.
Array
(
[0] => Array
(
[name] => firt_name
[value] => Robin
)
[1] => Array
(
[name] => last_name
[value] => Singh
)
[2] => Array
(
[name] => email
[value] => 123#gmail.com
)
[3] => Array
(
[name] => password
[value] => 12345
)
)
Here is the code
function key_replace($params = array())
{
if (!empty($params)) {
$array[] = array();
foreach ($params as $key => $value) {
$array[$value['name']] = $value['value'];
}
print_r($array);
}
}
Any solution appreciated!
Another approach is to use array_column and array_combine
array_combine(array_column($array, 'name'), array_column($array, 'value'));
https://3v4l.org/boAOI
A Simple foreach() will do the trick for you.
$result = [];
foreach($array as $k=>$v){
$result[$v['name']] = $v['value'];
}
print_r($result);
WORKING DEMO: https://3v4l.org/hH39i
$datas = $array = array
(
'0' => array
(
'name' => 'firt_name',
'value' => 'Robin'
)
,
'1' => array
(
'name' => 'last_name',
'value' => 'Singh'
)
,
'2' => array
(
'name' => 'email',
'value' => '123#gmail.com'
)
,
'3' => array
(
'name' => 'password',
'value' => '12345',
)
,
'4' => array
(
'name' => 'phone',
'value' => 'skdsjdkdjskd'
)
,
'5' => Array
(
'name' => 'city',
'value' => 'dskjdksjd'
)
,
'6' => Array
(
'name' => 'state',
'value' => 'kjksdjskdsk'
)
);
$array = '';
foreach ($datas as $key => $value) {
$array[$value['name']] = $value['value'];
}
echo '<pre>';
print_r($array);
echo '</pre>';
Array
(
[firt_name] => Robin
[last_name] => Singh
[email] => 123#gmail.com
[password] => 12345
[phone] => skdsjdkdjskd
[city] => dskjdksjd
[state] => kjksdjskdsk
)
I have below array,
Array ( [0] => Array ( [report_id] => 1 [amount] => 100.00 [category_name] => Trial1 ) [1] => Array ( [report_id] => 1 [amount] => 150.00 [category_name] => Trial2 ) [2] => Array ( [report_id] => 1 [amount] => 200.00 [category_name] => Trial2 )
What i want to send to have JSON with below format
It will get some of Equal category name and then send it as json.
[{'category_name': 'Trial1', 'Sum':100]}, {'category_name':'Trial2', 'Sum':350]
How can i achieve this?
Was thinking to get foreach loop and then make compare of category_name and use .=+ to get sum? but i lost there,
Thanks,
Try below solution:
<?php
$array = array (
'0' => Array ( 'report_id' => 1, 'amount' => '100.00', 'category_name' => 'Trial1' ) ,
'1' => Array ( 'report_id' => 1, 'amount' => '150.00' ,'category_name' => 'Trial2' ),
'2' => Array ( 'report_id' => 1, 'amount' => '200.00' ,'category_name' => 'Trial2' ) ,
);
$new_array = array();
foreach($array as $a){
if(!isset($new_array[$a['category_name']]['amount'])){
$new_array[$a['category_name']]['amount'] = 0;
}
$new_array[$a['category_name']] = array(
'category_name' => $a['category_name'],
'amount' => $new_array[$a['category_name']]['amount'] + $a['amount'],
);
}
//print_r(array_values($new_array));
echo json_encode(array_values($new_array));
Output
[{"category_name":"Trial1","amount":100},{"category_name":"Trial2","amount":350}]
Possible solution:
$categoriesArray = array();
foreach ($yourArray as $arrayItem) {
if (!isset($categoriesArray[$arrayItem['category_name']])) {
$categoriesArray[$arrayItem['category_name']] = array(
'category_name' => $arrayItem['category_name'],
'sum' => 0
);
}
$categoriesArray[$arrayItem['category_name']]['sum'] += $arrayItem['amount'];
}
$categoriesArray = json_encode(array_values($categoriesArray));
Assuming $input is your array and $output is the JSON string:
$categorysum = [];
array_walk($input, function($el) use (&$categorysum) {
$categorysum += [$el['category_name'] => ['category_name' => $el['category_name'], 'Sum' => 0]];
$categorysum[$el['category_name']]['Sum'] += $el['amount'];
});
$output = json_encode(array_values($categorysum));
This is my db result,
Array ([0] => Array ( [shopname] => Shop name [fueltype] => Pertol [amount] => 1000 )
[1] => Array ( [shopname] => dfsdfsd [fueltype] => Pertol [amount] => 54456 )
[2] => Array ( [shopname] => dfsdfsd [fueltype] => Disel [amount] => 54456 )
)
I need result like
[["Shop name", "Pertol", 1000],["dfsdfsd", "Pertol", 54456],["Shop name", "Disel", 54456]]
How to get like this, I have no idea?
array_map() along with array_values() will work for you:-
<?php
$array = Array ( '0' => Array ( 'shopname' => 'Shop name','fueltype' => 'Pertol','amount' => 1000 ),
'1' => Array ( 'shopname' => 'dfsdfsd' ,'fueltype' => 'Pertol','amount' => 54456 ),
'2' => Array ( 'shopname' => 'dfsdfsd','fueltype' => 'Disel','amount' => 54456 )
);
$values_data_only = array_map('array_values', $array);
$desire_result = json_encode($values_data_only);
echo $desire_result;
?>
Output:- https://eval.in/395344
Also via simple foreach() it is possible:-
<?php
$array = Array ( '0' => Array ( 'shopname' => 'Shop name','fueltype' => 'Pertol','amount' => 1000 ),
'1' => Array ( 'shopname' => 'dfsdfsd' ,'fueltype' => 'Pertol','amount' => 54456 ),
'2' => Array ( 'shopname' => 'dfsdfsd','fueltype' => 'Disel','amount' => 54456 )
);
$new_array = array();
foreach ($array as $k=> $arr){
$new_array[$k][] = $arr['shopname'];
$new_array[$k][] = $arr['fueltype'];
$new_array[$k][] = $arr['amount'];
}
echo "<pre/>";print_r($new_array);
$desired_result_2 = json_encode($new_array);
echo $desired_result_2;
?>
Output:-https://eval.in/395354
$mapped = array_map('array_values', $input_array); // apply filter so we dont get the keys
$json = json_encode($mapped);
Just try with:
$input = array( /* your input data */ );
$output = array();
foreach ($input as $data) {
$output[] = array_values($data);
}
How may I count the times a key shows up in a multi-array?
In the below example, weight should return 2, and reps 4. Does PHP have a built-in function for this?
[0] => Array
(
[0] => Array
(
[weight] => 317.51474856007
[reps] => 10
)
[1] => Array
(
[weight] => 50
[reps] => 10
)
[2] => Array
(
[reps] => 10
)
[3] => Array
(
[reps] => 10
)
)
you can try something like this:
function counter($array){
$result = array();
foreach($array as $key=>$value){
foreach($value as $name => $val){
$result[$name][]=$val;
}
}
return array('all'=>$result,'count weight'=>count($result['weight']),'count reps'=>count($result['reps']));
}
$array= Array(
'0' => Array
(
'weight' => 317.51474856007,
'reps' => 10
),
'1' => Array
(
'weight' => 50,
'reps' => 10
),
'2' => Array
(
'reps' => 12
),
'3' => Array
(
'reps' => 10
)
);
$resp = counter($array);
var_dump($resp);
Try this one liner using array_walk_recursive():
Given your array, $arr
array_walk_recursive($arr, function($v, $k)use(&$count){if(array_key_exists($k, $count))$count[$k]++;else $count[$k]=1;},$count=array());
print_r($count);
See demo
or the old fashioned way:
$count = array();
foreach ($arr as $ar){
foreach ($ar as $k=>$v){
if (array_key_exists($k, $count)){
$count[$k]++;
}
else{
$count[$k] = 1;
}
}
}
print_r($count);
See demo
Output:
Array
(
[weight] => 2
[reps] => 4
)
you can try this:
$array= Array(
'0' => Array
(
'weight' => 317.51474856007,
'reps' => 10
),
'1' => Array
(
'weight' => 50,
'reps' => 10
),
'2' => Array
(
'reps' => 10
),
'3' => Array
(
'reps' => 10
)
);
$weight=0;
$reps=0;
//print_r($array);
foreach($array as $key=>$val){
if(isset($val['weight'])){
echo $val['weight']."<br>";
$weight++;
}
if(isset($val['reps'])){
echo $val['reps']."<br>";
$resp++;
}
}
echo $weight." ".$resp;