What I want to achieve
I want to make as many arrays as the number of the secondArray (in this particular case: 2) only if the value is 1
I have the array firstArray.
firstArray
(
[analysis] => coding
[data] => Array
(
[data1] => Array
(
[0] => Array
(
[name] => "test"
)
)
[data2] => Array
(
[0] => Array
(
[age] => 22
)
)
[data3] => Array
(
[0] => Array
(
[CODE] => 1111 //Stable
[QUANTITY] => 1
)
)
)
)
My second array is secondArray
secondArray
(
[0] => Array
(
[test1] => 1
[test2] => 1
[test3] => 1
[test4] => 1
)
[1] => Array
(
[test1] => 1
[test2] =>
[test3] => 1
[test4] =>
)
)
What I want to achieve
I want to make as many arrays as the number of the secondArray (in this particular case: 2) only if the value is 1
The result must be:
firstArray
(
[analysis] => coding
[data] => Array
(
[data1] => Array
(
[0] => Array
(
[name] => "test"
)
)
[data2] => Array
(
[0] => Array
(
[age] => 22
)
)
[data3] => Array
(
[0] => Array
(
[CODE] => 1111 //Stable
[QUANTITY] => 1
)
[1] => Array
(
[CODE] => 0001
[QUANTITY] => 1
)
[2] => Array
(
[CODE] => 0002
[QUANTITY] => 1
)
[3] => Array
(
[CODE] => 0003
[QUANTITY] => 1
)
[4] => Array
(
[CODE] => 0004
[QUANTITY] => 1
)
)
)
)
and second firstArray must be
firstArray
(
[analysis] => coding
[data] => Array
(
[data1] => Array
(
[0] => Array
(
[name] => "test"
)
)
[data2] => Array
(
[0] => Array
(
[age] => 22
)
)
[data3] => Array
(
[0] => Array
(
[CODE] => 1111 //Stable
[QUANTITY] => 1
)
[1] => Array
(
[CODE] => 0001
[QUANTITY] => 1
)
[2] => Array
(
[CODE] => 0003
[QUANTITY] => 1
)
)
)
)
the code I am currently working on is, but I do not get the desired result. the second firstArray also prints the values of the first firstArray
foreach ($secondArray as $key => $value) {
if($key == 'test1' && $value == "1"){
array_push($firstArray ['data']['data3'],
array('CODE' => "0001",
'QUANTITY' => "1"
)
);
}
if($key == 'test2' && $value == "1"){
array_push($firstArray ['data']['data3'],
array('CODE' => "0002",
'QUANTITY' => "1"
)
);
}
if($key == 'test3' && $value == "1"){
array_push($firstArray ['data']['data3'],
array('CODE' => "0003",
'QUANTITY' => "1"
)
);
}
if($key == 'test4' && $value == "1"){
array_push($firstArray ['data']['data3'],
array('CODE' => "0004",
'QUANTITY' => "1"
)
);
}
}
You could write for example a nested foreach and check if the key starts with test instead of hardcoding all the different keys.
Then increment a variable for the value of CODE and use for example a padding for the leading zeroes.
$firstArray = [
"analysis" => "coding",
"data" => ["data1" => [["name" => "test"]], "data2" => [["age" => 22]], "data3" => [["CODE" => 1111, "QUANTITY" => 1]]]
];
$secondArray = [
["test1" => "1", "test2" => "1", "test3" => "1", "test4" => "1"],
["test1" => "1", "test2" => "", "test3" => "1", "test4" => ""]
];
$result = [];
foreach ($secondArray as $sa) {
$code = 1;
$tmp = $firstArray;
foreach ($sa as $key => $value) {
if (substr($key, 0, 4) === "test" && $value === "1") {
$tmp["data"]["data3"][] = [
"CODE" => str_pad($code, 4, "0", STR_PAD_LEFT),
"QUANTITY" => 1
];
}
$code++;
}
$result[] = $tmp;
}
foreach ($result as $r) {
print_r($r);
}
Instead of substr to check the first 4 characters to start with test you could also make the check more specific and check for test followed by 1 o more digits using:
if (preg_match("/^test\d+$/", $key) && $value === "1") {
See a php demo.
Output
Array
(
[analysis] => coding
[data] => Array
(
[data1] => Array
(
[0] => Array
(
[name] => test
)
)
[data2] => Array
(
[0] => Array
(
[age] => 22
)
)
[data3] => Array
(
[0] => Array
(
[CODE] => 1111
[QUANTITY] => 1
)
[1] => Array
(
[CODE] => 0001
[QUANTITY] => 1
)
[2] => Array
(
[CODE] => 0002
[QUANTITY] => 1
)
[3] => Array
(
[CODE] => 0003
[QUANTITY] => 1
)
[4] => Array
(
[CODE] => 0004
[QUANTITY] => 1
)
)
)
)
Array
(
[analysis] => coding
[data] => Array
(
[data1] => Array
(
[0] => Array
(
[name] => test
)
)
[data2] => Array
(
[0] => Array
(
[age] => 22
)
)
[data3] => Array
(
[0] => Array
(
[CODE] => 1111
[QUANTITY] => 1
)
[1] => Array
(
[CODE] => 0001
[QUANTITY] => 1
)
[2] => Array
(
[CODE] => 0003
[QUANTITY] => 1
)
)
)
)
Related
I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)
I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
I need to modify a few things about a multidimensional array.
First, I need to convert a key that contains an array into an element of that same array. In order to do that, I need to find the key's name. The problem here is that the key keeps changing it's name. The key I'm gonna base the modifications on is called "grupo*" and a number.
Here's an example of the array (extracted using the print_r() PHP function):
Array
(
[codigo] => 21201
[nombre] => CALCULO INTEGRAL
[creditos] => 3
[grupo8] => Array
(
[horario] => Array
(
[martes] => Array
(
[salon] => Array
(
[0] => G-216
[1] => G-216
)
[horaInicio] => Array
(
[0] => 1600
[1] => 1700
)
[horaFin] => Array
(
[0] => 1700
[1] => 1800
)
)
[jueves] => Array
(
[salon] => Array
(
[0] => C-102
[1] => C-102
)
[horaInicio] => Array
(
[0] => 1600
[1] => 1700
)
[horaFin] => Array
(
[0] => 1700
[1] => 1800
)
)
)
)
)
Array
(
[codigo] => 21202
[nombre] => FISICA MECANICA
[creditos] => 4
[grupo1] => Array
(
[horario] => Array
(
[lunes] => Array
(
[salon] => Array
(
[0] => Lab B-207
[1] => Lab B-207
)
[horaInicio] => Array
(
[0] => 1300
[1] => 1400
)
[horaFin] => Array
(
[0] => 1400
[1] => 1500
)
)
[martes] => Array
(
[salon] => Array
(
[0] => G-110
[1] => G-110
)
[horaInicio] => Array
(
[0] => 1300
[1] => 1400
)
[horaFin] => Array
(
[0] => 1400
[1] => 1500
)
)
[jueves] => Array
(
[salon] => Agora 101
[horaInicio] => 1300
[horaFin] => 1400
)
)
)
)
Array
(
[codigo] => 21203
[nombre] => ALGEBRA LINEAL
[creditos] => 3
[grupo13] => Array
(
[horario] => Array
(
[lunes] => Array
(
[salon] => B-108
[horaInicio] => 1100
[horaFin] => 1200
)
[viernes] => Array
(
[salon] => Array
(
[0] => B-107
[1] => B-107
)
[horaInicio] => Array
(
[0] => 1000
[1] => 1100
)
[horaFin] => Array
(
[0] => 1100
[1] => 1200
)
)
)
)
)
Array
(
[codigo] => 21304
[nombre] => PROGRAMACION ORIENTADA A OBJETOS
[creditos] => 3
[grupo4] => Array
(
[horario] => Array
(
[miercoles] => Array
(
[salon] => Array
(
[0] => B209 lab
[1] => B209 lab
[2] => B209 lab
)
[horaInicio] => Array
(
[0] => 1400
[1] => 1500
[2] => 1600
)
[horaFin] => Array
(
[0] => 1500
[1] => 1600
[2] => 1700
)
)
)
)
)
Array
(
[codigo] => 275201
[nombre] => MATEMATICAS DISCRETAS
[creditos] => 2
[grupo2] => Array
(
[horario] => Array
(
[jueves] => Array
(
[salon] => Array
(
[0] => A-203
[1] => A-203
)
[horaInicio] => Array
(
[0] => 1100
[1] => 1200
)
[horaFin] => Array
(
[0] => 1200
[1] => 1300
)
)
)
)
)
Array
(
[codigo] => MAKE
[nombre] => MARKETING ELECTRONICO
[creditos] => 2
[grupo1] => Array
(
[horario] => Array
(
[viernes] => Array
(
[salon] => Array
(
[0] => G-219
[1] => G-219
)
[horaInicio] => Array
(
[0] => 1200
[1] => 1300
)
[horaFin] => Array
(
[0] => 1300
[1] => 1400
)
)
)
)
)
As you can see, the key [grupo*] keeps changing number, and it can be a 1 or 2 digit number. The only thing I know is that such a key contains an array.
I need to do the same thing to one of the arrays inside, the one that is a weekday.
Here is an example of how the array is supposed to look (as JSON):
{
"codigo": "21201",
"nombre": "CALCULO INTEGRAL",
"creditos": "3",
"grupo": "8",
"horario": [
{
"dia": "martes",
"salon": [
"G-216",
"G-216"
],
"horaInicio": [
"1600",
"1700"
],
"horaFin": [
"1700",
"1800"
]
},
{
"dia": "jueves",
"salon": [
"C-102",
"C-102"
],
"horaInicio": [
"1600",
"1700"
],
"horaFin": [
"1700",
"1800"
]
}
]
}
The weekday keeps changing, so I have a similar problem there.
old array
Array
(
[0] => Array
(
[codigo] => MAKE
[nombre] => MARKETING ELECTRONICO
[creditos] => 2
[grupo12] => Array
(
[horario] => Array
(
[lunes] => Array
(
[salon] => Array
(
[0] => G-219
[1] => G-219
)
[horaInicio] => Array
(
[0] => 1200
[1] => 1300
)
[horaFin] => Array
(
[0] => 1300
[1] => 1400
)
)
)
)
)
[1] => Array
(
[codigo] => MAKE
[nombre] => PROGRAMACION ORIENTADA A OBJETOS
[creditos] => 3
[grupo13] => Array
(
[horario] => Array
(
[viernes] => Array
(
[salon] => Array
(
[0] => B209 lab
[1] => B209 lab
)
[horaInicio] => Array
(
[0] => 1400
[1] => 1500
)
[horaFin] => Array
(
[0] => 1600
[1] => 1700
)
)
)
)
)
)
All in one line of code
foreach ($data as $k => $groups) {
foreach ($groups as $v => $group) {
// find the match
if (preg_match('/^grupo*/', $v)) {
// split the key into key and value.
$key = substr($v, 0, 5);
$value = substr($v, 5);
// push the key into the array and assign its value
$data[$k][$key] = $value;
// loop through the horario array
foreach ($group['horario'] as $d => $dia) {
// push dia as key into horario array assign the key $d as value
$group['horario'][] = ['dia'=>$d,$dia];
//push the sub arrays into the new horario array
//foreach ($dia as $a => $arr) {
// $group['horario'][$a] = $arr;
//}
//unset the old horario array
unset($group['horario'][$d]);
}
// push the new horario in the main array
$data[$k]['horario'] = $group['horario'];
// unset the old grupo array
unset($data[$k][$v]);
}
}
}
echo json_encode($data, JSON_PRETTY_PRINT);
[
{
"codigo": "MAKE",
"nombre": "MARKETING ELECTRONICO",
"creditos": 2,
"grupo": "12",
"horario": [
{
"dia": "lunes",
"0": {
"salon": [
"G-219",
"G-219"
],
"horaInicio": [
"1200",
"1300"
],
"horaFin": [
"1300",
"1400"
]
}
},
{
"dia": "martes",
"0": {
"salon": [
"G-219",
"G-219"
],
"horaInicio": [
"1200",
"1300"
],
"horaFin": [
"1300",
"1400"
]
}
}
]
}
]
hope this helps.
Assuming your array is:
Array
(
[codigo] => 21201
[nombre] => CALCULO INTEGRAL
[creditos] => 3
[grupo8] => Array
(
[horario] => Array
(
[martes] => Array
(
[salon] => Array
(
[0] => G-216
[1] => G-216
)
[horaInicio] => Array
(
[0] => 1600
[1] => 1700
)
[horaFin] => Array
(
[0] => 1700
[1] => 1800
)
)
[jueves] => Array
(
[salon] => Array
(
[0] => C-102
[1] => C-102
)
[horaInicio] => Array
(
[0] => 1600
[1] => 1700
)
[horaFin] => Array
(
[0] => 1700
[1] => 1800
)
)
)
)
)
You can get all keys of it with array_keys:
$keys = array_keys($arr);
$needed_key = false;
foreach ($keys as $k) {
if (strpos($k, 'grupo') !== false) {
$needed_key = $k;
break;
}
}
if ($needed_key) {
$arr['horario'] = $arr[$needed_key]['horario'];
$arr['grupo'] = substr($needed_key, 5);
unset($arr[$needed_key]);
}
I have this array $all_zones that comes sometimes with missing keys and values and I would like to fill the array with empty values for the messing keys, here's the array:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
)
)
The thing is the $all_zones[$key]['price'] depend on how many ranges there's for each Zone, inthis case $range_count = count($all_ranges); will display 2, so I'd like to fill the missing keys for 2 times : Here's the output:
Array
(
[0] => Array
(
[id_zone] => 1
[name] => Europe
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id_zone] => 3
[name] => Asia
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
[2] => Array
(
[id_zone] => 4
[name] => Africa
[price] => Array
(
[0] => 3.00
[1] => 6.00
)
[id_delivery] => Array
(
[0] => 3
[1] => 4
)
)
[3] => Array
(
[id_zone] => 5
[name] => Oceania
[price] => Array
(
[0] =>
[1] =>
)
[id_delivery] => Array
(
[0] =>
[1] =>
)
)
)
Here's what I've tried so far and didn't succeed:
$range_count = count($all_ranges);
$i=0;
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
if($range_count>$i){
$disabled[]=$key;
$all_zones[$key]['price'][] = '';
$all_zones[$key]['id_delivery'][] = '';
}
$i++;
}
}
Any help with this? Much appreciated.
try this
$range_count = count($all_ranges);
foreach ($all_zones as $key => $value) {
if(isset($value['id_zone']) && isset($value['name']) && (!isset($value['price']) || !isset($value['id_delivery']))){
$disabled[]=$key;
if((!isset($value['price']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
if((!isset($value['id_delivery']))
{
for($i=0; $i<$range_count<$i++)
{
$all_zones[$key]['id_delivery'][] = '';
}
}
}
}
You might have operator precedence problem.
( (!isset($value['price']) || !isset($value['id_delivery'])) )
The way to do this is to loop through the array, with an array_merge() on each array within the parent array to set your 'defaults'.
$zone_template = array(
'id_zone' => '',
'name' => '',
'price' => array(
0 => '',
1 => ''
),
'id_delivery' = array(
0 => '',
1 => ''
)
);
foreach ($all_zones as $zone) {
array_merge($zone_template, $zone);
}
Can also be done with array_walk()
I have an array,
$arr=(
[0] => Array
(
[groupid] => 1
[groupname] => Red
[members] => Array
(
[0] => Array
(
[mid] => 9
[name] => Anith
)
[1] => Array
(
[mid] => 11
[name] => Aravind
)
[2] => Array
(
[mid] => 10
[name] => Lekshmi
)
)
)
[1] => Array
(
[groupid] => 2
[groupname] => Blue
[members] => Array
(
[0] => Array
(
[mid] => 6
[name] => Yamuna
)
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
[2] => Array
(
[mid] => 13
[name] => Sooraj K
)
)
)
I want to check [mid] => 2 is in the array..If it exists
I want to delete it(ie. unset the array )-----
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
;;;
eg:--unset($arr[1]['members'][2];
This should do the trick
foreach ($arr as $group => $subarray) {
foreach ($subarray['members'] as $k => $v) {
if ($v['mid'] == 2) {
unset($arr[$group]['members'][$k]);
break;
}
}
}
var_dump($arr);
If you feel like getting crafty, you could do something like this:
// note: requires PHP >= 5.3
foreach ($arr as $key => &$value) {
$value['members'] = array_filter(
$value['members'],
function($member) {
return $member['mid'] != 2;
}
);
}
var_dump($arr);