Convert php string to array object array object array object array

王林
Release: 2023-05-06 10:09:07
Original
419 people have browsed it

In PHP development, it is often necessary to operate on strings, and converting strings into arrays is one of the common requirements. At the same time, array objects are also data types often used in development. Therefore, converting PHP strings into array objects array objects array objects arrays has become one of the skills that PHP developers must master.

This article will introduce in detail how to convert a PHP string into an array, how to convert the array into an object, and finally form an array of array objects, and introduce how to perform basic operations on this array of array objects.

  1. Convert string to array

PHP has a built-in explode() function to convert a string into an array. The explode() function splits a string according to the specified delimiter and returns an array containing the split substrings.

For example, the following code will convert a string separated by "," into an array:

$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);
Copy after login

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login

If no delimiter is specified, Space characters are used by default.

  1. Array to object

In PHP, you can use type casting syntax to convert an array into an object. This process will set each element of the array as an attribute of the object. The attribute name is the array key, and the attribute value is the value corresponding to the corresponding key of the array.

For example, the following code will convert an array into the corresponding object:

$arr = array('name' => 'Tom', 'age' => 18, 'gender' => 'male');
$obj = (object)$arr;
echo $obj->name; //输出Tom
echo $obj->age; //输出18
echo $obj->gender; //输出male
Copy after login

It should be noted that after converting the array into an object, the attribute names in the object are case-sensitive of.

  1. Object to Array

An object can be converted into an array by forcing the object into an array. This process will convert all the attributes of the object into key-value pairs in the array. The attribute name is the key of the array, and the attribute value is the value corresponding to the corresponding key of the array.

For example, the following code will convert an object into the corresponding array:

class Person {
    public $name;
    public $age;
    public $gender;
}

$person = new Person();
$person->name = 'Tom';
$person->age = 18;
$person->gender = 'male';

$arr = (array)$person;
print_r($arr);
Copy after login

Output:

Array
(
    [name] => Tom
    [age] => 18
    [gender] => male
)
Copy after login

It should be noted that after converting the object into an array, The key names in the array are the same as the object property names, and the property access symbol ($) precedes the key name.

  1. Composing an array of array objects

After converting the string into an array, use forced type conversion to convert the array into an object and then form an array of array objects.

$str = "name:Tom,age:18,gender:male;name:Lucy,age:21,gender:female;";
$data_arr = explode(";",$str);

function arrayToObject($arr){
    if(gettype($arr) != 'array'){
        return;
    }
    foreach ($arr as $key => $value) {
        if(is_array($value)){
            $arr[$key] = arrayToObject($value);
        }
    }
    return (object) $arr;
}

function objectToArray($obj){
    $obj = (array) $obj;
    foreach ($obj as $k => $v){
        if(gettype($v) == 'resource') return;
        if(gettype($v) == 'object' || gettype($v) == 'array')
            $obj[$k] = (array) objectToArray($v);
    }
    return $obj;
}

$arr = array();
foreach ($data_arr as $data_item) {
    $data_item_arr = explode(",",$data_item);
    $data_obj = new stdClass();
    foreach ($data_item_arr as $item) {
        $item_arr = explode(":",$item);
        if(count($item_arr) == 2){
            $key = $item_arr[0];
            $value = $item_arr[1];
            $data_obj->$key = $value;
        }
    }
    $data_arr[] = $data_obj;
}

$arr_obj = arrayToObject($arr);
print_r($arr_obj);

$arr2 = objectToArray($arr_obj);
print_r($arr2);
Copy after login

Output:

Array
(
    [0] => stdClass Object
        (
            [name] => Tom
            [age] => 18
            [gender] => male
        )

    [1] => stdClass Object
        (
            [name] => Lucy
            [age] => 21
            [gender] => female
        )

)
Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 18
            [gender] => male
        )

    [1] => Array
        (
            [name] => Lucy
            [age] => 21
            [gender] => female
        )

)
Copy after login
  1. Operation on array of array objects

After getting the array of array objects, we can perform common operations on it, such as Add, delete, filter, etc.

Add elements:

$data_obj1 = new stdClass();
$data_obj2 = new stdClass();
$data_obj1->name = 'Tom';
$data_obj2->name = 'Lucy';
$data_arr[] = $data_obj1;
$data_arr[] = $data_obj2;
Copy after login

Delete elements:

unset($data_arr[0]);
Copy after login

Filter elements:

$result_arr = array_filter($data_arr,function ($item){
    return $item->age > 20;
});
print_r($result_arr);
Copy after login

The above operation will get information about people older than 20 years old.

Through the introduction of this article, you should already know how to convert a PHP string into an array object array object array object array, and you can perform basic operations on it. This skill is very useful in PHP development. I hope everyone can study it seriously and apply it to actual development.

The above is the detailed content of Convert php string to array object array object array object array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template