PHP objects and arrays to convert each other, PHP arrays to convert _php tutorial

Source: Internet
Author: User

PHP objects and arrays to convert each other, PHP arrays to convert each other


The examples in this article describe how PHP objects and arrays are converted to and from each other. Share to everyone for your reference. The specific analysis is as follows:

This defines 2 php anonymous objects and arrays of functions that are converted to each other, with the following code:

function Array2object ($array) {  if (Is_array ($array)) {    $obj = new StdClass ();    foreach ($array as $key = + $val) {      $obj $key = $val;    }  }  else {$obj = $array;}  return $obj;} function Object2array ($object) {  if (Is_object ($object)) {    foreach ($object as $key = = $value) {      $array [ $key] = $value;    }  }  else {    $array = $object;  }  return $array;}

The usage examples are as follows:

$array = Array (' foo ' = ' bar ', ' one ' = ' + ', ' three ' = ' four '), $obj = Array2object ($array);p rint $obj->one; Output ' s "of" $arr = Object2array ($obj);p rint $arr [' foo ']; Output ' s bar

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/998574.html www.bkjia.com true http://www.bkjia.com/PHPjc/998574.html techarticle PHP object and array conversion methods, PHP arrays to each other This article describes the PHP objects and arrays to convert each other method. Share to everyone for your reference. The specific analysis is as follows: ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.