2-dimensional Array - unset if not value - php

I have a 2-dimensional array and want to delete all elements, whose values are not "Name1". They should keep their index numbers (keys):
Array
(
[array001] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name1
)
[array002] => Array
(
[0] => Name2
[1] => Name1
[2] => Name4
)
[array003] => Array
....
)
will output
Array
(
[array001] => Array
(
[0] => Name1
[3] => Name1
)
[array002] => Array
(
[1] => Name1
)
[array003] => Array
....
)
Possible solutions could be achieved with a foreach loop, with preg_replace, when the array is converted into a string: $array = print_r($array,true);

none of them is working..
I found the solution by myself:
foreach($array as $key => $value) {
foreach($value as $innerkey => $innervalue){
if($innervalue != 'Name1'){
unset($array[$key][$innerkey]);
}
}
}

foreach($array as $key => $value) {
foreach ($value as $string) {
if ($string !== "Name1") {
unset($string);
}
}
}

try this:
function removeElementDifferentValue($array, $value){
foreach($array as $subKey => $val){
if($val != $value){
unset($array[$subKey]);
}
}
return $array;
}
$array = removeElementWithValue($array, 'Name1');

foreach($array as $key1 => $val1) {
foreach($val1 as $key2 => $val2) {
if(strcmp($val2,"Name1") != 0) {
unset($array[$key1][$key2]);
}
}
}

Related

How can I retrieve data from array

Am a beginner in yii2. In my program there is an array named Place. It contains the values like:
Array
(
[0] => Array
(
[0] => Aluva
[1] => Paravoor
)
[1] => Array
(
[0] => Ernakulam
[1] => Paravoor
)
[2] => Array
(
[0] => Aluva
[1] => Ernakulam
)
[3] => Array
(
[0] => Kottuvally
[1] => Paravoor
)
)
How can I retrieve each element from this array?
You can use the array_walk function
$total = [];
array_walk($sales, function($value) use(&$total) {
foreach ($value as $key => $arr) {
echo $arr. "<br>";
}
});
using foreach ?
foreach($array as $row){
foreach($row as $key => $val){
echo $val.'<br>';
}
}
Sample code -
<?php
$data = array(
0 => array(0=>'Aluva', 1=>'Paravoor'),
1 => array(0=>'Ernakulam', 1=>'Paravoor'),
2 => array(0=>'Aluva', 1=>'Ernakulam'),
3 => array(0=>'Kottuvally', 1=>'Paravoor')
);
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value1) {
echo '<br>'.$value1;
}
}
$var = $Place[0][1];
Where is $var is now "Paravoor"

Parsing an array in PHP after serializeArray() in JS

I receive this array in my PHP page after form serializeArray() done on a form in Javascript:
Array
(
[datas] => Array
(
[0] => Array
(
[name] => room_1
[value] => a
)
[1] => Array
(
[name] => room_2
[value] => b
)
[2] => Array
(
[name] => room_3
[value] => c
)
)
)
How can I parse it after during a foreach loop ?
Thanks.
I tried:
foreach ($datas as $key => $item) {
echo $item;
}
foreach ($array['datas'] as $item) {
echo $item['name'];
echo $item['value'];
}
If the name of the array is $array,
$arr = $array['datas'];
foreach($arr as $key => $val){
$name = $val['name'];
$value = $val['value'];
}
}

Find point in certain array values and replace with comma

I have array like this
Array
(
[0] => Array
(
[0] => Price
[1] => 308.5
[2] => 2009
)
[1] => Array
(
[0] => Price
[1] => 308.5
[2] => 2009
)
)
and i need to replace "." with "," in key [0][1], [1][1], [2][1] etc
This will work, notice how you have to use sub_array with a reference
foreach ($arr as &$sub_arr) {
foreach ($sub_arr as &$val) {
$val = str_replace('.', ',', $val);
}
}
try
foreach($arr as $v) {
foreach($v as $v1) {
$newarr[] = str_replace('.', ',', $v1);
}
}
print_r($newarr);

php array key unset if element is another element in a second array

I have a array named $new_items in the following format, with multiple items....
Array
(
[0] => Array
(
[id] => 66901
[weight] => 0.3000
[Price] => 14.1800
[category] => Array
(
[parent_id] => Array
(
[0] => 222
[1] => 1232
[2] => 1315
)
)
.......and (cont...)
[1] => Array
( ......
I have another array $data in following format:
Array
(
[174] => 67495
[253] => 67471
[278] => 67460
[323] => 67412
[390] => 67332
[600] => 67282
[738] => 67209
)
I wish to remove the parent in $new_items array where id is present in $data array.
My code is as follows:
foreach ($new_items as $key => $item) {
if ($item['id'] === $data) {
unset($new_items[$key]);
}
}
is not removing the parent.
But if i hardcode the id as follows:
if ($item['id'] === "67495"),
It is getting removed..
Please help me...
$data is an array, so you can't compare it with integer value! Use in_array instead..
foreach ($new_items as $key => $item) {
if (in_array($item['id'], $data)) {
unset($new_items[$key]);
}
}
foreach ($new_items as $key => $item)
{
if(array_search($item['id'], $data))
{
$key = array_search($item['id'], $data);
unset($data[$key]);
}
}

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

Categories