how to use array_map/array_walk function for multidimensional array - php

I have the following multidimensional array:
array (size=2)
10 =>
array (size=7)
1 =>
array (size=4)
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)
2 =>
array (size=4)
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
3 =>
array (size=4)
0 => string '10view3' (length=7)
1 => string '10ins3' (length=6)
2 => string '10edit3' (length=7)
3 => string '10del3' (length=6)
11 =>
array (size=7)
1 =>
array (size=4)
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
2 =>
array (size=4)
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
3 =>
array (size=4)
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
I want to convert it to the array like:
1 => array(10 => array(
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
),
2 => array(
10 => array(
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
),
11 => array(
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
)
),
3 => array(
10 => array(
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
)
please help me i confuse to solve this problem thanks in advance.
It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!

Using array map you can achieve like this
<?php
function merge_array($n, $m)
{
$data = array();
$data[10] = $n;
$data[11] = $m;
return $data;
}
$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);
Using for loop it is very simple you can achieve like this
$count_10 = count($array[10]);
$count_11 = count($array[11]);
$count = ($count_10>$count_11)?$count_10:$count_11;
$data = array();
for($i=0; $i<$count; $i++)
{
if(isset($array[10][$i]))
{
$data[$i][] = $array[10][$i];
}
if(isset($array[11][$i]))
{
$data[$i][] = $array[11][$i];
}
}

Related

Split 1 day array into 2 days array

I have an array like this and I want to split this into 2 arrays with the same date.
array (size=2)
'dates' => string '2021-10-20' (length=10)
'hours' =>
array (size=2)
0 =>
array (size=12)
0 => string '07:30' (length=5)
1 => string '08:00' (length=5)
2 => string '08:30' (length=5)
3 => string '09:00' (length=5)
4 => string '09:30' (length=5)
5 => string '10:00' (length=5)
6 => string '10:30' (length=5)
7 => string '11:00' (length=5)
8 => string '11:30' (length=5)
9 => string '12:00' (length=5)
10 => string '12:30' (length=5)
11 => string '13:00' (length=5)
1 =>
array (size=4)
0 => string '15:30' (length=5)
1 => string '16:00' (length=5)
2 => string '16:30' (length=5)
3 => string '17:00' (length=5)
I want to make 2 arrays with the same date, and it looks like this:
0 =>
'dates' => string '2021-10-20'
'hours' =>
array (size=1)
0 =>
array (size=12)
0 => string '07:30' (length=5)
1 => string '08:00' (length=5)
2 => string '08:30' (length=5)
3 => string '09:00' (length=5)
4 => string '09:30' (length=5)
5 => string '10:00' (length=5)
6 => string '10:30' (length=5)
7 => string '11:00' (length=5)
8 => string '11:30' (length=5)
9 => string '12:00' (length=5)
10 => string '12:30' (length=5)
11 => string '13:00' (length=5)
1 =>
'dates' => string '2021-10-20'
'hours' =>
array (size=1)
0 =>
array (size=4)
0 => string '15:30' (length=5)
1 => string '16:00' (length=5)
2 => string '16:30' (length=5)
3 => string '17:00' (length=5)
How to make this happen? and my code looks like this,
if ($cekWaktuDosen4->num_rows == 0) {
$timesDosen4[$i]['dates'] = $dates;
$timesDosen4[$i]['hours'] = Array
(
(0) => Array
(
0 => '07:30',
1 => '08:00',
2 => '08:30',
3 => '09:00',
4 => '09:30',
5 => '10:00',
6 => '10:30',
7 => '11:00',
8 => '11:30',
9 => '12:00',
10 => '12:30',
11 => '13:00',
12 => '13:30',
13 => '14:00',
14 => '14:30',
15 => '15:00',
16 => '15:30',
17 => '16:00',
18 => '16:30',
19 => '17:00'
)
);
}else {
$y = 0;
while ($k = $cekWaktuDosen4->fetch_object()) {
$tglMulaiDosen4 = $carbon->create($carbon->parse($k->tanggal_mulai)->format('Y-m-d'));
$tglSelesaiDosen4 = $carbon->create($carbon->parse($k->tanggal_selesai)->format('Y-m-d'));
$tglBantuMulai4 = $tglMulaiDosen4->addHours(7)->addMinutes(30);
$tglBantuSelesai4 = $tglSelesaiDosen4->addHours(17);
if($tglBantuMulai4 < $k->tanggal_mulai) {
$timesDosen4[$i]['dates'] = $tglMulaiDosen4->format('Y-m-d');
$timesDosen4[$i]['hours'][$y] = create_time_range($tglBantuMulai4->format('H:i'), rounddown_timestamp($carbon->parse($k->tanggal_mulai)->format('H:i')));
$y++;
if($k->tanggal_selesai < $tglBantuSelesai4) {
$timesDosen4[$i]['hours'][$y] = create_time_range(roundup_timestamp($carbon->parse($k->tanggal_selesai)->format('H:i')), $tglBantuSelesai4->format('H:i'));
}
}else if($tglBantuMulai4 == $k->tanggal_mulai && $tglBantuSelesai4 != $k->tanggal_selesai) {
$timesDosen4[$i]['dates'] = $tglMulaiDosen4->format('Y-m-d');
$timesDosen4[$i]['hours'][$y] = create_time_range(roundup_timestamp($carbon->parse($k->tanggal_selesai)->format('H:i')), $tglBantuSelesai4->format('H:i'));
}
}
}
i tried to add variable for dates but its not works. And sometimes just showing last arrays. Help me to split an array into 2 different arrays
For single dimensional array:
$newArray = array();
foreach($originalArray['hours'] as $k=>$hours){
$newArray[$k]['dates'] = $originalArray['dates'];
$newArray[$k]['hours'] = $hours;
}
Output: https://3v4l.org/oE3hh
For Multidimensional array:
$newArray = array();
foreach($originalArray as $originalArr){
foreach($originalArr['hours'] as $k=>$hours){
$newArray[$originalArr['dates']][$k]['dates'] = $originalArr['dates'];
$newArray[$originalArr['dates']][$k]['hours'] = $hours;
}
}
$newArray = array_values($newArray);
print_r($newArray);
Output: https://3v4l.org/PAppV

PHP Array for hard multilevel items

i have this array structure and i want to create other array structure with that:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
4 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)
5 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Plastico' (length=8)
6 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '64GB' (length=4)
2 => string 'Cristal' (length=7)
Each array item have a group of characteristics like:
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
....
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)
I need an array that have this values like
Azul=>64GB=>Plastico
128GB=>Cristal
if sub item repeats only write the children value
Azul=>64GB=>Cristal Only adds Cristal because 64GB exists
I have this code in php:
foreach ($arrCaracteristicas as $itemMadre => $valueMadre) {
$NombreNodo = $valueMadre[0];
if ($NumeroCaracteristicas > 1) {
for ($i = 1; $i <= $NumeroCaracteristicas - 1; $i++) {
$NombreCaract = $valueMadre[$i];
echo "$NombreNodo: $NombreCaract <br>";
array_push( $arrSelectMain[$NombreNodo], $NombreCaract);
if (!isset($arrSelectMain[$NombreNodo][$NombreCaract])) {
//$arrSelectMain[$NombreNodo]["d"] = $NombreCaract;
}
}
}
}
Where $arrCaracteristicas have the long array, and $arrSelectMain have the first level of the array keys:
Code of first level:
foreach ($arrCaracteristicas as $item => $value) {
$arrSelectMain[$value[0]] = $value[0];
}
asort($arrSelectMain);
$arrSelectMain = array_unique($arrSelectMain);
This is an image with structure that i want:
https://ibb.co/Q8k198Y
If somebody can help me i will invite a beer in paypal :)
This should work, first separate the array into columns and then loop over the items.
$first = array_column($items, 0);
$second = array_column($items, 1);
$third = array_column($items, 2);
$output = [];
for ($i = 0; $i < count($first); $i++) {
$output[$first[$i]][$second[$i]][] = $third[$i];
}
Have a look at the sample

How to get specific data from multidimensional array using php

How to get 'syn' and 'sim' values as a string from the given arrays, as array could vary i only want to extract 'syn' and 'sim' as an example following arrays are given ,i know it might be a simple question but i am new to multidimensional array that's why can't seem to solve it.
array (size=1)
'adjective' =>
array (size=2)
'syn' =>
array (size=7)
0 => string 'cagey' (length=5)
1 => string 'cagy' (length=4)
2 => string 'canny' (length=5)
3 => string 'apt' (length=3)
4 => string 'cunning' (length=7)
5 => string 'ingenious' (length=9)
6 => string 'adroit' (length=6)
'sim' =>
array (size=4)
0 => string 'adroit' (length=6)
1 => string 'artful' (length=6)
2 => string 'intelligent' (length=11)
3 => string 'smar
array (size=2)
'noun' =>
array (size=3)
'syn' =>
array (size=23)
0 => string 'passion' (length=7)
1 => string 'beloved' (length=7)
2 => string 'dear' (length=4)
3 => string 'dearest' (length=7)
4 => string 'honey' (length=5)
5 => string 'sexual love' (length=11)
6 => string 'erotic love' (length=11)
7 => string 'lovemaking' (length=10)
8 => string 'making love' (length=11)
9 => string 'love life' (length=9)
10 => string 'concupiscence' (length=13)
11 => string 'emotion' (length=7)
12 => string 'eros' (length=4)
13 => string 'loved one' (length=9)
14 => string 'lover' (length=5)
15 => string 'object' (length=6)
16 => string 'physical attraction' (length=19)
17 => string 'score' (length=5)
18 => string 'sex' (length=3)
19 => string 'sex activity' (length=12)
20 => string 'sexual activity' (length=15)
21 => string 'sexual desire' (length=13)
22 => string 'sexual practice' (length=15)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
'usr' =>
array (size=1)
0 => string 'amour' (length=5)
'verb' =>
array (size=2)
'syn' =>
array (size=29)
0 => string 'love' (length=4)
1 => string 'enjoy' (length=5)
2 => string 'roll in the hay' (length=15)
3 => string 'make out' (length=8)
4 => string 'make love' (length=9)
5 => string 'sleep with' (length=10)
6 => string 'get laid' (length=8)
7 => string 'have sex' (length=8)
8 => string 'know' (length=4)
9 => string 'do it' (length=5)
10 => string 'be intimate' (length=11)
11 => string 'have intercourse' (length=16)
12 => string 'have it away' (length=12)
13 => string 'have it off' (length=11)
14 => string 'screw' (length=5)
15 => string 'jazz' (length=4)
16 => string 'eff' (length=3)
17 => string 'hump' (length=4)
18 => string 'lie with' (length=8)
19 => string 'bed' (length=3)
20 => string 'have a go at it' (length=15)
21 => string 'bang' (length=4)
22 => string 'get it on' (length=9)
23 => string 'bonk' (length=4)
24 => string 'copulate' (length=8)
25 => string 'couple' (length=6)
26 => string 'like' (length=4)
27 => string 'mate' (length=4)
28 => string 'pair' (length=4)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
Any help would be appreciated thanks!
Create a function:
function getArrayAsString($array, $key) {
if (isset($array[$key])) {
return is_array($array[$key]) ? implode(', ', $array[$key]) : $array[$key];
}
return '';
}
Usage example:
$synData = getArrayAsString($array['adjective'], 'sim');
$simData = getArrayAsString($array['noun'], 'syn');

check if values of an array is unique. if so randomize array

I have an array like this
0 => string '' (length=0)
1 => string 'nameMiddle' (length=10)
2 => string 'dsa' (length=3)
3 => string '' (length=0)
4 => string 'phoneFirst' (length=10)
5 => string 'addressStreet' (length=13)
6 => string 'addressCity' (length=11)
7 => string 'addressState' (length=12)
8 => string 'addressZip' (length=10)
9 => string 'status' (length=6)
10 => string 'gender' (length=6)
11 => string 'dateOfBirth' (length=11)
12 => string 'numberOfDependents' (length=18)
13 => string 'salary' (length=6)
14 => string 'startDate' (length=9)
15 => string 'ltdClass' (length=8)
16 => string 'deductionMedical' (length=16)
17 => string 'deductionDental' (length=15)
18 => string 'deductionVision' (length=15)
19 => string 'suppLyfCost' (length=11)
20 => string 'stdCost' (length=7)
21 => string 'effectiveDate' (length=13)
These values would eventually be keys using array_combine but I cannot have the same key on an array_combine. How can I check if the array has the same value if so how can I make the value unique into like this
0 => string '0'
3 => string '3'
thanks
To replace values that are empty or not unique with their key, try:
foreach($array as $k => &$v) {
if(empty($v) || count(array_search($v, $array)) > 1) {
$v = $k;
}
}

Array 1st value as a key on new array and second as its values

I have a array:
$array1 = array
0 =>
array
0 => string 'biodata' (length=7)
1 => string 'family_name' (length=11)
1 =>
array
0 => string 'biodata' (length=7)
1 => string 'first_name' (length=10)
2 =>
array
0 => string 'biodata_education' (length=17)
1 => string 'subject' (length=7)
3 =>
array
0 => string 'biodata_education' (length=20)
1 => string 'year' (length=5)
which need to converted like:
array
biodata =>
array
0 => string 'family_name' (length=7)
1 => string 'first_name' (length=11)
biodata_education =>
array
0 => string 'subject' (length=7)
1 => string 'year' (length=10)
as it can be done by simple iteration, I tried this one and done.
foreach($array1 as $tbl):
$table[$tbl[0]][] = $tbl[1];
endforeach;
<?php
//map the array using a foreach loop
foreach($array1 as $tbl)
{
$table[ $tbl[0] ][] = $tbl[1];
}

Categories