The 3D assoc. array looks like below.
Array
(
[COL] => Array
(
[0] => Array
(
[emp_num] => 1000001
[user_name] => Test User
[amount] => 775.00
[name] => COL
)
[1] => Array
(
[emp_num] => 26
[user_name] => John Doe
[amount] => 555.00
[name] => COL
)
)
[RA. 20%] => Array
(
[0] => Array
(
[emp_num] => 1000001
[user_name] => Test User
[amount] => 110.00
[name] => RA. 20%
)
)
[BS] => Array
(
[0] => Array
(
[emp_num] => 1000001
[user_name] => Test User
[amount] => 444.00
[name] => BS
)
)
)
I want to remove the the last key=>value pair of each inner most array. (want to remove the key value pair that has [name] for the key)
The result should look like the array below.
Array
(
[COL] => Array
(
[0] => Array
(
[emp_num] => 1000001
[user_name] => Test User
[amount] => 775.00
)
[1] => Array
(
[emp_num] => 26
[user_name] => John Doe
[amount] => 555.00
)
)
[RA. 20%] => Array
(
[0] => Array
(
[emp_num] => 1000001
[user_name] => Test User
[amount] => 110.00
)
)
[BS] => Array
(
[0] => Array
(
[emp_num] => 1000001
[user_name] => Test User
[amount] => 444.00
)
)
)
I wrote a function to do this.
<!-- language: php -->
function remove_name_from_psa($psa_array){
foreach( $psa_array as $key=>$value ) {
foreach( $value as $key2=>$value2 ){
foreach( $value2 as $key3=>$value3 ){
if( $key3 != 'name') {
$psa_name_removed[$key][$value[$key2][$value2[$key3]]] = $value3;
}
}
}
}
return $psa_name_removed;
}
The returned array is this, which is obviously not what I need.
Array ( [COST OF LIVING] => Array
( [] => 555.00 )
[RENT ALLOW. 20%] => Array
( [] => 110.00 )
[BASIC SALARY] => Array
( [] => 444.00 )
)
And there are lots of undefined offset and undefined index notices.
$psa_name_removed[$key][$value[$key2][$value2[$key3]]] = $value3; //is this the line I am doing the mistake? Or is the whole method a mistake? :-P
How can I get this to work? Can anyone help?
Thank You!
function remove_name_from_psa($psa_array){
foreach( $psa_array as $key => $value ) {
foreach( $value as $key2 => $value2 ){
unset( $psa_array[$key][$key2]['name'] );
}
}
return $psa_array;
}
Wee, functional solution!
$array = array_map(function ($i) {
return array_map(function ($j) {
return array_diff_key($j, array_flip(array('name')));
}, $i);
}, $array);
More traditional solution:
foreach ($array as &$i) {
foreach ($i as &$j) {
unset($j['name']);
}
}
Note the & in as &$i. Use this reference to modify the item.
foreach($array as &$foo){
foreach($foo as &$bar){
unset($bar['name']);
}
}
To truly unset the last element in a 3D array your would do this:
$data = array(
array(
array(1, 2, 3),
),
);
foreach ($data as $i1 => $j1) {
foreach ($j1 as $i2 => $j2) {
end($j2);
unset($data[$i1][$i2][key($j2)]);
}
}
var_dump($data);
See it in action here:
http://codepad.viper-7.com/CbgnVf
function remove_name_from_psa( $psa_array ){
foreach( $psa_array as $key => $value ) {
foreach( $value as $key2 => $value2 ) {
array_pop( $psa_array[$key][$key2] );
}
}
return $psa_array;
}
Related
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Array
(
[0] => Array
(
[hotel_id] => 79
[logo] => 1463466926-97157549.jpg
)
[1] => Array
(
[hotel_id] => 78
[logo] => 1463466942-15603675.jpg
)
[2] => Array
(
[hotel_id] => 77
[logo] => 1463466953-25200244.jpg
)
[3] => Array
(
[hotel_id] => 76
[logo] => 1463466967-62926110.jpg
)
[4] => Array
(
[hotel_id] => 75
[logo] =>
)
[5] => Array
(
[hotel_id] => 74
[logo] =>
)
)
Its my array values.
Here I send some values such as hotel_id & logo of that hotel..
But I should need to send the logo as image URL
i.e:,for this:1463466926-97157549.jpg
I need to send as
http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg
By adding the path of that image..
And finally my array should be like this..
Array
(
[0] => Array
(
[hotel_id] => 79
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg
)
[1] => Array
(
[hotel_id] => 78
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466942-15603675.jpg
)
[2] => Array
(
[hotel_id] => 77
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466953-25200244.jpg
)
[3] => Array
(
[hotel_id] => 76
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466967-62926110.jpg
)
[4] => Array
(
[hotel_id] => 75
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
)
[5] => Array
(
[hotel_id] => 74
[logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
)
)
Here, the last two hotels having null images.
For that I send an default image URL such as http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
like this..
Someone could help me please..
Thanks in advance...
I have tried like this
$im =array();
foreach ($Roo as $key => $value)
{
$im[]=(\URL::to('').'/'.$value['logo']);
}
Here, \URL::to('').'/' is my path AND $Roo is my array
But,Instead it retrieve only the logo in separate array.
loop through your array and update your array as follows :
foreach ($array as $key => &$value) {
if ($value['logo'] != '') {
$value['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/'.$value['logo'];
} else {
$value['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg';
}
}
print_r($array);
foreach($hotel_details as $key => $detail) {
if(!empty($detail['logo']) {
$detail['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/'.$detail['logo']
$hotel_destails[$key] = $detail
} else {
$detail['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg'
$hotel_destails[$key] = $detail
}
}
Say your array name is $arr, so start traversing the whole array and replace your logo with new one if hotel_id is match.
Using the & you mean that the array is reference so what you change here must update in the main array, this is optional.
$path = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/';
$hotel_id = 79;
foreach($arr as $key => &$value){
if($value['hotel_id'] == $hotel_id){
$value['logo'] = $path.$value['logo'];
break;
}
}
print_r($arr);
Note: If you want to change the whole array then it will be very easy,
just remove the if condition.
There you should check about array_map() function.
With this entry array :
Array
(
[0] => Array
(
[hotel_id] => 0
[logo] => image_0.jpg
)
[1] => Array
(
[hotel_id] => 1
[logo] => image_1.jpg
)
[2] => Array
(
[hotel_id] => 2
[logo] => image_2.jpg
)
[3] => Array
(
[hotel_id] => 3
[logo] =>
)
[4] => Array
(
[hotel_id] => 4
[logo] => image_4.jpg
)
)
You can get this output array :
Array
(
[0] => Array
(
[hotel_id] => 0
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_0.jpg
)
[1] => Array
(
[hotel_id] => 1
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_1.jpg
)
[2] => Array
(
[hotel_id] => 2
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_2.jpg
)
[3] => Array
(
[hotel_id] => 3
[logo] => http://some_addr.tld/some_dir/some_sub_dir/300x300.jpg
)
[4] => Array
(
[hotel_id] => 4
[logo] => http://some_addr.tld/some_dir/some_sub_dir/image_4.jpg
)
)
By simply doing this :
$base = 'http://some_addr.tld/some_dir/some_sub_dir/';
$default = '300x300.jpg';
$res = array_map(
function($x) use ($base, $default)
{
if ( array_key_exists( 'logo', $x ) )
{
if ( !empty( $x['logo'] ) )
{
$x['logo'] = $base . $x['logo'];
}
else
{
$x['logo'] = $base . $default;
}
}
return $x;
}
, $arr);
Where $arr is the array containing your hotels, and $res will be the modified array.
you can do achieve this by following code:
$url = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/';
foreach ($arr as &$value) {
if ($value['logo'] != '') {
$value['logo'] = $url.$value['logo'];
} else {
$value['logo'] = $url;
}
}
I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)
how can i count an element if it appears more than once in the same array?
I already tried with array_count_values, but it did not work, is it beacuse i got more than one key and value in my array?
This is my output from my array (restlist)
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla)
[1] => Array ( [restaurant_id] => 32144 [title] => test5)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
[3] => Array ( [restaurant_id] => 32144 [title] => test5)
[4] => Array ( [restaurant_id] => 42154 [title] => blabla2 )
)
I want it to count how many times the same element appears in my array and then add the counted value to my newly created 'key' called hits in the same array.
Array (
[0] => Array ( [restaurant_id] => 47523 [title] => cafe blabla [hits] => 1)
[1] => Array ( [restaurant_id] => 32144 [title] => test5 [hits] => 2)
[2] => Array ( [restaurant_id] => 42154 [title] => blabla2 [hits] => 2)
)
This is how i tried to do what i wanted.
foreach ($cooltransactions as $key)
{
$tempArrayOverRestaurants[]= $key['restaurant_id'];
}
$wordsRestaruants = array_count_values($tempArrayOverRestaurants);
arsort($wordsRestaruants);
foreach ($wordsRestaruants as $key1 => $value1)
{
$temprestaurantswithhits[] = array(
'restaurant_id' => $key1,
'hits' => $value1);
}
foreach ($restlistas $key)
{
foreach ($temprestaurantswithhits as $key1)
{
if($key['restaurant_id'] === $key1['restaurant_id'])
{
$nyspisestedsliste[] = array(
'restaurant_id' => $key['restaurant_id'],
'title' => $key['title'],
'hits' => $key1['hits']);
}
}
}
I know this is probably a noob way to do what i want but i am still new at php..I hope you can help
Just try with associative array:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $item ) {
$id = $item['restaurant_id'];
if ( !isset($output[$id]) ) {
$output[$id] = $item;
$output[$id]['hits'] = 1;
} else {
$output[$id]['hits']++;
}
}
And if you want to reset keys, do:
$outputWithoutKeys = array_values($output);
I have thee following $array:
Array
(
[0] => Array
(
[cd] => 1675
[amt_1] => 199.50
[fname] => Joe
[lname] => A
)
[1] => Array
(
[cd] => 1675
[amt_1] => 69.90
[fname] => Joe
[lname] => A
)
[2] => Array
(
[cd] => 1676
[amt_1] => 69.90
[fname] => Tracy
[lname] => A
)
[3] => Array
(
[cd] => 1676
[amt_1] => 199.50
[fname] => Tracy
[lname] => A
)
...
)
I am trying to do is to group them together, in this case, by fname or cd so that i will have something like:
[0] => Array
(
[cd] => 1676
Array
(
[0] => Array
(
[amt_1] => 199.50
)
[1] => Array
(
[amt_1] => 69.90
)
[fname] => Joe
[lname] => A
)
[1] => Array
(
[cd] => 1676
Array
(
[0] => Array
(
[amt_1] => 199.50
)
[1] => Array
(
[amt_1] => 69.90
)
[fname] => Tracy
[lname] => A
)
........
I can't seem to figure it out.
This cannot be done in mysql, I need to do it in php.
Any ideas?
Thanks
edit: I know that the result example is not formatted correct, but basically I want to combine the fname and the rest of results place them in arrays.
edit:
#Paulo H has a good idea. also i found another way of doing it that groups it together not combining it :
$groups = array ();
foreach ( $the_array as $item ) {
$key = $item ['fname'];
if (! isset ( $groups [$key] )) {
$groups [$key] = array ('items' => array ($item ), 'count' => 1 );
} else {
$groups [$key] ['items'] [] = $item;
$groups [$key] ['count'] += 1;
}
}
Try this:
function &array_group_value_by($input_array,$value,$by){
$result = array();
foreach($input_array as $array){
if(!isset($result[$array[$by]])){
$result[$array[$by]] = array();
}
foreach($array as $key=>$data){
if((is_string($value) && $key==$value) || (is_array($value) && in_array($key,$value))){
if(!isset($result[$array[$by]][$key])){
$result[$array[$by]][$key] = array();
}
$result[$array[$by]][$key][] = $data;
}else{
$result[$array[$by]][$key] = $data;
}
}
}
return $result;
}
$grouped = array_group_value_by($yourarray,'amt_1','fname');
print_r($grouped);