Useful php functions
Rus
Eng
Полезные php функции

All English-language materials have been translated fully automatically using the Google service

I've been collecting this hodgepodge for a long time and I haven't found most of the authors / don't remember. If you find out your code, then write - I will add your authorship

Search for key matches in a multidimensional array

  function search ($ array, $ key, $ value)
{
    $ results = array ();

    if (is_array ($ array)) {
        if (isset ($ array [$ key]) && $ array [$ key] == $ value) {
            $ results [] = $ array;
        }

        foreach ($ array as $ subarray) {
            $ results = array_merge ($ results, search ($ subarray, $ key, $ value));
        }
    }

    return $ results;
}

$ arr = array (0 => array (id => 1, name => "cat 1"),
             1 => array (id => 2, name => "cat 2"),
             2 => array (id => 3, name => "cat 1"));

print_r (search ($ arr, 'name', 'cat 1'));  

Convert StdClassObject to Array

  $ array = json_decode (object, True);  

Split array by index

  // an array
$ array = array (1, 2, 3, "products", 4, 5);

// $ offset = 3
$ offset = array_search ("products", $ array);

// array (4,5)
$ last_batch = array_slice ($ array, ($ offset + 1));

// array (1,2,3)
$ first_batch = array_slice ($ array, 0, $ offset);  

Remove the last element of the array

array_pop($stack) 

Determine the last index of the array

  $ array = array (
    'first' => 123,
    'second' => 456,
    'last' => 789,
);

end ($ array); // move the internal pointer to the end of the array
$ key = key ($ array); // fetches the key of the element pointed to by the internal pointer

var_dump ($ key);  

What to do if json_encode doesn't work

  function safe_json_encode ($ value, $ options = 0, $ depth = 512) {
    $ encoded = json_encode ($ value, $ options, $ depth);
    switch (json_last_error ()) {
        case JSON_ERROR_NONE:
            return $ encoded;
        case JSON_ERROR_DEPTH:
            return 'Maximum stack depth exceeded'; // or trigger_error () or throw new Exception ()
        case JSON_ERROR_STATE_MISMATCH:
            return 'Underflow or the modes mismatch'; // or trigger_error () or throw new Exception ()
        case JSON_ERROR_CTRL_CHAR:
            return 'Unexpected control character found';
        case JSON_ERROR_SYNTAX:
            return 'Syntax error, malformed JSON'; // or trigger_error () or throw new Exception ()
        case JSON_ERROR_UTF8:
            $ clean = utf8ize ($ value);
            return safe_json_encode ($ clean, $ options, $ depth);
        default:
            return 'Unknown error'; // or trigger_error () or throw new Exception ()

    }
}

function utf8ize ($ mixed) {
    if (is_array ($ mixed)) {
        foreach ($ mixed as $ key => $ value) {
            $ mixed [$ key] = utf8ize ($ value);
        }
    } else if (is_string ($ mixed)) {
        return utf8_encode ($ mixed);
    }
    return $ mixed;
}  

Thanks to Konstantin for the function from the site How to solve JSON_ERROR_UTF8 error in php json_decode?

Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number