0

I am trying to create an array that will hold the only array that contains values.

Code below works well, but I get trouble if for ex, $array2 (but can array1 or array3) doesn't contain any value. In that case, I need to merge only array1 and array3.

$array3 = array_filter( array_map( function( $term ) {
    if ( ! $term = \Softing\Term::get( $term ) ) {
        return false;
    }

    return [
        'link' => $term->get_link(),
        'name' => $term->get_name(),
        'color' => $term->get_color(), 
        ]; 


}, $terms ) );


$formatted_terms[] = array_merge($array1, $array2, $array3);

Each if three arrays are formed on the same way as $array3, but some of them could be empty, no values. Those Arrays I dont want to merge. Want to create array only from arrays that holds value.

What is the easiest way to accomplish this.

I tried using

$formatted_terms[] = array_merge((array)$array1, (array)$array2, (array)$array3);

Any advice ?

7
  • 2
    What is the easiest way to accomplish this. No idea what you are trying to ask unless you show us example of input and expected ouput Commented Jan 28, 2020 at 14:37
  • @Marko did you have multiple arrays $array1,$array2,$array3,$array4?. Where are the $array1 - $array4 from (parent array)?. If it is from single array we can use loop. Commented Jan 28, 2020 at 14:43
  • I have updated question @B001ᛦ Commented Jan 28, 2020 at 14:48
  • @DroidDev Please check question now, I posted how I form arrays Commented Jan 28, 2020 at 14:49
  • There is no problem if one of the arrays is empty. Another thing is if one of the arrays is undefined or is not an array. Commented Jan 28, 2020 at 14:49

3 Answers 3

1

You can use array_filter() to remove empty array values. Since you have a multidimensional array, you may consider using array_map() in conjunction with array_filter().

Take the following for example:

$array1 = ['link'=>'foo', 'name'=>'bar', 'filter'=>'hello world'];

$array2 = false;

$array3 = ['link'=>'foo', 'name'=>'bar', 'filter'=>'hello world'];

$formatted_terms[] = array_merge((array)$array1, (array)$array2, (array)$array3);

$formatted_terms = array_map('array_filter',$formatted_terms);

echo '<pre>',print_r($formatted_terms),'</pre>';

https://www.php.net/manual/en/function.array-map.php

https://www.php.net/manual/en/function.array-filter.php

Sign up to request clarification or add additional context in comments.

1 Comment

I need to get at the end array, which I use to print out all values $formatted_terms[] , it doesnt work like this
0

Just check if theyre an array and if theyre not empty. If so, use array merge. If not, just do nothing.

$a_all_arrays = array($array1, $array2, $array3);
$a_merged     = array();
foreach($a_all_arrays as $arr) 
{
    if(is_array($arr) && !is_empty($arr)) 
    {
        $a_merged = array_merge($a_merged, $arr);
    }
}

Comments

0

Check the array if it has values before merging,

$array1=!empty($array1) && is_array($array1)?$array1:[];
$array2=!empty($array2) && is_array($array2)?$array2:[];
$array3=!empty($array3) && is_array($array3)?$array3:[];
$formatted_terms[] = array_merge($array1, $array2, $array3);

5 Comments

Does that really make any sense?
Why not, if $array2 has a empty value in it.
Why not, if $array2 has a string in it... If it contains string then it is not empty
empty variable\
An empty variable? What is that? However your code is just saying: if array is empty then array is empty, if not so then it is not empty... Makes no sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.