how to convert stdClass Object in to an array - php

I want to get the names of stdClass Object.
"Array
(
[0] => stdClass Object
(
[id] => 179111965447818
[name] => foot ball
)
[1] => stdClass Object
(
[id] => 103992339636529
[name] => Cricket
)
)
Wishes out put:
array("foot ball","Cricket")

I tried this and it worked for me
$sports=array();
foreach($sport as $key=>$val){
$sports[$key] =$val->name;
}

Your question is hard to understand, and you clearly haven't tried it. Here's the answer anyways champ.
$array = array();
foreach($obj as $item) {
array_push($array, $item->name);
}
The data will be in $array.

try a loop and create a new array
$newarr = array();
foreach($arr as $v) {
$newarr[] = $v->name;
}
print_r($newarr);

$newArray=array();
foreach($array as $valArray){
//$valArray has all the value that you required and
// $valArray['name'] is actually what you need
$newArray=$valArray['name'] ;
}
$newArray has what you require.

Related

How to parse arrays with different levels PHP

In a foreach loop i would like to compare [name] value beetween different arrays but they have not the same levels.
Array(
[array1] => Array
(
[0] => WP_Term Object
(
[name] => Plafond
)
)
[array2] => WP_Term Object
(
[name] => Chaudière
)
[array3] => Array
(
[0] => WP_Term Object
(
[name] => Pla
)
[1] => WP_Term Object
(
[name] => Toc
)
)
)
I don't know how could i get the [name] in the same loop whereas levels are different.
I have tried to make :
foreach( $fields as $name => $value )
{
echo $value->name; }
Should i add another loop in the first loop ?
thanks
So your data looks like this:
$json = '{"array1":[{"name":"Plafond"}],"array2":{"name":"Chaudière"},"array3":[{"name":"Pla"},{"name":"Toc"}]}';
$array = json_decode($json);
If you don't know how deep it will go, a simple recursive function should work. Perhaps something like this:
function get_name($o, &$output) {
if (is_array($o)) {
foreach($o as $v) {
get_name($v, $output);
}
} elseif (property_exists($o, "name")) {
$output[] = $o->name;
}
}
$output = [];
foreach ($array as $v) {
get_name($v, $output);
}
If you data is going to look like the sample you provided (i.e. it will always be first or second level) then you don't need to worry about recursion.
$output = [];
foreach ($array as $k=>$v) {
if (is_array($v)) {
foreach ($v as $k2=>$v2) {
$output[] = $v2->name;
}
} else {
$output[] = $v->name;
}
}
Either way, your output values are all in the $output array:
print_r($output);
Output:
Array
(
[0] => Plafond
[1] => Chaudière
[2] => Pla
[3] => Toc
)
You can use array_map, array_key_exists to retrive the name index from the array
$jsonFormat = '{"array1":[{"name":"Plafond"}],"array2":{"name":"Chaudière"},"array3":[{"name":"Pla"},{"name":"Toc"}]}';
$jsonArray = json_decode($jsonFormat,true);
$res = [];
array_map(function($v) use (&$res){
if(array_key_exists('name', $v)){
$res[] = $v['name'];
}else{
foreach($v as $_key => $_value){
$res[] = $_value['name'];
}
}
}, $jsonArray);
echo '<pre>';
print_r($res);
Result:-
Array
(
[0] => Plafond
[1] => Chaudière
[2] => Pla
[3] => Toc
)
You can use $res to compare the names.

PHP Array stdClass Object

I have this array
Array (
[0] => stdClass Object (
[id] => 252062474)
[1] => stdClass Object (
[id] => 252062474)
[3] => stdClass Object (
[id] => 252062474)
)
I need echo all of id's
I tried,
foreach($result as $item) {
echo $item->id;
}
but no luck
I try json_decode()
again no luck I use php 5.5.8
I know this work
echo $item[0]->id;
but i don't how many index is there
any idea?
Maybe you are confused on foreach(). If this works:
echo $item[0]->id;
Then you would need:
foreach($item as $result) {
echo $result->id;
}
Try this-
Code
foreach($result as $p) {
echo $p['id'] . "<br/>";
}
Output
252062474
252062474
252062474
This array could be looped through and data could be retrieved. As you are saying its not working I have added the following code to illustrate how it works right from generating the array for you.
// generating an array like you gave in the example. Note that ur array has same value
// for all the ids in but my example its having different values
$arr = array();
$init = new stdClass;
$init->id = 252062474 ;
$arr[] = $init;
$init = new stdClass;
$init->id = 252062475 ;
$arr[] = $init;
$init = new stdClass;
$init->id = 252062476 ;
$arr[] = $init;
print_r($arr);
The above array is same as yours
Array ( [0] => stdClass Object ( [id] => 252062474 )
[1] => stdClass Object ( [id] => 252062475 )
[2] => stdClass Object ( [id] => 252062476 )
)
Now the following code will loop through and get the data as
foreach($arr as $key=>$val){
echo $key.' ID is :: '.$val->id;
echo '<br />';
}
The output will be
0 ID is :: 252062474
1 ID is :: 252062475
2 ID is :: 252062476
Try this
foreach($result as $item) {
$item = (array)$item;
echo $item['id'];
}

convert array containing a single key object

I have an array containing single key objects like so:
Array
(
[0] => stdClass Object
(
[state] => 1
)
[1] => stdClass Object
(
[state] => 1
)
)
I want it to look like this:
Array
(
[0] => 1
[1] => 1
)
What is the most efficient way of doing this? I'm not quite sure how to put this problem in simple words, so I can't google it either.
You could use array_map:
$result = array_map(function($object) {
return $object->state;
}, $originalArray);
you can do it with a for loop :
for $array in $val
$val =$val[state]
You can use array_walk and pass the value in by reference:
array_walk($array, function(&$v, $i) {
$v = $v->state;
});
or
array_walk($array, create_function('&$v', '$v = $v->state;'));
If you got one of the newer PHP versions you can do that with a foreach loop and a reference:
foreach ($array as &$value)
{
$value = $value->state;
}

php simple associative array

I've done this a million times but for some reason I can't get this to work today...
I have this associative array
Array
(
[0] => stdClass Object
(
[registrantKey] => 106569618
[firstName] => xxx
[lastName] => yyy
[email] => x#x.x
)
[1] => stdClass Object
(
[registrantKey] => 106975808
[firstName] => qqq
[lastName] => ppp
[email] => aaa#aaa.com
)
...
...
I just want to get the first name of each one of them, im using a foreach loop but doesn't really let me get what I want.
Any ideas?
foreach($array as $key=>$value){
echo $value['firstName'];
}
For this case, your array element isn't an array but an object.
As such, it should be:
foreach($array as $key=>$value){
echo $value->firstName;
}
Try this:
$value->firstName;
You can also do:
foreach($array as $key=> (array) $value){
echo $value['firstName'];
}
This will typecast your object to an array.
foreach($array as $key=>$value){
echo $value->firstName;
}
You have stdClass Objects as array elements and not associative arrays so you need the option notation: $value->firstName
You could also convert the stdClass Object to array by type casting:
foreach($array as $key=> (array) $value){
echo $value['firstName'];
}
<?php
$array = (array)$array;
$firstNames = array();
foreach($array as $a)
{
$firstNames[] = $a['firstName'];
}
print_r($firstNames);
?>

Array value and index migration

My array is like:
Array
(
[0] => Array
(
[0] => "name"
[1] => "zxczxc5"
)
[1] => Array
(
[0] => "about"
[1] => "zxczxc"
)
[2] => Array
(
[0] => "contact"
[1] => "zxczxc"
)
)
I want to generate another array like this :
Array
{
['name']="zxczxc5";
}
Array
{
['contact']="zxczxc";
}
Array
{
['about']="zxczxc";
}
I want the first array index zero value goes as the index of second value in my new array.
Thanks.
There are many ways to solve what you want to achieve, this is just one of those:
foreach ($array as &$pair) {
$pair = call_user_func_array('array_combine', $pair);
}
unset($pair);
print_r($array);
It makes use of array_combine.
Assuming you name your first Array $aTest:
foreach($aTest as $aElement)
{
$aNewArray[$aElement[0]] = $aElement[1];
}
print_r($aNewArray);
foreach ($array as $value) {
$newArray[$value['0']] = $value['1'];
}
Assuming the first array is called $array
$new_array = array();
foreach($array as $element)
{
$new_array[] = array($element[0] => $element[1]);
}
$newArr = array();
foreach($arr as $val) {
$newArr[$val[0]] = $val[1];
}

Categories