Remove array with single element from assosiative array - php

I have an array
Array
(
[0] => Array
(
[0] => 96
[1] => ML based
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
I want to remove
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
from this array
I tried:
$data = array_map('array_filter', $rowData);
unset($data[0][0]);
Expected output:
Array
(
[0] => Array
(
[0] => 96
[1] => ML based )
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
any help would be Appreciated .

array_filter() will work. Try -
array_filter($array, function ($a) {
return count($a[0]) == 3; // return array with 3 elements only
});
Working code

Related

How to ordering or sort array by keys in ascending using PHP?

My problem is that I want to sort array keys by ascending and I am googling last 2 days what I am getting the solution is uksort, ksort but it's not give the exact solution what am I want.I feel great full for helping to solve this issue. My output array is:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
)
and we need array is in:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
)

Merging array keys in multidimentional array does not give desired output

This is my generated array
i have tried array_merge_recrusive but not successful.
I am doing program it should generate this array
Array
(
[0] => Array
(
[competancy] => Array
(
[0] => 1
)
)
[1] => Array
(
[assistment] => Array
(
[0] => 0
)
)
[2] => Array
(
[competancy] => Array
(
[0] => 3
)
)
[3] => Array
(
[competancy] => Array
(
[0] => 4
)
)
[4] => Array
(
[assistment] => Array
(
[0] => 0
)
)
[5] => Array
(
[competancy] => Array
(
[0] => 6
)
)
required
[6] => Array
(
[competancy] => Array
(
[0] => 7
)
)
[7] => Array
(
[assistment] => Array
(
[0] => 0
)
)
)
I want following output array
I have tried lot of but not find this type of output
Array
(
[competancy] => Array
(
[0] => 1
)
[assistment] => Array
(
[0] => 0
)
[competancy] => Array
(
[0] => 3
[0] => 4
)
[assistment] => Array
(
[0] => 0
)
[competancy] => Array
(
[0] => 6
[0] => 7
)
[assistment] => Array
(
[0] => 0
)
)
Try to merge array with splat :
print_r(array_merge(...$array));

PHP array slice multidimensional array

I have an multidimensional array but need to make it smaller.
This is an easy question I believe. I need to remove 1 array in
jsonresult array, the first one but preserve the other in the next array. I have tried array_splice but it only keeps one.
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
)
[1] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889722
)
[1] => Array
(
[id] => 14389711
)
[2] => Array
(
[id] => 14869329
)
)
)
)
Would like to get:
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
[3] => Array
(
[id] => 14889722
)
[4] => Array
(
[id] => 14389711
)
[5] => Array
(
[id] => 14869329
)
)
)
Try this code. This may not be the correct method but it gives what you need. (As I understand from your question)
//creating a sample array similar to one you given in question.
$arr_test['searchword'] = 'search word';
$arr_test['jsonresult'] = array(array(array('id'=>14889770),array('id'=>14889720)),array(array('id'=>14889780),array('id'=>14889790)));
//creating new array
$arr_new = array();
//formatting array as you needed it
foreach($arr_test['jsonresult'] as $arr_jsonresult){
foreach($arr_jsonresult as $jsonresult){
$arr_new['jsonresult'][] = $jsonresult;
}
}
//overwriting the specific array key
$arr_test['jsonresult'] = $arr_new['jsonresult'];
//checking output
echo '<pre>';
print_r($arr_test);
This code produces the following output
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14889720
)
[2] => Array
(
[id] => 14889780
)
[3] => Array
(
[id] => 14889790
)
)
)

PHP search key in array and return its value?

I would like to search key in multidimensional array and i would like to get corrosponding value associated with that key.
For e.g.
I would like to extract following texts from below array :
SENT AT 12.08ms
And the text
sample id 41962
following is an array print_r() output :
Array
(
[0] => Array
(
[VERSION] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => 3.0
)
)
)
)
[SAMPLE] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample id 41962
)
)
)
)
[TSAM] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample group 141
)
[1] => Array
(
[0] => ¯
)
[2] => Array
(
[0] => sample batch 81
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
)
)
[STATUS] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
[TYPE] => Array
(
[0] => CART
)
)
[value] => Array
(
[0] => Array
(
[0] => SENT AT 12.08ms
)
)
)
)
)
)
Can somebody provide me optimized code for above problem. The multidimensional array contains more than 5000 to 10000 arrays.
Please, see if my function works for you:
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
Use:
$array = array('array1'=>array('array2'=>array('find_some_key'=>'some_value')));
echo get_value_by_key($array,'find_some_key'); // outputs: some_value
If all the array keys have the same structure the following code should work:
foreach($array as $item){
$sentat = $item['STATUS'][0]['value'][0][0];
$sample = $item['SAMPLE'][0]['value'][0][0];
}
More detailed information would help us to provide you more tips :)

Conditionally replace values in multidimensional PHP array

There has been a few other questions regarding replacing values in multidimensional array on here, but I didn't find anything regarding what I was trying to do, exactly, per se.
I have an array that I get from an API and I need to update a few values based on other values in the array tree before sending the API payload to the browser.
In the array when the sale[0] === true I am looking for some logic to then replace the [price][0] value with a corresponding new sale price.
Using the foreach, I can easily loop through the each of the nodes in the array, but I am unsure once I loop through the array, how I can update the original array with new price if/when sale node === true.
Array
(
[response] => Array
(
[0] => Array
(
[results] => Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[id] => Array
(
[0] => 846471605959
)
[title] => Array
(
[0] => Test Item 846471605959
)
[imageURL] => Array
(
[0] => https://foo/bar/images/846471605959.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/846471605959
)
[price] => Array
(
[0] => 799.00
)
[sale] => Array
(
[0] => true
)
)
[1] => Array
(
[id] => Array
(
[0] => 414953260545
)
[title] => Array
(
[0] => Test Item 414953260545
)
[imageURL] => Array
(
[0] => https://foo/bar/images/414953260545.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/414953260545
)
[price] => Array
(
[0] => 199.00
)
[sale] => Array
(
[0] => false
)
)
[2] => Array
(
[id] => Array
(
[0] => 684865199812
)
[title] => Array
(
[0] => Test Item 684865199812
)
[imageURL] => Array
(
[0] => https://foo/bar/images/684865199812.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/684865199812
)
[price] => Array
(
[0] => 699.00
)
[sale] => Array
(
[0] => false
)
)
[3] => Array
(
[id] => Array
(
[0] => 987800965761
)
[title] => Array
(
[0] => Test Item 987800965761
)
[imageURL] => Array
(
[0] => https://foo/bar/images/987800965761.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/987800965761
)
[price] => Array
(
[0] => 499.00
)
[sale] => Array
(
[0] => true
)
)
[4] => Array
(
[id] => Array
(
[0] => 005457536677
)
[title] => Array
(
[0] => Test Item 005457536677
)
[imageURL] => Array
(
[0] => https://foo/bar/images/005457536677.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/005457536677
)
[price] => Array
(
[0] => 99.00
)
[sale] => Array
(
[0] => false
)
)
)
)
)
}
)
)
Use a reference variable for the foreach iteration variable, then you can update the element in place.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
if ($item['sale'][0]) {
$item['price'][0] = $new_price;
}
}
If you also need to loop through all the elements in the sale array, add a nested loop.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
foreach ($item['sale'] as $i => $sale) {
if ($sale) {
$item['price'][$i] = $new_price;
}
}
}

Categories