Using implode for stdClass Objects in php

Asked
Active3 hr before
Viewed126 times

7 Answers

using
90%

With PHP 7.0+ you can use array_column for this.,You could use array_map() and implode()...

You could use array_map() and implode()...

$a = array_map(function($obj) {
      return $obj - > foo;
   },
   array(1 => $obj1, 2 => $obj2, 3 => $obj3));

$a = implode(", ", $a);
load more v
88%

You may easily do this just casting as an array. Most people seem to have skipped college or C programming., Podcast 392: Do polyglots have an edge when it comes to mastering programming... ,Here's an alternative solution using array_map:,how can I use implode(', ' ) to get the following result:

You may easily do this just casting as an array. Most people seem to have skipped college or C programming.

implode(',', (array) $categories);
load more v
72%

And I want to implode on foo of all the stdClass objects in that array to create a comma separated list. So the desired result is:,You could use array_map() and implode()...,I have an array of stdClass objects and I want to build a comma separated list using one specific field of all those stdClass objects. My array looks like this:,Is there any way to do this with implode (or some other mystery function) without having to put this array of objects through a loop?

I have an array of stdClass objects and I want to build a comma separated list using one specific field of all those stdClass objects. My array looks like this:

$obj1 = stdClass Object([foo] => 4[bar] => 8[foo - bar] => 15);
$obj2 = stdClass Object([foo] => 16[bar] => 23[foo - bar] => 42);
$obj3 = stdClass Object([foo] => 76[bar] => 79[foo - bar] => 83);

$a = array(1 => $obj1, 2 => $obj2, 3 => $obj3);

And I want to implode on foo of all the stdClass objects in that array to create a comma separated list. So the desired result is:

4, 16, 76
load more v
65%

$output = implode(', ', array_map(
   function($v, $k) {
      return sprintf("%s='%s'", $k, $v);
   },
   $input,
   array_keys($input)
));
75%

is_object — Finds whether a variable is an object, Finds whether the given variable is an object. ,is_array() - Finds whether a variable is an array,is_bool() - Finds out whether a variable is a boolean

load more v
40%

Method 2: Using json_encode() Function: The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.Syntax:,Method 1: Using implode() function: The implode() method is an inbuilt function in PHP and is used to join the elements of an array. The implode() method is an alias for PHP | join() function and works exactly same as that of join() function.,How to create comma separated list from an array in PHP ?,How to convert a string into number in PHP?

Syntax:

string implode($separator, $array)

Welcome to GeeksforGeeks A Computer Science Portal

Method 2: Using json_encode() Function: The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
Syntax:

string json_encode($value, $option, $depth)

Output:

{
   "name": "GFG",
   "0": {
      "email": "abc@gfg.com",
      "mobile": "XXXXXXXXXX"
   }
}
load more v
22%

Anyone know a better way of doing this:,ie create a comma-separated string from an array of object properies (a PDO result set - returned using the object notation PDO::FETCH_OBJ),Since each object is returned as standardized object…,you’re wanting generalized code that can handle as many result returns as desired,

Anyone know a better way of doing this:

$a = new stdClass;
$a - > id = 1;
$b = new stdClass;
$b - > id = 2;

$arr = array($a, $b);

// from that generate string "(1,2)"

$html = "(";
foreach($arr as $ar)
$html. = $ar - > id.
',';

$out = rtrim($html, ',').
')';
var_dump($out);
// (1,2)
load more v

Other "using-undefined" queries related to "Using implode for stdClass Objects in php"