Remove String Value From Array PHP Example

Remove String Value From Array PHP Example

how to remove string value from an array in php, php remove string value from an array, remove string value from array php, remove all string value from an array php, remove string from array php

PHP Multidimensional Array Search by Key and Value with Examples

PHP Multidimensional Array Search by Key and Value with Examples

In this article, we would love to shows, how you can create your own function for searching PHP Multidimensional Array. Here we will take two examples for searching in the multidimensional array using custom created function.

PHP search a multidimensional array (Search By key and Value). Here we will learn how to search in the multidimensional array for value and return key.

Sometimes we need to search in an array or multidimensional array by key or value without using any function. This tutorial shows you, the fastest way to search in multidimensional array.

In this article, we would love to shows, how you can create your own function for searching Multidimensional Array. Here we will take two examples for searching in the multidimensional array using custom created function.

PHP Search Multidimensional Array

You have one array look like this:

$array = array(
    array(
        'id' => '100',
        'name' => 'Rock',
    ),
    array(
        'id' => '105',
        'name' => 'Test',
    ),
    array(
        'id' => '109',
        'name' => 'Michael',
    ),
    array(
        'id' => '111',
        'name' => 'Mack',
    )
);
PHP search multidimensional array for value and return key

If you want to search in multidimensional-array by value and return key. So you can use this below example for that:

<?php
 
    $array = array(
        array(
            'id' => '100',
            'name' => 'Rock',
        ),
        array(
            'id' => '105',
            'name' => 'Test',
        ),
        array(
            'id' => '109',
            'name' => 'Michael',
        ),
        array(
            'id' => '111',
            'name' => 'Mack',
        )
    );
 
    function searchByValue($id, $array) {
       foreach ($array as $key => $val) {
           if ($val['id'] === $id) {
             $resultSet['name'] = $val['name'];
             $resultSet['key'] = $key;
             $resultSet['id'] = $val['id'];
             return $resultSet;
           }
       }
       return null;
    }
 
 
  $searchValue = searchByValue('105', $array);
  print_r($searchValue);die;
 
 ?>
PHP search multidimensional array for key and return value

If you want to search in a multidimensional array by key and return value. So you can use the below example for that:

<?php
 
    $array = array(
        array(
            'id' => '100',
            'name' => 'Rock',
        ),
        array(
            'id' => '105',
            'name' => 'Test',
        ),
        array(
            'id' => '109',
            'name' => 'Michael',
        ),
        array(
            'id' => '111',
            'name' => 'Mack',
        )
    );
 
    function searchByKey($keyVal, $array) {
       foreach ($array as $key => $val) {
           if ($keyVal == $key) {
             $resultSet['name'] = $val['name'];
             $resultSet['key'] = $key;
             $resultSet['id'] = $val['id'];
             return $resultSet;
           }
       }
       return null;
    }
 
  $searchByKey = searchByKey('2', $array);
  print_r($searchByKey);die;
 
 ?>

Note:

It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.

Conclusion

Fastest way to search a multidimensional array. In this tutorial, you have learned how to search in a multidimensional array by key and value.

How to make a things in a string be a part of a array

I am making a thing that involves users. All the users are stored in an array, and I want the program to read the contents of a file, which stores the stuff that is to be put into the array, like:

I am making a thing that involves users. All the users are stored in an array, and I want the program to read the contents of a file, which stores the stuff that is to be put into the array, like:

"user1" => "pwd", "user2 => "pwd"

in the file. Then, I use file_get_contents to get all the data into a string called $a. I put $a into the array like:

array($a);

And I want the stuff in $a to be a part of the array, not something in a array in total. How do I do that? Please help. Thanks.

My code is:

fopen("authkey.txt", "r");
$a = file_get_contents("authkey.txt");
$auth_users = array('admin' => 'something', $a);
// I want the things in $a to be a part of the array, not a string


How to remove multiple keys from PHP Array?

How to remove multiple keys from PHP Array?

In this example, we will remove array elements by keys array in php. We can delete multiple keys from array php. Basically we will unset multiple keys from php array.

Originally published by Hardik Savani at https://itsolutionstuff.com

If you see in PHP documentation there are not available directly remove multiple keys from PHP array. But we will create our own PHP custom function and remove all keys by given array value.

In this example I created custom function as array_except(). You need to pass one main array and another will be keys array that you want to remove it.

So let's see bellow example:

Example:

<?php

$myArray = [
'name'=>'Hardik Savani',
'email'=>'[email protected]',
'gender'=>'male',
'website'=>'itsolutionstuff.com'
];

$newArray = array_except($myArray, ['gender', 'email']);

print_r($newArray);

function array_except($array, $keys){
foreach($keys as $key){
unset($array[$key]);
}
return $array;
}
?>

Output:

Array
(
[name] => Hardik Savani
[website] => itsolutionstuff.com
)

I hope it can help you...

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Learn More

PHP for Beginners - Become a PHP Master - CMS Project

Learn Object Oriented PHP By Building a Complete Website

PHP OOP: Object Oriented Programming for beginners + Project

Laravel PHP Framework Tutorial - Full Course for Beginners (2019)

Symfony PHP Framework - Full Course

What is a Full Stack Developer?

Build RESTful API In Laravel 5.8 Example

Laravel 5.8 Tutorial from Scratch for Beginners

Build a CRUD Operation using PHP & MongoBD