Create new array from two array; - php

I have this two arrays
$views[] = $id;
$pid[] = $page_id;
which prints
Array
(
[0] => 9
[1] => 12
[2] => 13
[3] => 14
[4] => 15
)
Array
(
[0] => 174
[1] => 221
[2] => 174
[3] => 174
[4] => 174
)
now i want to create new array from this result like(first will be the key and second be the value)
Array
(
[9] => 174
[12] => 221
[13] => 174
[14] => 174
[15] => 174
)
I have tired array_push function but didnt work for me.

You can use array_combine:
Creates an array by using one array for keys and another for its
values
ie:
$newarr = array_combine($array1, $array2); //$array1: key, $array2: value

$result = array();
for($i=0; $i<sizeof($array1); $i++)
$result[$array1[$i]] = $array2[i];

Related

How to merge mutiple array in a single array

As i searched alot but didn't get perfect solution of merging array in one single array.
These arrays are Dynamic (may be increase in future- would 50+). So for that we have to use count() or for loops to fetch and then merge.
Here's my code which i'm trying to resolve over core level. Please anyone tell me how may receive all values in Single array.
Array(
[0] => Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
)
[1] => Array
(
[0] => 93
[1] => 94
[2] => 95
[3] => 172
[4] => 30
)
[2] => Array
(
[0] => 109
[1] => 81
[2] => 79
[3] => 155 )
)`
My expectation of result is: (which i'm unable to get)
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
Use array_merge with splat operator,
$result = array_merge(...$arr);
array_merge — Merge one or more arrays
Splat operator - Since PHP 5.6, you can use splat operator (...) to create simpler variadic functions (functions that take an undefined number of arguments).
Demo
Output:-
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
using array_merge()
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
$newArr = array_merge($a[0], $a[1], $a[2]);
print_r($newArr);
assuming that your array will grow, you can use foreach like this :
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
foreach($a as $index){
foreach($index as $item){
array_push($newArr, $item);
}
}
As i used this technique. I got my answer in just a simple way. of array_flatten
print_r(array_flatten($overall_arr));
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){
$return = array_merge($return, array_flatten($value));
} else {
$return[$key] = $value;
}
}
return $return;
}

Build array by string name OR make multiple array by string values

I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it

What's the error in my foreach loop over multidimensional array?

Following is my multidimensional array named $prev_map_data:
Array
(
[0] => Array
(
[class_id] => 2
[class_name] => II
[class_checked] => 1
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 81
[subject_name] => 11 Engllish
[subject_checked] => 1
[teacher_cs_id] => 81
)
)
)
[1] => Array
(
[class_id] => 3
[class_name] => III
[class_checked] => 1
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 155
[subject_name] => Hidi
[subject_checked] => 1
[teacher_cs_id] => 155
)
[1] => Array
(
[cs_map_id] => 156
[subject_name] => 11 Maths
[subject_checked] => 1
[teacher_cs_id] => 156
)
[2] => Array
(
[cs_map_id] => 157
[subject_name] => 11 Science
[subject_checked] => 1
[teacher_cs_id] => 157
)
)
)
)
I want to get all the values from key [cs_map_id] one by one and push the values in an array $data. But getting the warning
Warning: Invalid argument supplied for foreach()
I'm not understanding what is wrong with my code. Can any one help me to improve my code and append the values to the new array? Thanks in advance. My code is as follows :
$prev_map_data = $objTeacherClassesSubjects->GetClassSubjectMappingsbyTeacherId ($request, $teacher_class_subjects_error_messages);
//print_d($prev_map_data);
$data = array();
foreach($prev_map_data as $map_id){
foreach($map_id as $ast){
foreach($ast as $tp){
//print_p($tp);
$tp['cs_map_id'];
array_push($data,$tp['cs_map_id']);
}
}
}
You are digging unconditionally into the array on items that are not themselves arrays. Do this instead:
foreach($prev_map_data as $map_id){
foreach($map_id['class_subjects'] as $subject){
$data[] = $subject['cs_map_id'];
}
}
Use this
$arr = array();
foreach($prev_map_data as $map_id){
$i = 0;
foreach($map_id['class_subjects'] as $val) {
$arr[$i] = $val['cs_map_id'];
$i++;
}
}

merge array in single array

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)

Find common values in multiple arrays with PHP

I need to find common values in multiple arrays. Number of arrays may be infinite.
Example (output from print_r)
Array1
(
[0] => 118
[1] => 802
[2] => 800
)
Array2
(
[0] => 765
[1] => 801
)
Array3
(
[0] => 765
[1] => 794
[2] => 793
[3] => 792
[4] => 791
[5] => 799
[6] => 801
[7] => 802
[8] => 800
)
now, I need to find the values that are common on all 3 (or more if available) of them.... how do I do that?
Thanx
array_intersect()
$intersect = array_intersect($array1,$array2,$array3);
If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);

Categories