How to replace the nested array value in php - php

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;
}
}

Related

Get value from array using key

I have an array like this:
Array
(
[0] => Array
(
[settingID] => 1
[name] => audioCueDistance
[setValue] => false
)
[1] => Array
(
[settingID] => 2
[name] => audioCueDistanceToGo
[setValue] => true
)
[2] => Array
(
[settingID] => 3
[name] => audioCues
[setValue] => true
)
[3] => Array
(
[settingID] => 4
[name] => audioCueStyle
[setValue] => default
)
[4] => Array
(
[settingID] => 5
[name] => audioCueTime
[setValue] => true
)
[5] => Array
(
[settingID] => 6
[name] => isMetric
[setValue] => true
)
How can I get individual values from key for example, I would like to output the setValue of isMetric.
Thanks
foreach ($foo as $bar) {
if ($bar['name'] == "isMetric") {
// Use setValue here
}
}
From what I understand you want to do something like $myArray['isMetric']['setValue'].
As your array is not in that form you need to map it that way.
$myArray = array(
array(
'settingID'=>6,
'name'=>'isMetric',
'value'=>true
)
);
$myAssocArray = array_reduce($myArray, function($carry, $item){
$carry[$item['name']] = $item;
return $carry;
}, array());
echo $myAssocArray['isMetric']['setValue'];
Run this code here: https://repl.it/CZ3R
foreach($array as $element)
{
if($element['name'] = 'isMetric')
return $element['setValue'];
}
throw new \Exception('isMetric Not Found.');

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)

PHP Array re-arrange to Multi Dimensional

I have an array structure like this and wanted to Re-arrange it to the one below. Any suggestions for a faster/simple fix? I already did the addition of the dates. Thanks! :)
Input:
Array
(
[0] => Array
(
[user_id] => 255
[display_name] => Mark
[company_name] => Company_A
)
[1] => Array
(
[user_id] => 150
[display_name] => Paul
[company_name] => Company_A
)
[2] => Array
(
[user_id] => 25
[display_name] => Hulk
[company_name] => Company_B
)
[3] => Array
(
[user_id] => 50
[display_name] => Bob
[company_name] => Company_B
)
)
Output:
Array
(
[Company_A] => Array
(
[company_total_hours] => 20h 45m
[employees] => Array
(
[0] => Array
(
[user_id] => 255
[display_name] => Mark
)
[1] => Array
(
[user_id] => 150
[display_name] => Paul
)
)
)
[Company_B] => Array
(
[company_total_hours] => 7h 30m
[employees] => Array
(
[0] => Array
(
[user_id] => 25
[display_name] => Hulk
)
[1] => Array
(
[user_id] => 50
[display_name] => Bob
)
)
)
)
My Attempts:
<?php
$company_names = array();
foreach ($records as $k => $v) {
$company_names[] = $v->company_name;
}
$company_names = array_unique($company_names);
// hard coded testing
if (count($company_names) > 0) {
foreach($company_names as $k2 => $v2) {
$final_array[$v2]['company_total_hours'] = rand(1, 20);
$final_array[$v2]['employees'] = array(
array('user_id' => '255', 'display_name' => 'Mark'),
array('user_id' => '150', 'display_name' => 'Paul')
);
}
}
// on-going testing right now here....
I don't see where you derive your hours from so I left that out.
$i = 0;
foreach($vals as $keys => $arrays) {
if(!isset($new[$arrays['company_name']]))
$i = 0;
$new[$arrays['company_name']]['employees'][$i]['display_name'] = $arrays['display_name'];
$new[$arrays['company_name']]['employees'][$i]['user_id'] = $arrays['user_id'];
$i++;
}
Gives you:
Array
(
[Company_A] => Array
(
[employees] => Array
(
[0] => Array
(
[display_name] => Mark
[user_id] => 255
)
[1] => Array
(
[display_name] => Paul
[user_id] => 150
)
)
)
[Company_B] => Array
(
[employees] => Array
(
[0] => Array
(
[display_name] => Hulk
[user_id] => 25
)
[1] => Array
(
[display_name] => Bob
[user_id] => 50
)
)
)
)
foreach($arr as $v)
{
if(!$arr2[$v['company_name']]['employees'])
$arr2[$v['company_name']]['employees'] = array();
if(!$arr2[$v['company_name']]['company_total_hours'])
$arr2[$v['company_name']]['company_total_hours'] = '2h';//addional value
$arr2[$v['company_name']]['employees'][] = array('user_id'=>$v['user_id'],
'display_name'=>$v['display_name']
);
}
I have created a function which does the same thing as the answer given by #Rasclatt.
function groupByKeyValue($array, $oldKeyName, $newKeyName){
$newArray = array();
foreach($array as $key=>$value){
if(isset($newArray[$value[$oldKeyName]][$newKeyName])){
$newArray[$value[$oldKeyName]][$newKeyName][] = array('user_id'=> $value['user_id'], 'display_name' => $value['display_name']);
}else{
$newArray[$value[$oldKeyName]] = array($newKeyName => array(array('user_id'=> $value['user_id'], 'display_name' => $value['display_name'])));
}
}
return $newArray;
}
//usage
$newArray = groupByKeyValue($array, 'company_name', 'employees');
You can add a third parameter to send the keys for the array values which needs to be used in the new array for 'employees'. Please check this link for the working of the function http://goo.gl/I6Of5y

php add some value on specific location in multidimensional array

I have array like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
i want to locate user_id 22 and put this value "GO_FUEL_SGD_W" on brand, what should i do, so the view of array will look like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W => [1] =>GO_FUEL_SGD_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
Just use loop:
foreach($array as &$item)
{
if(array_key_exists('user_id', $item) &&
$item['user_id']==22 &&
array_key_exists('brand', $item) &&
!in_array('GO_FUEL_SGD_W', $item['brand']))
{
$item['brand'][] = 'GO_FUEL_SGD_W';
}
}
A simple foreach loop will do the job:
foreach($myarray AS &$subarray) {
if($subarray['user_id'] == 22) {
$subarray['brand'][] = "GO_FUEL_SGD_W";
break;
}
}
Working example: http://3v4l.org/8aQMj
You will need to iterate over the array and look for the element you're searching for.
foreach ($array as &$element) {
if ($element['user_id'] != 22)
continue;
$element['brand'][] = "GO_FUEL_SGD_W";
break;
}
With continue; all elements will be skipped, who have $element['user_id'] != 22 (and so none of the code after the continue; will be applied to them!).
Also it will end the loop once the requested element is reached and modified, thanks to break;.
$array= //your array;
foreach($array as $x){
if($x['user_id']=='22'){
$x['brand'][]='GO_FUEL_SGD_W';
break;
}
}

Removing a key=>value pair from a 3D associative array - PHP

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;
}

Categories