I have one multi dimentional array, the problem is I want the array values of specific key. I already tried current() and end() of array which is not useful to me. So please suggest me appropriate solution to find array values of specific key without using any loop. My Demo array is
Array
(
[0] => Array
(
[EntityType] => Array
(
[Id] => 1
[Code] => SUP/13-14/10001
[Name] => Supplier
[DisplayName] => Supplier
[ModuleIdentifier] => 1
[IsAdd] =>
[IsEdit] => 1
[IsDelete] => 1
)
)
[1] => Array
(
[EntityType] => Array
(
[Id] => 2
[Code] => Emp/13-14-10002
[Name] => Employee
[DisplayName] => Employee
[ModuleIdentifier] => 1
[IsAdd] =>
[IsEdit] =>
[IsDelete] =>
)
)
[2] => Array
(
[EntityType] => Array
(
[Id] => 3
[Code] => CUS/13-14/10003
[Name] => Customer
[DisplayName] => Customer
[ModuleIdentifier] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] =>
)
)
)
I want array having name Customer. So how to get these array...
Thanks !
You may use array_filter in conjunction with array_map:
function findElem($array, $val) {
$result = array_map(
function ($v) { return $v['EntityType']; },
array_filter($array, function ($v) use($val) { return $v['EntityType']['Name'] == $val; })
);
return count($result)? $result[0] : false;
}
print_r(findElem($array, 'Customer'));
If you want to access n'th element of your array just try with:
$array[n]
Where n is an integer value, so:
$array[2]
This line will get you all values of the key "Name" assuming your source array is named $arr if that's what you wanted:
$names = array_map( function($item) { return $item["EntityType"]["Name"]; } , $arr );
You can access your array data like this: $array[0]["EntityType"]["ID"].
Related
I have an array in the format
Array
(
[1] => Array
(
[Id] => 1
[Name] => Anaa Airport (AAA)
[IsDomestic] => 0
)
[2] => Array
(
[Id] => 2
[Name] => Arrabury Airport (AAB)
[IsDomestic] => 0
)
)
I want to change it to this format
[
{"Name":"Anaa (AAA)","Id":1,"IsDomestic":0},
{"Name":"Arrabury (AAB)","Id":2,"IsDomestic":0},
]
I have tried json_encode function but this is giving me something like this
{"1":{"Id":1,"Name":"Anaa Airport (AAA)","IsDomestic":0},
"2":{"Id":2,"Name":"Arrabury Airport (AAB)","IsDomestic":0}
}
I don't want that 1,2 keys which.
I want to get the data in the exact form mentioned.
You can use array_map to rearrange the array in the desired order, than use array_values in json_encode for the output
$r = array_map(function($v){
return [
'Name' => str_replace('Airport ','',$v['Name']),
'Id' => $v['Id'],
'IsDomestic' => $v['IsDomestic']
];
}, $a1);//$a1 is the input array
echo json_encode(array_values($r));
Working example : https://3v4l.org/GZr6Q
Use array_values() to remove index
$json_out = json_encode(array_values($your_array_here));
$your_array = Array
(
[1] => Array
(
[Id] => 1
[Name] => Anaa Airport (AAA)
[IsDomestic] => 0
)
[2] => Array
(
[Id] => 2
[Name] => Arrabury Airport (AAB)
[IsDomestic] => 0
)
);
$new_array = [];
foreach($your_array as $array)
{
$new_array[] = json_encode($array);
}
dd($new_array);
I am working on a php script where I have a json api array that looks something like this --
[body] => stdClass Object
(
[api] => stdClass Object
(
[status] => 200
[message] => GET stat....
[results] => 379
[filters] => Array
(
[0] => gameId
[1] => playerId
)
[statistics] => Array
(
[0] => stdClass Object
(
[gameId] => 17
[totReb] => 7
[assists] => 5
[pFouls] => 4
[steals] => 3
[turnovers] => 2
[blocks] => 0
[plusMinus] => 9
[playerId] => 265
)
[1] => stdClass Object
(
[gameId] => 24
[teamId] => 7
[totReb] =>
[assists] =>
[pFouls] =>
[steals] =>
[turnovers] =>
[blocks] =>
[plusMinus] =>
[playerId] => 265
)
And I am trying to get the values for each object to then output the total, so for each [?] => stdClass Object it will get the specified key and total the value.
How can I add all the values from one key?
First get the array of statistics (the one you want to loop and sum on). Then use array_column to extract only specific key and then array_sum for summing it.
This is pseudo but should give you the idea:
$arr = $obj->body->api->statistics;
$keyToSum = "steals";
$totalSteals = array_sum(array_column($arr, $keyToSum));
Reference: array-column, array-sum
There is one custom function I have which fetches column data recursively like array_column.
function array_column_recursive(array $haystack, $needle) {
$found = [];
array_walk_recursive($haystack, function($value, $key) use (&$found, $needle) {
if ($key == $needle)
$found[] = $value;
});
return $found;
}
echo array_sum(array_column_recursive($arr, 'steals'));
Source link.
I'm currently looking for a solution to this problem:
if I have something like that:
Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
[3] => Array
(
[id] => 1
[name] => Paper
)
[4] => Array
(
[id] => 4
[name] => Puppy
)
)
The goal I'd like to reach here is create a new array which contains the arrays with the same ID. So basically, at the end I'm gonna have two arrays: the first will contain element with different IDs and the second will contain element with the same ID.
Any tips? Thank you in advance!
One way is by mainly using array_filter()
// Gather ids and count
$id = array_count_values(array_column($array, 'id'));
// Filter not unique
$notUnique = array_filter($array, function($e) use ($id) {
return ($id[$e['id']] > 1);
});
// Filter unique
$unique = array_filter($array, function($e) use ($id) {
return !($id[$e['id']] > 1); // or ($id[$e['id']] == 1)
});
// Print result
print_r($notUnique);
echo '<br>';
print_r($unique);
Try this
<?php
$array = array(
array('id' => 1,'name' => 'Timer'),
array('id' => 2,'name' => 'Tub'),
array('id' => 1,'name' => 'Paper'),
array('id' => 4,'name' => 'Puppy')
);
$new = array();
foreach($array as $r)$new[$r['id']][] = $r['name'];
echo '<pre>';print_r($new);
?>
Output
Array
(
[1] => Array
(
[0] => Timer
[1] => Paper
)
[2] => Array
(
[0] => Tub
)
[4] => Array
(
[0] => Puppy
)
)
Help me pls combine array values.
I have the same array:
In my case i should save customers name like a key.
Array
(
[Test Name] => Array
(
[0] => Array
(
[name] =>
banana
[id] =>
45002
[quantity] =>
10
)
[1] => Array
(
[name] =>
banana
[id] =>
45002
[quantity] =>
20
)
[3] => Array
(
[name] =>
apple
[id] =>
23402
[qua] =>
1
)
[5] => Array
(
[name] =>
cherry
[id] =>
40017
[qua] =>
7
)
How to get something like this:
Array
(
[Test Name] => Array
(
[0] => Array
(
[name] =>
banana
[id] =>
45002
[quantity] =>
30 // summ quantity but unique name and id
)
[1] => Array
(
[name] =>
apple
[id] =>
23402
[qua] =>
1
)
[2] => Array
(
[name] =>
cherry
[id] =>
40017
[qua] =>
7
)
In my case i should save customers name like a key.
Then i will upload this on a table.
You can use array_map and array_reduce to accomplish it:
$results = array_map(function ($result) {
return array_reduce($result, function ($carry, $item) {
if (isset($carry[$item['id']])) {
$carry[$item['id']]['quantity'] += $item['quantity'];
} else {
$carry[$item['id']] = $item;
}
return $carry;
}, array());
}, $results);
From the documentation:
array_map() returns an array containing all the elements of array1 after applying the callback function to each one.
array_reduce() applies iteratively the callback function to the elements of the array, so as to reduce the array to a single value.
In our case the callback given to the array_reduce function returns a final array which contains unique items by id.
I need this multidimensional array converted to a simple array.
Array
(
[0] => Array
(
[id_zub] => 1
[name] => Backen
)
[1] => Array
(
[id_zub] => 2
[name] => Kochen
)
)
A simple array:
array(
[id_zub] => 1
[name] => Backen
[id_zub] => 2
[name] => Kochen
)
function array_flattern($array){
foreach($array as $key=> $value){
if(is_array($value)){
$this->array_flattern($value);
}
else{
$this->result[$key] = $value;
}
}
}
The function gives me this result:
Array
(
[id_zub] => 2
[name] => Kochen
)
your function works as intended, Your getting a "key clash" and the latter key's value is the one used. You would have yo have a suffix on the key if you wanted it in one dimension
eg
Array ( [id_zub_2] => Kochen )