Array in array with foreach - php

I have a new PHP problem. I have 2 arrays, and I want a third array which is a combination of the first 2. The first array, $arr1, is something like this:
Array (
[0] => name [1] => age
)
The second array, $arr2, is something like this:
Array (
[0] => Array( [0] => Dave [1] => 20 )
[1] => Array( [0] => Steve [1] => 25 )
[2] => Array( [0] => Ace [1] => 23 )
)
And my idea is to create a new array, called $arr3, which should like this:
Array (
[0] => Array( [name] => Dave [age] => 20 )
[1] => Array( [name] => Steve [age] => 25 )
[2] => Array( [name] => Ace [age] => 23 )
)
Can anyone tell me how to do this?

$arr3 = array();
foreach ($arr2 as $person) {
$arr3[] = array_combine($arr1, $person);
}

foreach($arr2 as $subArray){
foreach($subArray as $i=>$val){
$arr3[$arr1[$i]] = $val;
}
}

Related

combine 2 array with same key into 1 array

I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)

Change the Array Format

I have a page where it prints out this value:
"Firstname","Ana","George","Wilson"
"Lastname","Smith","Spencer","Carey"
"Age","18","20","22"
I get the values of these using file_get_contents and str_getcsv.
$array= str_getcsv($test);
The Array results that I get is this
Array ( [0] =>
"Firstname"
[1] => 'Ana'
[2] => 'George'
[3] => 'Wilson'
"Lastname"
[4] => 'Smith'
[5] => 'Spencer'
[6] => 'Carey'
"Age"
[7] => 18
[8] => 20
[9] => 22
))
Is there anyway I can change the Array format into this?
Array
(
[0] => Array
(
[0] => 'Ana'
[1] => 'George'
[2] => 'Wilson'
)
[1] => Array
(
[0] => 'Smith'
[1] => 'Spencer'
[2] => 'Carey'
)
[2] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
Instead of using file_get_contents() and str_getcsv(), i will recomend you to use file()
Do like below:-
<?php
$test = file('test.txt'); // you can add your file url here
echo "<pre/>";print_r($test);// initial array
foreach($test as &$te){
$new_array = explode(',',$te);
unset($new_array[0]);
$te = array_values($new_array);
}
echo "<pre/>";print_r($test); // modified and desired array
To change the given array to a specified format:
$array = ['Firstname' => ['Ana', 'George', 'Wilson'], 'Lastname' => ['Smith', 'Spencer', 'Carey'], 'Age' => [18, 20, 22]];
$new_array = [];
foreach ($array as $key => $value) {
$new_array[] = $value;
}
print_r($new_array);
Array
(
[Firstname] => Array
(
[0] => Ana
[1] => George
[2] => Wilson
)
[Lastname] => Array
(
[0] => Smith
[1] => Spencer
[2] => Carey
)
[Age] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
Try This bro i hope i can help:
$array = array(0=> array(
'firstname' => array(1 => 'Ana', 2 => 'George', 3=>'Wilson'),
'lastname' => array(4=>'Smith', 5=>'Spencer', 6=> 'Carey'),
'age' => array(7=>18,8=>20,9=>22)
));
$newArray = array();
echo "<pre/>";print_r($array);
foreach($array as $key=>$new){
foreach($new as $k=>$d){
$newArray[] = array_values($d);
}
}
echo "</br>New Array Format: </br>";
echo "<pre/>";print_r($newArray);

How to remove the parent numeric keys of php array

I've an array in php something like below
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).
Thanks.
One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():
$new_arr = array_unique(
call_user_func_array('array_merge', $old_arr));
Demo. Obviously, it'll work with array of any length.
$startArray = Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
);
//Edited to handle more the 2 subarrays
$finalArray = array();
foreach($startArray as $tmpArray){
$finalArray = array_merge($finalArray, $tmpArray);
}
$finalArray = array_unique($finalArray);
Using RecursiveArrayIterator Class
$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
$new_arr[]=$v;
}
print_r(array_unique($new_arr));
Demo
OUTPUT:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);
array_merge is merging the array's, array_unique is removinge any duplicate values.
Try something like this:
$new_array = array();
foreach($big_array as $sub_array) {
array_merge($new_array, $sub_array);
}
$new_array = array_unique($new_array);
(code not tested, this just a concept)
Try this:
$Arr = array(array(40173, 514081, 363885, 891382),
array(40173,5181, 385,891382));
$newArr = array();
foreach($Arr as $val1)
{
foreach($val1 as $val2)
{
array_push($newArr, $val2);
}
}
echo '<pre>';
print_r(array_unique($newArr));
Output:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
Refer: https://eval.in/124240
--
Thanks

ReCreate Readable Array

I have an array look like this via CSV:
Array
(
[0] => Array
(
[0] => Agent Name
[1] => Total Calls
[2] => Customer Services
[3] => Voicemail
)
[1] => Array
(
[0] => Paul
[1] => 53
[2] => 0
[3] => 0
)
[2] => Array
(
[0] => Sarah
[1] => 51
[2] => 0
[3] => 0
)
)
I would like to tidy an array to make it to more readable and easy to use.
I want an array to look something like this:
Array
(
[Paul] => Array
(
[Agent Name] => Paul
[Total Calls] => 53
[Customer Services ] => 30
[Voicemail ] => 0
)
[Sarah] => Array
(
[Agent Name] => Sarah
[Total Calls] => 51
[Customer Services ] => 0
[Voicemail ] => 0
)
)
I have came up with this solution and it work:
$fields = $report[0];
array_shift($report);
$data = array();
foreach($report as $row) {
$data[$row[0]] = array();
foreach($row as $k => $val) {
$data[$row[0]][$fields[$k]] = $val;
}
}
return $data;
Is there a better shorter way doing this?
Try
$result =array();
for($i=1;$i<count($arr);$i++){
$result[$arr[$i][0]] = array_combine(array_values($arr[0]),$arr[$i]);
}
See demo here
You could perhaps do it like this:
$f = array_shift($report);
$a = array_combine(array_column($t = array_map(function ($r) use ($f)
{
return array_combine($f, $r);
}
, $report), 'Agent Name'), $t);

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

Categories