I have an array, I just print it as-
print_r($data);
It shows output as-
Array
(
[0] => Array
(
[0] => Title
[1] => Featured Image
[2] => Catagories
[3] => Tags
[4] => Content
)
[1] => Array
(
[0] => title 1
[1] => img1.jpg
[2] => cat 1
[3] => tag 1
[4] => post 1 content
)
[2] => Array
(
[0] => title 2
[1] => img2.jpg
[2] => cat2
[3] => tag 2
[4] => post 2 content
)
[3] => Array
(
[0] => title 3
[1] => img3.jpg
[2] => cat3
[3] => tag3
[4] => post 3 content
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[6] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
)
I just want to remove blank or null values from array.
I tried array_dif() and array_filter() but still I couldn't remove null values.
How is this possible?
Thanks.
You can loop through the array, look for empty variables and use unset to remove them.
This code will loop through and check if the length of the first value in each array is at least one character long and unset it if its not.
<?php
foreach($data as $key => $value) {
if(!isset($value[0][0]))
unset($data[$key]);
}
This code will loop through the array in a similar way, except to check every value of every array to determine if its parrent array should be kept or left to be unset.
<?php
foreach($data as $key => $values) {
foreach($values as $value) {
if(isset($value[0]))
continue 2;
}
unset($data[$key]);
}
You should use array_filter with a function that also runs array_filters against the subarrays and returns false if the filtered subarrays become empty.
<?php
$array = Array(
Array(1, 2, 3),
Array(null, null, null),
Array(false, false, false),
Array(3, 2, 1)
);
$filtered = array_filter($array, function($elem) {
return count(array_filter($elem));
});
print_r($filtered);
?>
Related
I have two arrays, $full_list and $only_titles with matching value based on first key.
$full_list:
Array
(
[0] => Array
(
[0] => samsung-a40
[1] => Samsung A40 Phone
[2] =>
[3] => 21
[4] => 334234
[5] => 0
)
[1] => Array
(
[0] => samsung-a72
[1] => Galaxy A72 Phone
[2] =>
[3] => 230
[4] => 239049
[5] => 0
)
[2] => Array
(
[0] => lg-k61
[1] => LG K61 Phone
[2] =>
[3] => 22
[4] => 249582
[5] => 0
)
etc...
$only_titles:
Array
(
[0] => Array
(
[0] => samsung-a40
[1] => Black Phone Case For Samsung A40
)
[1] => Array
(
[0] => lg-k61
[1] => Red Case LG K61 Phone
)
[2] => Array
(
[0] => samsung-a72
[1] => Folding Phone Case for Galaxy A72 Phone
)
etc...
I want to take value from second key from $only_title array (its basicaly long product title) and replace it in the $full_list array:
$final_result:
Array
(
[0] => Array
(
[0] => samsung-a40
[1] => Black Phone Case For Samsung A40 // title from $only_titles
[2] =>
[3] => 21
[4] => 334234
[5] => 0
)
[1] => Array
(
[0] => samsung-a72
[1] => Folding Phone Case for Galaxy A72 Phone // title from $only_titles
[2] =>
[3] => 230
[4] => 239049
[5] => 0
)
[2] => Array
(
[0] => lg-k61
[1] => Red Case LG K61 Phone // title from $only_titles
[2] =>
[3] => 22
[4] => 249582
[5] => 0
)
etc...
I have tried array_replace_recursive(), but the problem with this is that it expects that the order of subarrays are the same. But as you can see in both arrays the second subarrays dont match. With array_replace_recursive() i get the second subarray wrong:
[1] => Array
(
[0] => samsung-a72
[1] => Red Case LG K61 Phone // this is wrong
[2] =>
[3] => 230
[4] => 239049
[5] => 0
)
How can I check if values from first key match?
You can utilize array_map and array_search for your task.
$items = array_map(function($item) use ($only_titles) {
$index = array_search($item[0], array_column($only_titles, 0));
if($index === false) return;
$item[1] = $only_titles[$index][1];
return $item;
}, $full_list);
array_map() takes $full_list and iterate each item as $item,
then for each item, we look for the matching title with array_search()
It will return an index from $only_titles.
We can use $index to reference and assign the long title to item.
The result are in $items
This script will help you to solve your problem
<?php
$full_array = [
[
'samsung-a40',
'Samsung A40 Phone',
null,
21,
334234,
0,
],
[
'samsung-a72',
'Galaxy A72 Phone',
null,
230,
239049,
0,
],
];
$only_titles = [
[
'samsung-a40',
'Black Phone Case For Samsung A40',
],
[
'samsung-a72',
'Red Case LG K61 Phone ',
],
];
$formattedOnlyTitles = [];
foreach ($only_titles as $value) {
$formattedOnlyTitles[$value[0]] = $value[1];
}
$fullArrayWithNewTitle = [];
foreach ($full_array as $value) {
if (isset($formattedOnlyTitles[$value[0]])) {
$value[1] = $formattedOnlyTitles[$value[0]];
}
$fullArrayWithNewTitle[] = $value;
}
print_r($fullArrayWithNewTitle);
output
Array
(
[0] => Array
(
[0] => samsung-a40
[1] => Samsung A40 Phone
[2] =>
[3] => 21
[4] => 334234
[5] => 0
)
[1] => Array
(
[0] => samsung-a72
[1] => Galaxy A72 Phone
[2] =>
[3] => 230
[4] => 239049
[5] => 0
)
)
First I change the format of the title_only var to make is easy to work with it in the next foreach
$formattedOnlyTitles = [];
foreach ($only_titles as $value) {
$formattedOnlyTitles[$value[0]] = $value[1];
}
In the next foreach I just check the title with my new formatted title_only array
if (isset($formattedOnlyTitles[$value[0]])) {
$value[1] = $formattedOnlyTitles[$value[0]];
}
And then change the title if I need
I've a muti dimensional array. I want to concatenate 2 strings separately for 2 array values and the 2 strings should not be concatenated for a single value. I want CM and PM concatenated 2 times any where in the array. I've tried looping the array and generating array_rand but i generates only once. Any help is much appreciated. Below is one example of what am achieving.
Thing am trying to achieve
Concatenate "PM" and "CM" string in one set of array and same value can't be CM and PM
Every array should have PM and CM concatenated
1 "Name" value should have minimum 1 CM and PM and Maximum 2 CM and PM
For example: I've the below multi dimensional array.
Array
(
[0] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C
[3] => Name-4
[4] => Name-5
)
[1] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C
[3] => Name-4
[4] => Name-5
)
[2] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C
[3] => Name-4
[4] => Name-5
)
[3] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C
[3] => Name-4
[4] => Name-5
)
[4] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C
[3] => Name-4
[4] => Name-5
)
)
After concatenating
Array
(
[0] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C["PM"]
[3] => Name-4["CM"]
[4] => Name-5
)
[1] => Array
(
[0] => Name-A
[1] => Name-B["PM"]
[2] => Name-C
[3] => Name-4["CM"]
[4] => Name-5
)
[2] => Array
(
[0] => Name-A["PM"]
[1] => Name-B
[2] => Name-C
[3] => Name-4
[4] => Name-5["CM"]
)
[3] => Array
(
[0] => Name-A["PM"]
[1] => Name-B["CM"]
[2] => Name-C
[3] => Name-4
[4] => Name-5
)
[4] => Array
(
[0] => Name-A
[1] => Name-B
[2] => Name-C["CM"]
[3] => Name-4
[4] => Name-5["PM"]
)
)
Sorry I didn't understand at first.
Given $your_array:
// Generate an array where the names are the key, and assign a zero value to PM (0) and CM (1) in a sub array
$ar = array_fill_keys($your_array[0], array (0 => 0, 1 => 0));
//$array the sub_value, $add the case PM ou CM, $exclusion is the key used the first time
function rand_in_array($array, $add, $ar, $exclusion)
{
// Select a random key
$arr_key = array_rand($array, 1);
if($ar[$array[$arr_key]][$add] < 2 && ($arr_key != $exclusion))
{
return $arr_key;
}
return rand_in_array($array, $add, $ar, $exclusion);
}
for($i=0; $i<count($your_array);$i++)
{
$arr_key_pm = rand_in_array($your_array[$i], 0, $ar, 99);
$ar[$your_array[$i][$arr_key_pm]][0]++;
$arr_key_cm = rand_in_array($your_array[$i], 1, $ar, $arr_key_pm);
$ar[$your_array[$i][$arr_key_cm]][1]++;
$your_array[$i][$arr_key_pm] .= "PM";
$your_array[$i][$arr_key_cm] .= "CM";
}
It is ugly but it works :)
Somewhere should existe someone able to make more aesthetic..
for($i=0; $i<count($your_array);$i++)
{
$arr_keys = array_rand($your_array[$i], 2);
$your_array[$i][$arr_keys[0]] .= "PM";
$your_array[$i][$arr_keys[1]] .= "CM";
}
Should do the work.
I assume your inner array index is starting from zero. So generate the random index between 0 to your ( inner_array_size - 1). Then assign the value in the array's reference variable withing the loop.
foreach ($arr as &$value) {
$randomIndex = array_rand(range(0, (count($value) -1) ), 2);
$value[$randomIndex[0]] .= ' ["CM"]';
$value[$randomIndex[1]] .= ' ["PM"]';
}
I am trying to fetch values from few - dynamically created HTML FORM & in action file, i am grabbing those values via $_POST. Please consider humbly, that i am using array in dynamic inputs like :
<input name="array1[]" />
<input name="array2[]" />
<input name="array3[]" />
So, that, after FORM SUBMIT, in action file, its giving :
$a = Array
(
[0] => 04/21/2017
[1] => 04/19/2017
[2] => 04/25/2017
[3] => 04/25/2017
[4] => 10/25/2017
)
$b= Array
(
[0] => 11
[1] => 34
[2] => 12
[3] => 12
[4] => 2
)
$c= Array
(
[0] => fghgthg
[1] => ggfg
[2] => fgfgfdgf
[3] => fgfdgdgfgdh
[4] => rgrgfgf
)
Now, m having troubles in arranging this way :
$FinalArray = array(
array($a[0], $b[0], $c[0]),
array($a[1], $b[1], $c[1]),
array($a[2], $b[2], $c[2]),
array($a[3], $b[3], $c[3]),
......last line without comma
);
And submitting in mysql table, so that i can easily retrieve it like :
step-1 : $FinalArray[0];
step-2 : $FinalArray[1];
.......goes on
Thanks, for help, in Advance.
This simple foreach help you in achieving your desired output.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$a = Array
(
0 => "04/21/2017",
1 => "04/19/2017",
2 => "04/25/2017",
3 => '04/25/2017',
4 => "10/25/2017"
);
$b = Array
(
0 => 11,
1 => 34,
2 => 12,
3 => 12,
4 => 2
);
$c = Array
(
0 => "fghgthg",
1 => "ggfg",
2 => "fgfgfdgf",
3 => "fgfdgdgfgdh",
4 => "rgrgfgf",
);
$result=array();
foreach($a as $key => $value)
{
$result[]=array($value,$b[$key],$c[$key]);
}
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 04/21/2017
[1] => 11
[2] => fghgthg
)
[1] => Array
(
[0] => 04/19/2017
[1] => 34
[2] => ggfg
)
[2] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfgfdgf
)
[3] => Array
(
[0] => 04/25/2017
[1] => 12
[2] => fgfdgdgfgdh
)
[4] => Array
(
[0] => 10/25/2017
[1] => 2
[2] => rgrgfgf
)
)
I have an array, let call it $mainArray, which looks like this: -
Array
(
[1] => Array
(
)
[5] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
[80] => Array
(
[0] => 20
[1] => 40
[2] => 50
[3] => 60
)
[777] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
[666] => Array
(
[0] => 1234
[1] => 5678
[2] => 20
[3] => 9865
)
[555] => Array
(
[0] => 111
[1] => 222
[2] => 333
[3] => 444
)
)
What I want to do is create 2 new arrays: -
1) Where values are equal to the key names of $mainArray, but only those where the sub-array (if there is one) contains the value "20" somewhere in it. For example my new array (call it $arrayOne) will be [0] => 5, [1] => 80, [2] => 666.
2) Similar to above, but where there's either no sub-array or, if there is, it doesn't include "20" as a value. So that (call it $arrayTwo) would be [0] => 1, [1] => 777, [2] =>555.
I've tried loads of for each loops and even a little RecursiveIteratorIterator (whatever that is!) but can't seem to reference keys and values in the way that I need to. Any help would be much appreciated!
Will this do?:
<?php
foreach( $mainArray as $mKey => &$mVal )
{
if( in_array( 20, $mVal ) )
{
$arrayOne[] = $mKey;
}
else
{
$arrayTwo[] = $mKey;
}
}
I trust you can create a function which would check if array contains 20 as it's value or not. Let's call this function has20.
You two new arrays would then be array_filter($mainArray, 'has20') and array_filter($mainArray, function ($x) {return !has20($x);})
You can do it like this:
$newArray = array();
foreach($mainArray as $key => $subArray) {
if (in_array(20, $subArray)) {
$newArray[] = $key;
}
}
I have the following array:
Array
(
[0] => BCD
[1] => ACE
[2] => AHP
[3] => BGH
[4] => ART
[5] => COT
[6] => ARG
[7] => BGT
)
I need to match all elements whose first letter is in the following array:
Array
(
[0] => B
[1] => A
)
to get:
Array
(
[0] => ACE
[1] => AHP
[2] => BGH
[3] => ART
[4] => ARG
[5] => BGT
)
Short of looping through the whole array, how do I do this in PHP? Is there a built-in PHP array function for this or a combination of so? The order does not matter for both keys and values of the resulting array. Thanks much.
You can use array_filter for these operations:
$array = array('CBD', 'NHN', 'NHP', 'WHC', 'NND', 'CQN', 'WST', 'WVT');
$whitelist = array('W', 'N');
$filtered = array_filter($array, function($val) use ($whitelist) {
// check if first letter is in the whitelist array
if (in_array($val{0}, $whitelist)) {
return $val;
}
return false;
});
Output:
Array
(
[1] => NHN
[2] => NHP
[3] => WHC
[4] => NND
[6] => WST
[7] => WVT
)