merge array in single array - php

I have used the following code to get array and merge them.
On the print_r(arr1) I get following array. In $arr I am trying to merge array but when i print_($arr) at the end of for-each I get same array.
Am I doing wrong array merge?
How can i combine or merge it?
foreach($q1->result_array() as $row4)
{
$arr1 = $q1->result_array();
echo"<pre>";
print_r($arr1);
echo"</pre>";
$arr = array_merge($arr, $arr1);
echo "<br/>";
$id = $row4['id'];
$parent_id = $row4['parent_id'];
if(!empty($arr1))
{
$this->showreply($id);
}
}
print_r($arr);
Array which i get on print_r($arr1):
Array
(
[0] => Array
(
[id] => 69
[reply] => First reply to Reply
[parent_id] => 68
[postid] => 0
[us_id] => 41
[added_by] => Shailesh
[photo] => 9.jpg
[added_on] => 2013-04-01 16:06:13
)
)
Array
(
[0] => Array
(
[id] => 70
[reply] => Reply to Nested Reply
[parent_id] => 69
[postid] => 0
[us_id] => 41
[added_by] => Shailesh
[photo] => 9.jpg
[added_on] => 2013-04-01 16:07:24
)
)
Array
(
[0] => Array
(
[id] => 52
[reply] => Reply on demand
[parent_id] => 70
[postid] => 0
[us_id] => 50
[added_by] => swapnil
[photo] =>
[added_on] => 2013-03-29 16:27:57
)
)

$arr = array_merge($arr, $arr1);
This $arr variable was never initialized, so nothing plus $arr1 equals to $arr1.
Also why is this code inside a foreach?

try this...i think this is what you are looking for
foreach($q1->result_array() as $row4)
{
$arr1 = $q1->result_array();
$arr[]=$row4;
}
print_r($arr)

Related

How to insert data on array multidimensional in PHP 7 [duplicate]

Following is the output of my multidimensional array $csmap_data
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
)
[flag] => 1
)
Initially there was no [flag] => 1 key-value in the array, I added it to the array $csmap_data.
But I want to add the [flag] => 1 in the above two array elements, not as a separate array element. In short I wanted following output :
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
[flag] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
[flag] => 1
)
)
The code I was trying to achieve this is as follows, but couldn't get the desired output:
if (!empty($csmap_data)) {
foreach($csmap_data as $csm) {
$chapter_csmap_details = $objClassSubjects->IsClassSubjectHasChapters($csm['cs_map_id']);
$csmap_data ['flag'] = 1;
}
}
Can anyone help me out in obtaining the desired output as I depicted? Thanks in advance.
<?
foreach($csmap_data as $key => $csm)
{
$csmap_data[$key]['flag'] = 1;
}
That should do the trick.
You can also do it using php array functions
$csmap_data = array_map(function($arr){
return $arr + ['flag' => 1];
}, $csmap_data);
UPDATE:
to use multiple variables in callback function of array_map function we can do it by use
$flagValue = 1;
$csmap_data = array_map(function($arr) use ($flagValue){
return $arr + ['flag' => $flagValue];
}, $csmap_data);

Collecting values from a multidimensional array using foreach loops

There is an array which is built looks like the following (with some more values which i left away for this example):
Array
(
[0] => Array
(
[id] => 44
[cars] => Array
(
[0] => Array
(
[id] => 38
)
[1] Array
(
[id] => 39
)
)
)
[1] => Array
(
[id] => 45
[cars] => Array
(
[0] => Array
(
[id] =>136
)
[1] =>Array
(
[id] =>137
)
[2] =>Array
(
[id] =>138
)
)
)
)
I want to build another array from the above in the following form:
Array
(
[0] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 38
)
[1] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 39
)
[2] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 136
)
[3] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 137
)
[4] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 138
)
)
I tried to achieve this with following function:
foreach($filterSortSaveArray as $filterSortSaveArray['cars'] => $value){
$id = $filterSortSaveArray['id'];
foreach($value['cars'] as $value => $car){
$field_values['car_filter_sort_id'] = $id;
$field_values['car_id'] = $car['id'];
}
}
But the the result differs from what I have expected. Any suggestions?
There are two big issues in your code. First, are you referencing undefined value with $filterSortSaveArray['cars'], since there is no 'cars' key in the first level of the original array. Second, by assigning values to $field_values['car_filter_sort_id'] and $field_values['car_id'] in the loop you are just overriding them in each iteration. You need to push the values into an array using []= operator (which is equivalent to applying array_push()).
Try this:
$result = [];
foreach($filterSortSaveArray as $k => $v) {
if (!is_array($v['cars']))
continue;
$id = $v['id'];
foreach ($v['cars'] as $i => $car){
$result[] = [
'car_filter_sort_id' => $id,
'car_id' => $car['id']
];
}
}

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
)

ksort doesn't seem to work

echo '<pre>'.print_r($listings,1).'</pre>';
ksort($listings, SORT_NUMERIC);
echo '<pre>'.print_r($listings,1).'</pre>';
Output:
Array
(
[quick-brown-fox] => Array
(
[0] => Quick-brown-fox
[1] => quick-brown-fox
[4] => general_thumbs/quick-brown-fox.jpg
[2] => 320
[3] => 240
)
)
Array
(
[quick-brown-fox] => Array
(
[0] => Quick-brown-fox
[1] => quick-brown-fox
[4] => general_thumbs/quick-brown-fox.jpg
[2] => 320
[3] => 240
)
)
I tried foreach, but it won't affect the original array, and for won't work because the its a key, not an index. What should I do in that case?
You have nested array in this $listings array. To sort it, write it like this:
foreach($listings as $k => $a){
ksort($a, SORT_NUMERIC);
$listings[$k] = $a;
}
array_walk(
$listings,
function(&$value) {
ksort($value, SORT_NUMERIC);
}
);

Merging two array elements into one array element in PHP

I want to merge two arrays into one array as follows,
Array1:
Array
(
[0] => Array
(
[id] => 3
[sku] => KOG456
[cart_id] => 2
[name] => Young Money
[slug] => young-money
[route_id] => 47
[description] =>
This is test song
[excerpt] =>
[saleprice] => 90.00
[related_products] =>
[images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
[seo_title] =>
[meta] =>
[enabled] => 1
)
)
Array2:
Array
(
[0] => Array
(
[filename] => Beethovens_Symphony_No._9_(Scherzo).wma
[title] => Young Money
[size] => 599.26
)
)
Expected array result is:
Array
(
[0] => Array
(
[id] => 3
[sku] => KOG456
[cart_id] => 2
[name] => Young Money
[slug] => young-money
[route_id] => 47
[description] =>
This is test song
[excerpt] =>
[saleprice] => 90.00
[related_products] =>
[images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
[seo_title] =>
[meta] =>
[enabled] => 1
[filename] => Beethovens_Symphony_No._9_(Scherzo).wma
[title] => Young Money
[size] => 599.26
)
)
How to merge these array elements into one array element ?
foreach ($origArray as $key => &$subArray)
$subArray += $arrayToBeAdded[$key];
Where $origArray is your array which is to be merged into and $arrayToBeAdded the array you merge into.
User array_merge_recursive():
$final = array_merge_recursive($array1, $array2);
Try this little known overload of the + operator for arrays:
$result = $array1[0] + $array2[0]
Use function array_merge($array1[0], $array2[0]) . Following is the example for the same
$array1 = array(0=>array('1'=>1,'2'=>2,'3'=>3));
$array2 = array(0=>array('4'=>4,'5'=>5,'6'=>6));
$result[0] = array_merge($array1[0],$array2[0]);
echo '<pre>';
print_r($result);
Since you have unique keys, you could use something as simple as the + operator (union)...
For example:
$arr1 = [1=>'testing',2=>'stack',3=>'overflow'];
$arr2 = [4=>'something',5=>'else',6=>'here'];
$arr3 = $arr1 + $arr2;
print_r($arr3);
Results:
Array ( [1] => testing [2] => stack [3] => overflow [4] => something [5] => else [6] => here )
For this php has multiple functions. You can use $arrays = array_combine($array1, $array2);.
PHP.net - array_combine
Hope it helped!

Categories