0

I am trying to build a form where user inputs a data from a string and the form will get subsets of data through json src based on the input and create a multidimensional array and save it in database. If the input was previously added to database, I dont want it to append in the array again.

Here is how my code looks:

//SET UP the Array
$thisquery_themes[] = array(
    strtolower($themename) => array(
        "author"=>$data['Author'],
        "desc"=>$data['Description'],
        "screenshot"=> $screenshot,
        "link"=> $data['URI'],
        "count"=> 1
    )
);

//Get Previously saved data
$existing_themes = get_option('top_themes');

if(!empty($existing_themes)){
    foreach ($existing_themes as $group){
        foreach(array_keys($group) as $key) {
            if($group[strtolower($themename)] == strtolower($themename)){
                unset($group[$key][strtolower($themename)]);
            }
        }
    }

    $total_themes= array_merge($existing_themes , $thisquery_themes);
    update_option('top_themes', $total_themes);
} else {
    update_option('top_themes', $thisquery_themes);
}

its not. if the key exist in the array, the data is still being added in the array:

Array ( 
[0] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 1 
        ) 
) 
[1] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 1 
        ) 
) 
[2] => Array ( 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla
           [count] => 1 
        ) 
) 
[3] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 1 
        ) 
)
[4] => Array ( 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla 
           [count] => 1 
        ) 
)

But I want it to be like this(Notice how "count" field value added up. let me know if its possible ):

Array ( 
[0] => Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 3 
        ) 
) 

[1] => Array ( 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla 
           [count] => 2 
        ) 
) 

Any help would be greatly appreciated. Thanks

1 Answer 1

1

Why are you using an array within an array if it's only going to hold one value? Unless that's what's being thrown at you, your structure could easily look like:

Array ( 
        [towfiq-i._v5] => Array ( 
           [author] => Towfiq I. 
           [desc] => Towfiq I. official website. 
           [count] => 3 
        ) 
        [wp-bangla] => Array ( 
           [author] => Ifty Rahman 
           [desc] => A website template for wpbangla 
           [count] => 2 
        ) 
) 

Then your world would become a lot easier because you could check to see if the array element exists by using isset($existing_themes[strtolower($themename)]) (as an example.)

If you can't change how you receive $existing_themes, you can reformat the data like this:

$existing_themes_mod = array();
foreach ($existing_themes as $t) {
    $existing_themes_mod[key($t)] = reset($t);
}

This should give you enough of a strategy to work with an be able to run an "update" on existing entries instead of an appendage. If you need help writing it, please show me your attempt first.

EDIT - Thanks for pasting your code. You should watch out for all that whitespace by the way... here's a better way to achieve your goal

if (!empty($existing_themes)) {
    if (isset($existing_themes[strtolower($themename)])) {
        if (isset($existing_themes[strtolower($themename)]['count'])) {
            $existing_themes[strtolower($themename)]['count'] = $existing_themes[strtolower($themename)]['count'] + 1;
        } else {
            $existing_themes[strtolower($themename)]['count'] = 1;
        }                                              
        $thisquery_themes = array();
    }                               
    $total_themes= array_merge($existing_themes , $thisquery_themes);
    update_option('top_themes', $total_themes);                       
} else {
    update_option('top_themes', $thisquery_themes);
}

The reason your attempt didn't work is because when you try to change the $value by incrementing it by 1, it doesn't modify the original array. When you use foreach like that, you need to iterate by reference or else you can't modify the value. You're making too much work for yourself!

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

1 Comment

Excellent. Dont know why I was making the array structure that complicated.. thanks for pointing that out. I took your sugestion and solved the duplicate entry issue. the only issue I am having is, update the count field. Here is how my code looks: pastebin.com/qBSH91Js . Thanks

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.