after json decode array merge
$permission_roles1 = DB::table('permission_roles')->select('permission_name')->first();
$permission_roles2 = DB::table('user_permissions')->select('userP_name')->first();
$json_array1 = json_decode($permission_roles1->permission_name);
$json_array2 = json_decode($permission_roles2->userP_name);
$arr1 = array($json_array1);
$arr2= array($json_array2);
$res = array_merge($arr1, $arr2);
dd($res);
In your code,
$json_array1 = json_decode($permission_roles1->permission_name);
$json_array2 = json_decode($permission_roles2->userP_name);
returns object not an array.
Just changed it to
$json_array1 = json_decode($permission_roles1->permission_name, true);
$json_array2 = json_decode($permission_roles2->userP_name, true);
convert array object into array then merge the array
$permission_roles1 = DB::table('permission_roles')->select('permission_name')->first();
$permission_roles2 = DB::table('user_permissions')->select('userP_name')->first();
$arr1 = (array) $permission_roles1->permission_name;
$arr2 = (array) $permission_roles2->userP_name;
$res = array_merge($arr1, $arr2);
dd($res);
Related
I use laravel. And then when from submitted, it pass value as array.
My question is, how i can split the value? I want number before '-' and after '-'.
I want just like below:
and
My code just like below
$form = $request->all();
$emelto = $form['emelto'];
$split = explode('-', $emelto );
but it shows error
explode() expects parameter 2 to be string, array given
Please someone can help me.
Explode each individual string as you tried.
Insert each number in it's respective column index in result.
Snippet:
<?php
$emelto = [
'4-2',
'11-5'
];
$data = [];
foreach($emelto as $str){
foreach(explode('-',$str) as $index => $s){
$data[$index] = $data[$index] ?? [];// assuming your PHP version supports ??
$data[$index][] = $s;
}
}
print_r($data);
You can use
$array1 = array();
$array2 = array();
foreach($request->emelto as $val){
$dataArray = explode('-', $val);
$array1[] = $dataArray[0];
$array2[] = $dataArray[1];
}
$emelto is an array and you are passing it to explode. You have to pass it like $emelto[0].
You should update your code with the below one.
<?php
$form = $request->all();
$emelto = $form['emelto'];
$array1 = explode('-', $emelto[0] );
$array2 = explode('-', $emelto[1] );
$array3 = array();
array_push($array3, $array1[0]);
array_push($array3, $array2[0]);
$array4 = array();
array_push($array4, $array1[1]);
array_push($array4, $array2[2]);
?>
I have this
{"1":{"name":"cat"},"2":{"name":"elephant"}}
how to convert it to
[{"name":"dog"},{"name":"cat"}]
or convert it to vice versa
$arr = json_decode('{"1":{"name":"cat"},"2":{"name":"elephant"}}');
$result = array();
foreach($arr as $item) {
$data = ['name' => $item->name];
$result[] = $data;
}
$json = json_encode($result);
print_r($json);
I have an array that stores some values. I'm trying to detect the similar values and add them to new array.
example:
$arrayA = array( 1,4,5,6,4,2,1);
$newarray = (4,1);
Any help?
Use the array_intersect() method. For example
$arrayA = array(1,4,5,6,4,2,1);
$arrayB = array(4,1);
$common_values = array_intersect($arrayA, $arrayB);
try this:
$array = array(1,4,5,6,4,2,1);
$duplicates = array_unique(array_diff_assoc($array, array_unique($array)));
$a1 = array( 1,4,5,6,4,2,1);
$a = array();
foreach($a1 as $value){
if(!in_array($value, $a)){
$a[] = $value;
}
}
$arrayA = array(1,4,5,6,4,2,1);
$newarray = array_diff_assoc($arrayA, array_unique($arrayA));
I have a site developed in php (codeigniter) and I want to merge some array with same structure.
This is the constructor of my array:
$first = array();
$first['hotel'] = array();
$first['room'] = array();
$first['amenities'] = array();
/*
Insert data into $first array
*/
$second = array();
$second['hotel'] = array();
$second['room'] = array();
$second['amenities'] = array();
/*
Insert data into $second array
*/
After insert data I want to merge this array but the problem is that I have subarray inside it and I want to create a unique array like that:
$total = array();
$total['hotel'] = array();
$total['room'] = array();
$total['amenities'] = array();
This is the try to merge:
$total = array_merge((array)$first, (array)$second);
In this array I have only the $second array why?
Use the recursive version of array_merge called array_merge_recursive.
It seems like array_merge doesn't do what you think it does: "If the input arrays have the same string keys, then the later value for that key will overwrite the previous one." Try this:
function merge_subarrays ($first, $second)
$result = array();
foreach (array_keys($first) as $key) {
$result[$key] = array_merge($first[$key], $second[$key]);
};
return $result;
};
Then call it as:
$total = merge_subarrays($first, $second);
and, if I've correctly understood your question, $total will contain the result you're looking for.
There is no standard way of doing it, you just have to do something like:
<?php
$first = array();
$first['hotel'] = array('hello');
$first['room'] = array();
$first['amenities'] = array();
/*
Insert data into $first array
*/
$second = array();
$second['hotel'] = array('world');
$second['room'] = array();
$second['amenities'] = array();
$merged = array();
foreach( $first as $key => $value )
{
$merged[$key] = array_merge( $value, $second[$key] );
}
print_r( $merged );
$arr = array();
$arr[0] = "2a123";
$arr[1] = "2123";
$arr["other_option"] = "2123";
var_dump($arr);
$arr = json_encode($arr);
$arr = (array)json_decode($arr);
var_dump($arr);
var_dump( $arr[1]);
var_dump( $arr["1"]);
The output of 2 last var_dump are NULL NULL, if we remove the 4th line $arr["other_option"] = "2123"; it'll ouput correctly, but I don't understand why!
instead of type casting to array , set true in json_encode
When TRUE, returned objects will be converted into associative arrays.
$arr = array();
$arr[0] = "2a123";
$arr[1] = "2123";
$arr["other_option"] = "2123";
$arr = json_encode($arr);
$arr = json_decode($arr,true);
var_dump( $arr['other_option']); // return 2123
working DEMO