php | array_map replacement - php

I'm looking for the way to replace my array.
My first array below:
$arr1 = Array
(
[0] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 0
)
[1] => stdClass Object
(
[field_value] => Red
[count] => 0
)
)
)
[1] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Plastic
[count] => 0
)
[1] => stdClass Object
(
[field_value] => Metall
[count] => 0
)
)
)
My second array:
$arr2 = Array
(
[0] => 2
[1] => 6
[2] => 3
[3] => 4
)
And I would like to get this:
Array
(
[0] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Red
[count] => 6
)
)
)
[1] => stdClass Object
(
[values] => Array
(
[0] => stdClass Object
(
[field_value] => Plastic
[count] => 3
)
[1] => stdClass Object
(
[field_value] => Metall
[count] => 4
)
)
)
I have tried to use array_map function but without any success.
array_map(function($a,$b){$a = $b; return $a;}, $arr1, $arr2);
Thanks!

$array=$arr[0]->values;
$new_array=array();
foreach($array as $key=>$val)
{
$new_array[$key]=$val;
$new_array[$key]->count=$arr2[$key];
}
$result=array();
$result[0]->values=$new_array;

Create your own function as needed, and customize as you like, see below example:
function buildMyArray($array1, $array2)
{
foreach($array1[0]->values as $key => $value){
$array1[0]->values[$key]['count'] = $array2[$key];
}
return $array1;
}
and you may call it like:
$result = buildMyArray($arr1, $arr2);

If you want to use array_map, then something should be like below:
$arr1[0]->values = array_map(function($v, $k) use ($arr2) {
// if not found in $arr2, remain the original value.
$v->count = isset($arr2[$k]) ? $arr2[$k] : $v->count;
return $v;
}, $arr1[0]->values, array_keys($arr1[0]->values));

Related

i have an array with 8 values in it i want to print 4 in each row dynamically

this is the array
Array (
[0] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[1] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[2] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[3] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[4] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
[5] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[6] => Array (
[0] => stdClass Object (
[cat_count] => 1
)
)
[7] => Array (
[0] => stdClass Object (
[cat_count] => 0
)
)
)
Since you haven't given very much information about your problem, it's not easy to give a 'correct' answer. But i think you can go with this:
x = your array
foreach(array_chunk($x, 4) AS $chunk){
foreach($chunk AS $value){
echo $value->catcount.' - ';
}
echo "<br />";
}
thx to #smartpal for the array_chunk idea

Convert Multidimensional array to single array in codeigniter php

I have a multidimensional array as shown below
Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 237
)
[1] => stdClass Object
(
[id] => 228
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 247
)
[1] => stdClass Object
(
[id] => 238
)
)
)
I want to convert into single array as shown below
Array
(
[0] => stdClass Object
(
[id] => 237
)
[1] => stdClass Object
(
[id] => 228
)
[2] => stdClass Object
(
[id] => 247
)
[3] => stdClass Object
(
[id] => 238
)
)
I have tried with the following solution Convert multidimensional array into single array
But the result am not getting Its Coming Null
How to get the desired result for the above input.
Any help appreciated.
Try foreach loop then array_merge()
$result = [];
foreach ($array as $value) {
$result = array_merge($result, $value);
}
var_dump($result);
Hope this one works
function array_flattern($arr) {
$returnArr=[];
foreach($arr as $k=>$v) {
$returnArr = array_merge($returnArr, $v);
}
return $returnArr;
}

Filter multidimensional php array with another array

I have an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);

PHP Array combine using one column value

I am trying to combine two Arrays with one common column.But, not getting exactly what I want. Please check the below requirement.
Array 1
Array
(
[0] => stdClass Object
(
[fieldLabel] => fname
[uuid] => 27478
)
[1] => stdClass Object
(
[fieldLabel] => Lname
[uuid] => 6103
)
[2] => stdClass Object
(
[fieldLabel] => Country
[uuid] => 7350
)
[3] => stdClass Object
(
[fieldLabel] => check1
[uuid] => 23155
)
[4] => stdClass Object
(
[fieldLabel] => radio1
[uuid] => 15664
)
)
Array 2
Array
(
[0] => stdClass Object
(
[uuid] => 27478
[value] => sai1
)
[1] => stdClass Object
(
[uuid] => 6103
[value] => sai2
)
[2] => stdClass Object
(
[uuid] => 7350
[value] => USA
)
[3] => stdClass Object
(
[uuid] => 23155
[value] => USA|India
)
)
I need output like the below. Both arrays 'UUID' is common. If value not there in second array it should be empty.
Array
(
[0] => stdClass Object
(
[fieldLabel] => fname
[uuid] => 27478
[value] =>sai1
)
[1] => stdClass Object
(
[fieldLabel] => Lname
[uuid] => 6103
[value] =>sai2
)
[2] => stdClass Object
(
[fieldLabel] => Country
[uuid] => 7350
[value] =>USA
)
[3] => stdClass Object
(
[fieldLabel] => check1
[uuid] => 23155
[value] =>USA|India
)
[4] => stdClass Object
(
[fieldLabel] => radio1
[uuid] => 15664
)
)
Please provide your suggestions. How can I achieve.
You can traverse those arrays and make changes like below :
foreach ($array1 as &$a1val) {
$value = 0;
foreach ($array2 as $a2val) {
if($a1val->uuid == $a2val->uuid) {
$value = $a2val->value;
break;
}
}
$a1val->value = $value;
}
P.S. :
& is used to update $array1 by reference.
foreach($arr1 as $key1 => val1){
foreach($arr2 as $key2 => $val2){
if($val1['uuid'] == $val2['uuid']){
$arr1[$key1]['value'] = $val2['value'];
}
}
}
print_r($arr1); // this should give desire output

reorder multidimensional php array trouble

I need to reorder an multidimensional array in php but I can't get it right and I am struggling with foreach, for and while loops for some time now.
This is what I have:
Array
(
[dimension] => Array
(
[dimension.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => geheel
)
[1] => SimpleXMLElement Object
(
[0] => geheel
)
)
[dimension.type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => hoogte
)
[1] => SimpleXMLElement Object
(
[0] => breedte
)
)
[dimension.value] => Array
(
[0] => SimpleXMLElement Object
(
[0] => 73
)
[1] => SimpleXMLElement Object
(
[0] => 84
)
)
[dimension.unit] => Array
(
[0] => SimpleXMLElement Object
(
[0] => cm
)
[1] => SimpleXMLElement Object
(
[0] => cm
)
)
)
[material] => Array
(
[material.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => deelmateriaal
)
[1] => SimpleXMLElement Object
(
[0] => deelmateriaal2
)
)
[material_type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => typemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => typemateriaal2
)
)
[material] => Array
(
[0] => SimpleXMLElement Object
(
[0] => materiaal
)
[1] => SimpleXMLElement Object
(
[0] => materiaal2
)
)
[material.notes] => Array
(
[0] => SimpleXMLElement Object
(
[0] => notemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => notemateriaal2
)
)
)
)
And I need to transform it to:
dimensions => Array ( 1 => array(dimension.part => geheel)
=> array (dimension.type => hoogte) )
.....
=> Array ( 2 => array(dimension.part => ...)
=> array (dimension.type => ...) )
....
....
material => Array ( 1 => ...
2 => ...
=> Array ( 1 =>
=>
....
Anyone got a good approach to this?
Thanks,
Joris
foreach($arr['dimension'] as $keyType => $type) {
foreach($type as $key => $value) {
$aNewArray[$key][$keyType] = $value;
}
}
Something like that.. And then process it a bit more to suit your needs..
What it does: walk through every dimension array (and store the key of it in $keyType) and then walk through every item and add it to a new array in the way you want it. It assumes every type has the same amount of items under it.
$new_array = new array();
$count = count($array['dimension']['dimension_part']);
for($i=0;$i<$count;$i++) {
$new_array[$i]['dimension.part'] = $array['dimension']['dimension.part'][$i][0];
$new_array[$i]['dimension.type'] = $array['dimension']['dimension.type'][$i][0];
$new_array[$i]['dimension.value'] = $array['dimension']['dimension.value'][$i][0];
}
I am not quite sure how you access simplexmlobjects, so I used the array type. maybe change it to [$i] -> 0

Categories