I'm having a hard time figuring out how to implement this so here it is. I have an array
$arr = array("purchase_order_details_id"=>array(
0=>"POD1",
1=>"POD1",
2=>"POD2",
),
"quantity_received"=>array(
0=>5,
1=>10,
2=>20
)
);
I want to split the arrays into two. Into something like this.
$pod_2 = array("purchase_order_details_id"=>array(
0=>"POD1",
1=>"POD1"
),
"quantity_received"=>array(
0=>5,
1=>10
));
$pod_1 = array("purchase_order_details_id"=>array(
2=>"POD2"
),
"quantity_received"=>array(
2=>20
));
Anyone has an idea on how to do this ? Any thoughts is appreciated. Thanks
I use array_intersect to find the POs in a loop of unique POs.
Then I use array_inyersect_key to get the quantity.
This requires only one iteration per unique Purchase_order_detali_id.
Meaning it has a much better performance than looping the full array.
Edit: added extract to create the two variables. But I would rather keep them in the array if I was you.
$pods = array_unique($arr["purchase_order_details_id"]);
Foreach($pods as $pod){
$PO = array_intersect($arr["purchase_order_details_id"], [$pod]);
$qt = array_intersect_key($arr["quantity_received"], $PO);
$new[$pod] = ["purchase_order_details_id" => $PO, "quantity_received" => $qt];
}
Var_dump($new);
extract($new);
https://3v4l.org/dBpuJ
Try with below code:
$array = array();
foreach($arr['purchase_order_details_id'] as $key => $val)
{
$array[$val]['purchase_order_details_id'][] = $val;
$array[$val]['quantity_received'][] = $arr['quantity_received'][$key];
}
echo "<pre>";
print_r($array);
echo "</pre>";
extract($array);
echo "<pre>";
print_r($POD1);
echo "</pre>";
echo "<pre>";
print_r($POD2);
echo "</pre>";
foreach ($arr as $key => $val) {
$size = ceil(count($val) / 2);
$arr2 = array_chunk($val, $size, true);
$pod_2[$key] = $arr2[0];
$pod_1[$key] = $arr2[1];
}
var_dump($pod_2);
var_dump($pod_1);
Related
I have already try like this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values(array_column($arrayName,'5'));
print_r($arr);
?>
I just want to counting of repeated value
This script counts the amount of each value.
<?php
echo '<pre>';
$arrayName = [1,2,3,4,5,6,2,3,1];
$arr = [];
foreach ($arrayName as $item) {
if (empty($arr[$item]))
$arr[$item] = 0;
$arr[$item] += 1;
}
print_r($arr);
if you want count the values
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr =array_count_values($arrayName);
print_r($arr);
Why are you using array_column if you only want to count repeated values
you can do this
<?php
echo '<pre>';
$arrayName = array(1,2,3,4,5,6,2,3,1 );
$arr= array_count_values($arrayName);
print_r($arr);
?>
I'm trying to write a function that do the following :
Let's say i have an array :
$data = array(
array('10','15','20','25'),
array('Blue','Red','Green'),
array('XL','XS')
)
and my result array should be like :
$result = array(
array('10','15','20','25'),
array('Blue','Red','Green','Blue','Red','Green','Blue','Red','Green','Blue','Red','Green')
array('XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS','XL','XS')
)
Im stuck with this because i want a function that is able to do this no matter how much array there is in the first array $data
I have only been able to write this, which is what give the $result array :
foreach($data[2] as $value2){
$result[2][] = $value2;
foreach($data[1] as $value1){
$result[1][] = $value1;
foreach($data[0] as $value0){
$result[0][] = $value0;
}
}
}
After a few research, it seems that a recursive function is the way to go in order to dynamically generate those foreach but i can't get it to work.
Thanks for your help.
This is dynamic:
$result[] = array_shift($data);
foreach($data as $value) {
$result[] = call_user_func_array('array_merge',
array_fill(0, count($result[0]), $value));
}
Get and remove the first element from original
Loop remaining elements and fill result with values X number of values in first element
Since the elements were arrays merge them all into result
If modifying the original is unwanted, then use this method:
$result[] = reset($data);
while($value = next($data)) {
$result[] = call_user_func_array('array_merge',
array_fill(0, count($result[0]), $value));
}
just use array_fill and array_merge functions
$result = array(
$data[0],
array_merge(...array_fill(0,count($data[0]), $data[1])),
array_merge(...array_fill(0,count($data[0])*count($data[0]), $data[2]))
);
print_r($result);
demo on eval.in
I have the following PHP code:
$special_files = array(
array("Turnip", "Tweed"),
array("Donald", "Trump")
);
I want to be able to get the second value in a nested array by identifying a first. eg: if_exists("Donald") would return "trump".
I've tried to recurse through the array but I'm at a loss on how to select the second value once the first is identified.
Any help would be appreciated
You can use something like this:
$special_files = array(
array("Turnip", "Tweed"),
array("Donald", "Trump")
);
$search_val = "Donald";
$key = array_search($search_val, array_column($special_files,0));
$output = $special_files[$key][1]; //outputs "Trump"
Here is a working sample.
Well, you can try the following:
foreach ($special_files as $special_file) {
$i = 1;
foreach ($special_file as $element) {
if ($i==2) {
echo ("Second value is: " . $element);
break;
}
$i++;
}
}
You can extract the [1] elements and index them by the [0] elements:
$lookup = array_column($special_files, 1, 0);
$result = isset($lookup['Donald']) ?: false;
The $lookup array yields:
Array
(
[Turnip] => Tweed
[Donald] => Trump
)
I have below two arrays,
$category = array('available', 'notavailable' );
$values = array(1, 2 );
Now i want to get JSON output as below,
[{category: 'available', value:1}{category: 'notavailable', value:2}]
I tried using array_merge array_combine but could not got desired outlut with new Key values category and value,
How can i get that?
Thanks,
You can use array_map, if you have fixed keys:
<?php
$category = array('available', 'notavailable' );
$values = array(1, 2 );
$array = array_map(function($category, $value) {
return ['category' => $category, 'value'=>$value];
}, $category, $values);
echo "<pre>";
var_dump(json_encode($array));
echo "</pre>";
Output:
string(74) "[{"category":"available","value":1},{"category":"notavailable","value":2}]"
I think you must doing like this:
$result = array();
for ($i = 0; $i < count($category); $i++) {
$result[] = array(
'category' => $category[$i],
'value' => $values[$i]
);
}
echo json_encode($result);
$data variable get populated from required keys via $personalDetailsRequiredFields. The code below does work but is there a better shorter way?
$personalDetailsRequiredFields = [
'contact_title',
'contact_first_name',
'contact_last_name',
'contact_phone_number',
'contact_mobile_number',
'contact_email',
];
$personalDetails = SessionOrder::getPersonalDetails();
foreach($personalDetails as $key => $value) {
if (in_array($key,$personalDetailsRequiredFields)) {
$data['personalDetails'][$key] = $value;
}
}
echo "<pre>";
print_r($data);
echo "</pre>";
$data['personalDetails'] = array_intersect_key(
$personalDetails,
array_flip($personalDetailsRequiredFields)
);
should give you what you want without needing the loop and if test