I have an array like this:
$_SESSION['food'] = array(
// ARRAY 1
array(
"name" => "apple",
"shape" => "round",
"color" => "red"
),
// ARRAY 2
array(
"name" => "banana",
"shape" => "long",
"color" => "yellow"
)
);
I want to search through all keys in all child arrays and delete the entire child array if the search term is found.
So, basically:
If searching for "long", the entire Array 2 is removed.
If searching for "apple", the entire Array 1 is removed.
How would I accomplish this?
Thanks!
This should do the trick:
foreach ($array as $key => $value) {
foreach ($value as $child_value) {
if ($child_value == $search_term) {
unset($array[$key]);
continue 2;
}
}
}
Depending on how many dimensions you have, you can use array_search.
I haven't tested the following, but it should work:
$unset = array_search('apple', $_SESSION['food']);
unset($_SESSION['food'][$unset]);
Here you go:
<?php
function deleteObjWithProperty($search,$arr)
{
foreach ($arr as &$val)
{
if (array_search($search,$val)!==false)
{
unlink($val);
}
}
return $arr;
}
?>
$_SESSION['food'] = array(
// ARRAY 1
array(
"name" => "apple",
"shape" => "round",
"color" => "red"
),
// ARRAY 2
array(
"name" => "banana",
"shape" => "long",
"color" => "yellow"
)
);
echo '<pre>'.print_r($_SESSION['food']).'</pre>';
$arr_food = array();
$search_term = 'apple';
foreach($_SESSION['food'] AS $arr) {
if($arr['name'] == $search_term) {
unset($arr);
}
$arr_food[] = $arr;
}
$_SESSION['food'] = $arr_food;
echo '<pre>'.print_r($_SESSION['food']).'</pre>';
Related
I have 3 array like
$arr = [
"color" => [["name"=>"red"]],
"size" => [["name"=>"18 inch"], ["name"=>"15 inch"]],
"type" => [["name"=>"plastic"]]
]
$combo = array();
foreach ($arr['size'] as $size) {
foreach($arr['color'] as $color){
foreach ($arr['type'] as $type) {
$variant = json_encode(['size' => $size->name, 'color' =>
$color->name, 'type' => $type->name]);
array_push($combo,$variant);
}
}
}
echo $combo;
// result
0 => "{"size":"15 inch","color":"yellow","type":"metal"}"
1 => "{"size":"18 inch","color":"yellow","type":"plastic"}"
It works properly but but there is can be less or more variants. How can I handle this.
For example
$arr = [
"size" => [["name"=>"18 inch"], ["name"=>"15 inch"]],
"type" => [["name"=>"plastic"]]
]
Or
$arr = [
"color" => [["name"=>"red"]],
"size" => [["name"=>"18 inch"], ["name"=>"15 inch"]],
"type" => [["name"=>"plastic"]],
"brand" => [['name' => 'something']],
]
For what i understand, you have to combine the arrays of properties into one array of
object.
I have to leave now, but if you need a explanation leave a comment and i updated the answers
$arr = [
"color" => [["name"=>"red"],['name'=>'yellow']],
"size" => [["name"=>"18 inch"], ["name"=>"15 inch"]],
"type" => [["name"=>"plastic"]],
"brand" => [['name' => 'something']],
];
function runFor($arr ,&$array, $keys,$index,&$positions){
foreach ($arr[$keys[$index]] as $key => $espec){
$positions[$keys[$index]] = $key;
if($index + 1 < count($keys)){
runFor($arr,$array,$keys, $index+1,$positions);
}else{
$item = (object)[];
foreach ($keys as $key){
$item->$key = $arr[$key][$positions[$key]]['name'];
}
array_push($array,$item);
}
unset($positions[$keys[$index]]);
}
}
$array = array();
$keys = array_keys($arr);
$positions = [];
runFor($arr,$array,$keys,0,$positions);
$combo = array();
foreach ($array as $item){
array_push($combo,json_encode($item));
}
var_dump($combo);
What is the best way to sum the 'val' field with the same 'color' for each different color:
Array
(
[0] => Array
(
[color]=> "red"
[val]=> 4
)
[1] => Array
(
[color]=> "green"
[val]=> 3
)
[2] => Array
(
[color]=> "blue"
[val]=> 1
)
[3] => Array
(
[color]=> "green"
[val]=> 6
)
[4] => Array
(
[color]=> "blue"
[val]=> 2
)
)
Desired result : red: 4; green: 9; blue: 3.
Regards,
Elio Fernandes
I would do it this way, with a foreach loop:
$temp = [];
foreach($arr as $value) {
//check if color exists in the temp array
if(!array_key_exists($value['color'], $temp)) {
//if it does not exist, create it with a value of 0
$temp[$value['color']] = 0;
}
//Add up the values from each color
$temp[$value['color']] += $value['val'];
}
You can use array_reduce to have less and more readable code:
$array = array
(
0 => array("color"=> "red","val"=> 4),
1 => array("color"=> "blue","val"=> 3),
2 => array("color"=> "blue","val"=> 1)
);
function sum($accumulator, $item){
$accumulator[$item['color']] = $accumulator[$item['color']] ?? 0;
$accumulator[$item['color']] += $item['val'];
return $accumulator;
}
$sum = array_reduce($array, "sum");
var_dump($sum); // return here array(2) { ["red"]=> int(4) ["blue"]=> int(4) }
And the documentation: http://php.net/manual/en/function.array-reduce.php
I would loop it for readability's sake.
$output = array();
foreach($array as $value){
if(!isset($output[$value['color']])) $output[$value['color']] = 0; //Ensure the value is 0 if it doesn't exist
$output[$value['color']] += $value['val']; //Add the total for that color
}
This will return an array of colors to total counts.
Personally I'd do something like this with array_reduce, but as mentioned above it might not be the most readable option. Definitely a matter of opinion though.
$result = array_reduce($array, function ($carry, $item) {
$carry[$item['color']] = $carry[$item['color']] ?? 0;
$carry[$item['color']] += $item['val'];
return $carry;
}, []);
See https://eval.in/894644
Edit: Fix notices
To keep up the speed, without the foreach loop, I would say to do it like this:
$array = [
[
"color" => "red",
"val" => "4"
],
[
"color" => "green",
"val" => "3"
],
[
"color" => "blue",
"val" => "1"
],
[
"color" => "green",
"val" => "6"
],
[
"color" => "blue",
"val" => "2"
]
];
$array = array_values(array_reduce(
$array,
function (array $a, array $v) {
$k = "".$v['color']."";
if (!array_key_exists($k, $a)) {
$a[$k] = $v;
} else {
$a[$k]['val'] += $v['val'];
}
return $a;
},
array()
));
var_dump($array);
$arrays = array(
array(
"color"=>"red",
"val"=>4
),
array(
"color"=>"green",
"val"=>3
)
);
foreach ($color as $array) {
echo $array["color"].": ".$array["val"].";";
}
I have array inside array:
{
"0" => array("key" => "code", "id" => "4", "value" => "yes"),
"1" => array("key" => "parameter", "id" => "4", "value" => "0"),
"2" => array("key" => "code", "id" => "5", "value" => "no"),
etc...
}
This is what I want to do: I want to have one dimension array in which key would be "id" and value would be "value". However, I need to filter out entries whose key is "parameters". So, in this example, the final array should look like this:
{
"4" => "yes",
"5" => "no"
}
I just can't seem to figure out how to do this. Could you please help me a bit? I tried writing this foreach inside foreach but I just can't wrap my head around how to filter data.
foreach ($settings AS $key => $value) {
$id = null;
$value = null;
foreach ($value AS $key2 => $value2) {
// No idea how to filter out uneccesary entries and save the correct ones
}
$finalArray[$id] = $value;
}
This should do it :
$finalArray = array();
foreach ($settings as $setting) {
if ($setting['key'] != 'parameter') {
$finalArray[$setting['id']] = $setting['value'];
}
}
Assuming all your entries have keys 'key', 'id' and 'value'.
use array_column and array_filter like this, if you want to filter more keys add them to out_keys array :
<?php
$array = [
["key" => "code", "id" => "4", "value" => "yes"],
["key" => "parameter", "id" => "4", "value" => "0"],
["key" => "code", "id" => "5", "value" => "no"]
];
$out_keys = ['parameter'];
$result = array_column(array_filter($array, function($item) use($out_keys) {
return !in_array($item['key'], $out_keys);
}), 'value', 'id');
echo "<pre>";
print_r($result);
output:
Array
(
[4] => yes
[5] => no
)
Assuming $data is your starting array, the code below will output what you want in $result
$result = [];
foreach(array_filter($data, function($el){return $el['key']!='parameter';}) as $el){
$result[$el['id']] = $el['value'];
}
Live demo
I have two multidimensional dynamic arrays like so in PHP:
$old = array(
"meta_data" => array(
"page_title" => "Test1",
"page_description" => "Test2",
"page_keywords" => "Test3"
),
"content" => array(
"page_header_one" => "1",
"page_content_one" => "2",
"page_header_two" => "3",
"page_content_two" => "4",
"page_header_three" => "5"
),
);
$new = array(
"meta_data" => array(
"page_title" => "Test1",
"page_description" => "Test2",
"page_keywords" => "Test3324"
),
"content" => array(
"page_header_one" => "124",
"page_content_one" => "243",
"page_header_two" => "343"
),
);
I'm struggling to compare these as they're dynamic, e.g. the keys change. What I'm aiming to do is compare the arrays, find out what's changed, leave out keys that don't match, and only add the changes to the new array.
The only things that will be constant are "meta_data" and "content"
So for example in $old we have 5 items in the content array, but in the $new array we only have 3 items (3 changed items), so the new array would have 3 content items.
Is there a way to do this I can't for the life in me figure out how?
Final array should look like so:
$final = array(
"meta_data" => array(
"page_keywords" => "Test3324"
),
"content" => array(
"page_header_one" => "124",
"page_content_one" => "243",
"page_header_two" => "343"
),
);
Maybe it can be done easier, but I think this will do the job:
<?php
$newArray = [];
foreach($old['meta_data'] as $key => $value) {
if(array_key_exists($key, $new['meta_data'])) {
if($new['meta_data'][$key] == $old['meta_data'][$key]) {
$newArray['meta_data'][$key] = $value;
}
}
}
foreach($old['content'] as $key => $value) {
if(array_key_exists($key, $new['content'])) {
if($new['content'][$key] == $old['content'][$key]) {
$newArray['content'][$key] = $value;
}
}
}
You can also make the top key dynamic, so content and meta_data are dynamic as well, like this:
foreach($old as $topKey => $subArray) {
if(array_key_exists($topKey, $new)) {
foreach($subArray as $key => $value) {
if(array_key_exists($key, $new[$topKey])) {
if($new[$topKey][$key] == $old[$topKey][$key]) {
$newArray[$topKey][$key] = $value;
}
}
}
}
}
I haven't tested it. But I think you'll get the gist.
The only things that will be constant are "meta_data" and "content"
Given this thing in mind, the solution to your problem would be to use array_diff() function like this:
$final = array();
$final['meta_data'] = array_diff($new['meta_data'], $old['meta_data']);
$final['content'] = array_diff($new['content'], $old['content']);
// display $final array
var_dump($final);
Here's a live demo
You can simply use the PHP array_diff() function.
$final = array_diff($new, $old);
I have two arrays that look like this:
(this one is ordered by value_max)
$max_values = [
["name" => "john", "id" => 5, "value_max" => 500],
["name" => "john", "id" => 3, "value_max" => 200],
...
];
$min_values = [
["name" => "john", "id" => 5, "value_min" => 100],
["name" => "john", "id" => 3, "value_min" => 150],
...
];
And I need to have a final array like this:
(This one stills need to be ordered by value_max, so I assume I could just overwrite the first array with the calculations done with the second)
$max_and_difference_values = [
["name" => "john", "id" => 5, "value_max" => 500, "difference_value" => 400],
["name" => "john", "id" => 3, "value_max" => 200, "difference_value" => 50 ],
...
];
My question is pretty straight: What is the best/effective way to run through both first arrays and build the last one. Assuming that the size of the arrays can be of around 150 elements.
To avoid looping through all arrays repeatedly, index one array by the field you want to merge on, i.e. the 'id' key:
$second = array_combine(array_map(function ($i) { return $i['id']; }, $second_array), $second_array);
Then looping through the other and comparing the values is pretty easy:
$third = array();
foreach ($first_array as $i) {
$third[] = $i + array('difference_value' => $i['value_max'] - $second[$i['id']]['value_min']);
}
If it's guaranteed that both arrays will have exactly matching keys, you don't even need the first step and just go by already existing keys.
This will sort your array. So you can sort the second array.
<?php
$min_values = array(
array("name" => "john", "id" => 5, "value_min" => 100),
array("name" => "john", "id" => 3, "value_min" => 150),
);
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
aasort($min_values,"id");
echo "<pre>";
print_r($min_values);
echo "</pre>";
?>
And then you can use the logic what Alessandro Minoccheri mentioned.
$arr = array();
for ($i=0; $i<count($first_array);$i++){
$arr['name'] = $first_array[$i]['name'];
$arr['id'] = $first_array[$i]['id'];
$arr['value_max'] = $first_array[$i]['value_max'];
$arr['difference_value'] = $first_array[$i]['value_max']-$second[$i['id']]['value_max'] ;
}
As you have not shared how the second array with the minimum values is ordered (in itself and relative to the maximum values array), I'd say, index the minimum values by the id entry and then do the calculation in a second iteration. It should be fast enough, 150 elements is just "nothing":
$max_values = [
["name" => "john", "id" => 5, "value_max" => 500],
["name" => "john", "id" => 3, "value_max" => 200],
];
$min_values = [
["name" => "john", "id" => 5, "value_min" => 100],
["name" => "john", "id" => 3, "value_min" => 150],
];
$min_values_by_id = [];
foreach($min_values as $min) {
$min_values_by_id[$min['id']] = $min['value_min'];
}
$max_and_difference_values = $max_values;
foreach($max_and_difference_values as &$entry)
{
$entry['difference_value'] = $entry['value_max'] - $min_values_by_id[$entry['id']];
}
unset($entry);
print_r($max_and_difference_values);
This is just a straight forward example, nothing fancy. Demo
the following works for me. I'm not a PHP expert though, i.e. I'm not so familiar with cloning (note the empty clone array). Note that it only copies existing properties (if one array does contain a min_value and another does only contain a some other key)
In essence
function convertArr( $arr ) {
// Easy referencing without having to search through the arrays more than once for a matching id
$new_arr = array();
foreach( $arr as $array ) {
$new_arr[ $array['id'] ] = cloneArr( $array );
}
return $new_arr;
}
function merge( $arr1, $arr2 ) {
$convertedArr1 = convertArr( $arr1 );
$convertedArr2 = convertArr( $arr2 );
$arr = array();
// Based on the ordered array
foreach( $convertedArr1 as $array ) {
$id = $array['id'];
$tempArr = array();
foreach( $convertedArr1[ $id ] as $k => $v ) {
$tempArr[ $k ] = $v;
}
foreach( $convertedArr2[ $id ] as $k => $v ) {
$tempArr[ $k ] = $v;
}
array_push( $arr, $tempArr );
}
return $arr;
}
Full example:
<?php
//$arr1 = [ ["name" => "john", "id" => 5, "value_max" => 500], ["name" => "john", "id" => 3, "value_max" => 200] ];
//$arr2 = [ ["name" => "john", "id" => 5, "value_min" => 100], ["name" => "john", "id" => 3, "value_min" => 150] ];
$arr1 = array(
array( "name" => "john", "id" => 5, "value_max" => 500 ),
array( "name" => "john", "id" => 3, "value_max" => 200 )
);
$arr2 = array(
array( "name" => "john", "id" => 5, "value_min" => 100 ),
array( "name" => "john", "id" => 3, "value_min" => 150 )
);
function neatPrint( $arr ) {
echo "<pre>";
print_r( $arr );
echo "</pre>";
}
echo "<h2>Before</h2>";
neatPrint( $arr1 );
neatPrint( $arr2 );
echo "<hr/>";
function cloneArr( $old ) {
// I dunno how to properly clone
return $old;
}
function convertArr( $arr ) {
// Easy referencing without having to search through the arrays more than once for a matching id
$new_arr = array();
foreach( $arr as $array ) {
$new_arr[ $array['id'] ] = cloneArr( $array );
}
return $new_arr;
}
function merge( $arr1, $arr2 ) {
$convertedArr1 = convertArr( $arr1 );
$convertedArr2 = convertArr( $arr2 );
$arr = array();
// Based on the ordered array
foreach( $convertedArr1 as $array ) {
$id = $array['id'];
$tempArr = array();
neatPrint( $convertedArr1[ $id ] );
neatPrint( $convertedArr2[ $id ] );
foreach( $convertedArr1[ $id ] as $k => $v ) {
$tempArr[ $k ] = $v;
}
foreach( $convertedArr2[ $id ] as $k => $v ) {
$tempArr[ $k ] = $v;
}
array_push( $arr, $tempArr );
}
echo "<h2>Result</h2>";
return $arr;
}
echo "<h2>Loopthrough</h2>";
neatPrint( merge( $arr1, $arr2 ) );
?>
Let me know what you think :-)
if the element are in order try this code:
$arr = array();
for ($i=0; $i<count($first_array);$i++){
$arr['name'] = $first_array[$i]['name'];
$arr['id'] = $first_array[$i]['id'];
$arr['value_max'] = $first_array[$i]['value_max'];
$arr['difference_value'] = $first_array[$i]['value_max']-$second[$i['id']]['value_max'] ;
}