fill an array with missing values and keys - php

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()

Related

Remove Duplicated Values From Array - PHP

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
)
)
)
)

Array find and replace based other array

I have an array $data:
Array
(
[0] => Array
(
[quarter] => Q1
[category] => DEODORANTS,FACE CARE
[fund_type] => EOT
)
[1] => Array
(
[quarter] => Q2
[category] => BODY CARE
[fund_type] => A&P
)
[2] => Array
(
[quarter] => Q2
[category] => ORAL CARE,NOCATEGORY
[fund_type] => A&P
)
)
and other array $categories:
Array
(
[0] => Array
(
[id] => 1
[descriptions] => DEODORANTS
)
[1] => Array
(
[id] => 2
[descriptions] => BODY CARE
)
[2] => Array
(
[id] => 3
[descriptions] => FACE CARE
)
[3] => Array
(
[id] => 4
[descriptions] => ORAL CARE
)
)
I need to change value of category in $data with id from $categories:
code:
function lookup($data = array(), $categories = array())
{
if(is_array($data))
{
foreach ($data as $k => $v)
{
$valCat = explode(',', $v['category']);
if(!empty($valCat))
{
foreach ($valCat as $vc)
{
foreach ($categories as $c)
{
if($c['descriptions'] === $vc)
{
$CatID[] = $c['id'];
}
}
}
}
$data[$k]['category'] = $CatID;
}
return $data;
}
}
result lookup($data, $categories):
Array
(
[0] => Array
(
[quarter] => Q1
[category] => Array
(
[0] => 1
[1] => 3
)
[fund_type] => EOT
)
[1] => Array
(
[quarter] => Q2
[category] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[fund_type] => A&P
)
[2] => Array
(
[quarter] => Q2
(
[0] => 1
[1] => 3
[2] => 2
[3] => 4
)
[fund_type] => A&P
)
)
correct result should be:
Array
(
[0] => Array
(
[quarter] => Q1
[category] => Array
(
[0] => 1
[1] => 3
)
[fund_type] => EOT
)
[1] => Array
(
[quarter] => Q2
[category] => Array
(
[0] => 2
)
[fund_type] => A&P
)
[2] => Array
(
[quarter] => Q2
(
[0] => 4
)
[fund_type] => A&P
)
)
You're not resetting the value of $CatID. After this line
foreach ($data as $k => $v)
{
add
$CatID = array();

array one format to another

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',
)

array search for a key=>value

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);

array difference

I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;

Categories