Obtaining JSON object value from array - php

Is there a way to get the values of a JSON object member variable from a array without looping the objects? Similar to how call_user_func_array would work but for member variables?
E.g.
array 0 =>
object(stdClass)[402]
public 'id' => int 196
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 1 =>
object(stdClass)[403]
public 'id' => int 197
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 2 =>
object(stdClass)[404]
public 'id' => int 3269
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 3 =>
object(stdClass)[405]
public 'id' => int 3334
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 4 =>
object(stdClass)[406]
public 'id' => int 5831
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 5 =>
object(stdClass)[379]
public 'id' => int 6358
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 6 =>
object(stdClass)[400]
public 'id' => int 6592
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0
In one single call I'm trying to obtain the summation of creditLimit.

Pass "true" to json_decode to get a regular array.

Related

Combine Eloquent (Laravel 4.2) Results Into Multidimensional Array Based On A Value

I have this array that is returned from Laravel 4.2 Query Builder I am trying to rearrange a bit. I can't seem to figure out how to get the result I am looking for.
array (size=8)
0 =>
object(stdClass)[565]
public 'id' => int 53
public 'vehicle_id' => int 11
public 'date' => string '2015-10-27' (length=10)
1 =>
object(stdClass)[563]
public 'id' => int 55
public 'vehicle_id' => int 11
public 'date' => string '2015-10-29' (length=10)
2 =>
object(stdClass)[567]
public 'id' => int 61
public 'vehicle_id' => int 7
public 'date' => string '2015-10-30' (length=10)
3 =>
object(stdClass)[561]
public 'id' => int 63
public 'vehicle_id' => int 6
public 'date' => string '2015-10-30' (length=10)
4 =>
object(stdClass)[572]
public 'id' => int 64
public 'vehicle_id' => int 8
public 'date' => string '2015-10-30' (length=10)
5 =>
object(stdClass)[580]
public 'id' => int 65
public 'vehicle_id' => int 9
public 'date' => string '2015-10-30' (length=10)
6 =>
object(stdClass)[579]
public 'id' => int 66
public 'vehicle_id' => int 10
public 'date' => string '2015-10-30' (length=10)
7 =>
object(stdClass)[578]
public 'id' => int 57
public 'vehicle_id' => int 11
public 'date' => string '2015-10-31' (length=10)
Example of desired result:
array(
'2015-10-27' => array(11),
'2015-10-30' => array( 6, 7, 8, 9, 10 ),
'2015-10-31' => array(11)
)
I have tried foreach loops nested in for loops etc.. been driving me nuts for a while now. Am I missing something simple here?
$dates = [];
foreach($yourArray as $obj) {
$dates[$obj->date][] = $obj->vehicle_id;
}

Symfony2 Doctrine Array of joined objects

I have the following repository:
class CustomerRepository extends EntityRepository
{
public function searchCustomer($criteria)
{
$q = $this->createQueryBuilder('c');
$q->join('TeamERPCustomerBundle:Company', 'o',
'WITH', 'c.company = o.id');
if (isset($criteria) and $criteria!=""){
$q->orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
(...)
}
$q = $q->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
return $q;
}
}
I call it from the controller as follows:
$result = $this->getDoctrine()->getManager()
->getRepository('TeamERPCustomerBundle:Customer')
->searchCustomer($customerInfo);
It returns an array of elements as follows:
array (size=20)
0 =>
array (size=9)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
1 =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
2 =>
array (size=9)
'id' => int 2
'customer_name' => string 'Kay' (length=3)
'address' => null
'postal_address' => null
'city_town_village' => null
'e_mail' => null
'land_line' => null
'cell_phone' => null
'fax' => null
3 =>
array (size=2)
'id' => int 3
'company_name' => string 'DTC' (length=3)
(...)
It gives one element of the array per object, but what I want it the classic mysql join; as result an array of elements of the same type. Something like mergin elements 1 and 2 together.
The other problem with this query is that is does not work like that classic join ether, it gives all the customers, but only the companies that are not repeated.
Can anyone help me to get a uniform array of elements?
Edit: I managed to find a solution:
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT c, i
FROM TeamERPCustomerBundle:Customer c
JOIN c.company i');
//$query->setParameter('id', '1'); With this is can add as many parameter as I need
return $query->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
This returned something like this:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' =>
array (size=2)
'id' => int 1
'company_name' => string 'Debswana' (length=8)
As you might have already appreciated this array is different from what I was getting before. so now I just have to go through the array and change it to what ever I want. for instance:
foreach ($result as $key => $value){
$result[$key]['company'] = $value['company']['company_name'];
}
Now we are talking. This Is kind of what I wanted:
array (size=11)
0 =>
array (size=10)
'id' => int 1
'customer_name' => string 'Abel' (length=4)
'address' => string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
'postal_address' => string '75100' (length=5)
'city_town_village' => string 'Las Tunas' (length=9)
'e_mail' => string 'abel#ltu.sld.cu' (length=15)
'land_line' => string '346386' (length=6)
'cell_phone' => null
'fax' => null
'company' => string 'Debswana' (length=8)

Removing an item from a multidimensional array in php by value

I have a multidimensional array $array that looks like this:
array (size=3)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[501]
public 'id' => int 9
public 'first_name' => string 'Joe' (length=3)
public 'last_name' => string 'Bob' (length=3)
2 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
I would like to be able to remove one of the items in the array by searching for values (not indexes).
So, I would like to remove the item in the array that has the first_name property of 'Joe'.
So if I removed it, the array would look like this:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
How would I be able to do this? Thanks.
Yes you could just use a foreach in this case. It will work just fine. Just use your search string needle and add an if inside the loop comparing the objects property firstname and the needle:
$first_name_search = 'Joe';
foreach ($array as $key => $value) {
$value = reset($value); // get that index 0 which is nested
if($value->first_name == $first_name_search) {
unset($array[$key]);
}
}

How could I expose data using var_dump on Eloquent ORM Laravel?

I'm new to php development and I want to use var_dump to debug my php program on the client side.
$test= Admin::where("EPANTHERID","=",$email);
var_dump($test);die();
On the client side, I receive this
bject(Illuminate\Database\Eloquent\Builder)[187] protected 'query' => object(Illuminate\Database\Query\Builder)[186] protected 'connection' => object(Illuminate\Database\MySqlConnection)[179] protected 'pdo' => object(PDO)[180] ... protected 'readPdo' => null protected 'reconnector' => object(Closure)[185] ... protected 'queryGrammar' => object(Illuminate\Database\Query\Grammars\MySqlGrammar)[181] ... protected 'schemaGrammar' => null protected 'postProcessor' => object(Illuminate\Database\Query\Processors\MySqlProcessor)[182] ... protected 'events' => object(Illuminate\Events\Dispatcher)[14] ... protected 'paginator' => object(Closure)[184] ... protected 'cache' => object(Closure)[183] ... protected 'fetchMode' => int 8 protected 'transactions' => int 0 protected 'queryLog' => array (size=0) ... protected 'loggingQueries' => boolean true protected 'pretending' => boolean false protected 'database' => string 'xinwen_development' (length=18) protected 'tablePrefix' => string '' (length=0) protected 'config' => array (size=9) ... protected 'grammar' => object(Illuminate\Database\Query\Grammars\MySqlGrammar)[181] protected 'selectComponents' => array (size=11) ... protected 'tablePrefix' => string '' (length=0) protected 'processor' => object(Illuminate\Database\Query\Processors\MySqlProcessor)[182] protected 'bindings' => array (size=5) 'select' => array (size=0) ... 'join' => array (size=0) ... 'where' => array (size=1) ... 'having' => array (size=0) ... 'order' => array (size=0) ... public 'aggregate' => null public 'columns' => null public 'distinct' => boolean false public 'from' => string 'ADMIN' (length=5) public 'joins' => null public 'wheres' => array (size=1) 0 => array (size=5) ... public 'groups' => null public 'havings' => null public 'orders' => null public 'limit' => null public 'offset' => null public 'unions' => null public 'lock' => null protected 'backups' => array (size=0) empty protected 'cacheKey' => null protected 'cacheMinutes' => null protected 'cacheTags' => null protected 'cacheDriver' => null protected 'operators' => array (size=19) 0 => string '=' (length=1) 1 => string '<' (length=1) 2 => string '>' (length=1) 3 => string '<=' (length=2) 4 => string '>=' (length=2) 5 => string '<>' (length=2) 6 => string '!=' (length=2) 7 => string 'like' (length=4) 8 => string 'not like' (length=8) 9 => string 'between' (length=7) 10 => string 'ilike' (length=5) 11 => string '&' (length=1) 12 => string '|' (length=1) 13 => string '^' (length=1) 14 => string '<<' (length=2) 15 => string '>>' (length=2) 16 => string 'rlike' (length=5) 17 => string 'regexp' (length=6) 18 => string 'not regexp' (length=10) protected 'model' => object(Admin)[178] protected 'table' => string 'ADMIN' (length=5) protected 'fillable' => array (size=1) 0 => string 'EPANTHERID' (length=10) protected 'connection' => null protected 'primaryKey' => string 'id' (length=2) protected 'perPage' => int 15 public 'incrementing' => boolean true public 'timestamps' => boolean true protected 'attributes' => array (size=0) empty protected 'original' => array (size=0) empty protected 'relations' => array (size=0) empty protected 'hidden' => array (size=0) empty protected 'visible' => array (size=0) empty protected 'appends' => array (size=0) empty protected 'guarded' => array (size=1) 0 => string '*' (length=1) protected 'dates' => array (size=0) empty protected 'touches' => array (size=0) empty protected 'observables' => array (size=0) empty protected 'with' => array (size=0) empty protected 'morphClass' => null public 'exists' => boolean false protected 'eagerLoad' => array (size=0) empty protected 'macros' => array (size=0) empty protected 'onDelete' => null protected 'passthru' => array (size=12) 0 => string 'toSql' (length=5) 1 => string 'lists' (length=5) 2 => string 'insert' (length=6) 3 => string 'insertGetId' (length=11) 4 => string 'pluck' (length=5) 5 => string 'count' (length=5) 6 => string 'min' (length=3) 7 => string 'max' (length=3) 8 => string 'avg' (length=3) 9 => string 'sum' (length=3) 10 => string 'exists' (length=6) 11 => string 'getBindings' (length=11)
It seems like Eloquent has some mechanism which prevents me from accessing data using var_dump. How could I solve this problem or could anyone show me a better way to debug my php scripts? Thank you.
You should use:
$test= Admin::where("EPANTHERID","=",$email)->get();
or
$test= Admin::where("EPANTHERID","=",$email)->first();
if EPANTHERID is unique and then use var_dump on result.
That's because without get() or first() (and some other methods) you have in your variable only object that holds your query and not the result of running the query (the query in your code has not been launched yet).

Doctrine2 getResult() duplicate objects

I have a weird error in Doctrine 2
Repository function
$query = $this->_em->createQuery('SELECT c FROM \Model\Entity\Cluster c
WHERE c.publisherid = :publisherid');
$query->setParameter("publisherid", (int)$publisher_id);
var_dump($query->getResult());
var_dump($query->getArrayResult());
And what we have
Objects:
array (size=8)
0 =>
object(Model\Entity\Cluster)[**396**]
private 'psiteid' => int 215
private 'psiteclusterdate' =>
object(DateTime)[371]
public 'date' => string '**2011-12-20** 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
private 'publisherid' => int 276
private 'views' => int 14657
private 'clicks' => int 6220
...
1 =>
object(Model\Entity\Psitecluster)[**396**]
private 'psiteid' => int 215
private 'psiteclusterdate' =>
object(DateTime)[371]
public 'date' => string '**2011-12-20** 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
private 'publisherid' => int 276
private 'views' => int 14657
private 'clicks' => int 6220
...
Rows with key 0-3 dublicate this object (396) and other rows dublicate second object.
When i dump as array:
array (size=8)
0 =>
array (size=22)
'psiteid' => int 215
'psiteclusterdate' =>
object(DateTime)[367]
public 'date' => string '**2011-12-20** 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
'publisherid' => int 276
'views' => int 14657
'clicks' => int 6220
....
1 =>
array (size=22)
'psiteid' => int 215
'psiteclusterdate' =>
object(DateTime)[396]
public 'date' => string '2011-12-21 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
'publisherid' => int 276
'views' => int 10166
'clicks' => int 4028
All the rows are different in array. What's wrong with objects?
I solved a problem. It is a bug in Doctrine2. I created bug report here http://doctrine-project.org/jira/browse/DDC-1780
You can fix with in you project (if you can edit Doctrine library files).
Just add
if ($id[$fieldName] instanceof \DateTime) {
$id[$fieldName] = $id[$fieldName]->getTimestamp();
}`
in foreach loop.

Categories