Accessing data from DB - php

I'm getting data from database and putting it in a variable $data. If I'll print_r($data), I'll get something like that:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Bob
)
[1] => stdClass Object
(
[id] => 2
[name] => Mike
)
)
It has more [key] => value in each of them, and obviously it doesn't end on [1].
I'm using foreach() like that:
foreach ($data as $item) {
foreach ($item as $key => $value) {
//code
}
}
I have two question regards all the above:
1) Is there less complex technique to get the $key and $value?
2) If, for example, I want to output only the [name], how would I access it?
The $key[placeholder] while placeholder is a number is outputting the letter number value of the $key.

You have an array with object, so if you want only the name:
$name_list = array();
foreach ($data as $obj) {
$name_list[] = $obj->name;
}
This way you will have an array with only the name of each object.
So to summarize :
You loop through an array of obj, so to access the properties of each object just do :
$obj->/* the properties */;

Related

Looping with multidimensional arrays in PHP

So I have an Array set up as follows
Array
(
[0] => App\Model\Entity\Member Object
(
[id] => 20
[fname] => John
// ...
[member_attribute] => Cake\ORM\Entity Object
(
[id] => 13
[membership_status] => Active
[last_payment] => 1541581496
// ...
)
)
I'm using a foreach loop for the first level
foreach ($aArray as $Member => $Value)
However I can't access the [member_attributes] data because it's in its own array.
Any help?
The items in your array are objects, so use object notation to access them:
foreach ($aArray as $Member => $Value) {
$id = $Value->member_attribute->id;
}
There also is no need to use the key => value form of the foreach, you could just access your Members like:
foreach ($aArray as $Member) {
$id = $Member->member_attribute->id;
}
That may be a matter of preference, however, I think it is more clear.

PHP - How can I append/merge onto an array with depth

I have a database query which returns several records, however I would like to append more data to this object by typing the array as an object so that I can for example add more data to the output than the database contains.
I have done some research on this and found some functions such as array_merge but whenever I attempt to use this it gave me issues related to the array's depth and sometimes even created an entirely new array key instead of adding onto the currently existing ones.
This is the data from my database:
Array
(
[Cat] => Array
(
)
[Dog] => Array
(
)
[Rabbit] => Array
(
[0] => stdClass Object
(
[name] => fluffy
[owner] => foobar
)
[1] => stdClass Object
(
[name] => toby
[owner] => foobar
)
[2] => stdClass Object
(
[name] => josie
[owner] => Joseph
)
)
)
I'd like to make it so that every array key which exist have an extra field but other animals remain empty if they do not have any records.
Example:
Array
(
[Cat] => Array
(
[0] => stdClass Object
(
[name] => ralph
[owner] => Joseph
[extra] => some extra data
)
)
[Dog] => Array
(
)
[Rabbit] => Array
(
[0] => stdClass Object
(
[name] => fluffy
[owner] => foobar
[extra] => some extra data
)
[1] => stdClass Object
(
[name] => toby
[owner] => foobar
[extra] => some extra data
)
[2] => stdClass Object
(
[name] => josie
[owner] => Joseph
[extra] => some extra data
)
)
)
The reason I am attempting to merge the two is because I have several helper functions which generate pretty results and I'd like to utilize them rather than output the raw data from the database, the extra data is demonstration of how i'd like to merge onto the current array.
Thanks!
EDIT - Updated code:
function listPets(){
$foo = [];
foreach($pets as $p){
$getPets = $database->Findall("SELECT name, owner, info FROM pets...");
$foo[$p->name] = $getPets;
foreach($foo as $arr){
if(count($arr)){
foreach($arr as $v){
$v->extra = $this->message($getPets->info);
}
}
}
}
return $foo;
}
The solution is:
Loop through the array using foreach loop.
Check if the inner array is empty or not.
If the inner array is not empty then loop through it and append extra property to each of the objects.
So your code should be like this:
// Suppose $array is your original array
foreach($array as $arr){
if(count($arr)){ // check if the array is empty or not
foreach($arr as $v){
$v->extra = 'some extra value'; // append extra property to the object
}
}
}
// display $array
echo '<pre>';
print_r($array);
echo '</pre>';
Here's the demo:
Live Demo
Are you actually trying to merge two arrays or just append some extra information to the non-empty keys in the array of animals?
In the second case, depending on the information you want to add, there's two ways you could go:
Add it to the SELECT statement:
SELECT name, owner, 'extra stuff' as extra FROM ...
Do it like #rajdeep-paul said. Only you can skip the empty array check, if it's empty, foreach will not iterate through it anyway:
foreach ($dbData as $animals) {
foreach ($animals as $animal) {
$animal->extra = 'extra stuff';
}
}
The function below merges arrays & objects:
UPDATE
<?
function fuse_data()
{
$list = func_get_args();
$data = array_shift($list);
$type = gettype($data);
$data = ((array)$data);
foreach ($list as $indx => $item)
{
foreach ($item as $name => $valu)
{
if (!isset($data[$name]) || empty($data[$name]))
{
$data[$name] = $valu;
}
}
}
return (($type == 'object') ? ((object)$data) : $data);
}
?>
Use it like this:
$data = fuse_data($result, $node2, $list3, $foo, $bar);
Edit as you like, it's just a proof of concept.
You can do something like ...
foreach($pets as &$pet) { // passing by reference to manipulate the original array.
if(!empty($pet) && is_array($pet)){ // Checking if the element is an array and not empty
foreach($pet as &$feature){ // Again passing by reference the 2nd dimension
if(is_object($feature)){ // Checking if the element is object to prevent the "Property of non-object" error.
$feature->extra = 'Some value'; // If all is well, we asign the new property to the object.
}
}
}
}
Now even though I think there are better ways to write(optimize) a query than firing it inside a loop!!! but just for the sake of your question, your code should be like..
function listPets(){
$foo = [];
foreach($pets as $p){
$getPets = $database->Findall("SELECT name, owner, info FROM pets...");
$foo[$p->name] = $getPets;
foreach($foo as $arr){
if(is_array($arr) && count($arr)){
foreach($arr as $v){
if(!empty($v) && is_object($v)){
$v->extra = $this->message($getPets->info);
}
}
}
}
}
return $foo;
}

PHP - Get All values from object without knowing properties

I'm having an issue with displaying object properties.
normally you would call them using :
$obj["name"]
but what if you did not know the properties e.g "name"
for an array , you can simply call $arr[0] , $arr[1] etc...
But if i have this object (print_r):
stdClass Object ( [id] => 1 [Name] => george [Number] => 437439742 [Email] => hds#gmail.com)
stdClass Object ( [id] => 2 [Name] => tom [Number] => 4343554643 [Email] => fdhk#gmail.com)
how can i use a foreach to display all the values e.g : 1 , george , 437...
-Without knowing the name of the properties -> NOT $o["id"];
foreach($object as $o)
{
echo $o[i]; // doesn't work on objects
}
I tried converting it to arrays , but it will be an array of the objects so i can;t get the inside info.
//$array = array ($object);
You have a couple of options. One is the get_object_vars() function, and another is to cast the object to an array.
foreach (get_object_vars($object) as $var => $val) {
// ...
}
or
foreach ((array) $object as $var => $val) {
// ...
}
You can use:
$array = get_object_vars($obj);

Syntax for reading an array in php

I have an array, stored in $array, that with
print "<pre>";
print_r($array);
print "</pre>";
gives an output like this:
Array
(
[device] => Array
(
[0] => Array
(
[#attributes] => Array
(
[name] => Low volt light
[id] => 10
[type] => Z-Wave Switch Multilevel
[value] => 0
[address] => 00016922-018
[code] =>
[canDim] => True
[lastChange] => 26-07-2014 17:31:33
[firstLocation] => Kitchen
[secondLocation] => Main
)
)
[1] => Array
(
[#attributes] => Array
(
[name] => Light
[id] => 11
[type] => Z-Wave Switch Multilevel
[value] => 99
[address] => 00016922-019
[code] =>
[canDim] => True
[lastChange] => 31-07-2014 20:01:05
[firstLocation] => Bedroom
[secondLocation] => Main
)
)
I cannot find my way to access/display for example the value (in this case 0) of device with [id]=>10. What syntax would be the right one in php?
There's not an easy way to do this, without looping through the array.
e.g.
foreach ($array['devices'] as $device) {
if ($device['#attributes']['id'] === $idBeingSearchedFor) {
// Do something with $device.
}
}
Due to the #attributes array key, I'm guessing that this came from XML at some point: You might consider using Simple XML to parse it instead, as you could potentially use XPath then, which does support this type of access.
Alternatively again, you could reformat the array so it could be easily accessed by ID.
For example:
$formattedArray = array();
foreach ($array['devices'] as $device) {
$id = $device['#attributes']['id'];
$formattedArray[$id] = $device;
}
You could then access the device by its ID as follows:
$device = $formattedArray[$idBeingSearchedFor];
You could do it like that:
$id = 10;
$device = array();
foreach($array['device'] as $devices) {
if($devices['#attributes']['id'] == $id) {
$device = $devices['#attributes'];
break;
}
}
echo $device['value'];
Looks like SimpleXML, and if that is the case then those arrays are actually objects that, when put through print_r, look just like arrays. To access them, do the following:
Get straight to the data:
$name = $array->device[0]->attributes()->name;
Or loop through each of the attributes in the first device:
foreach ($array->device[0]->attributes() as $key => $value) {
// Do something with the data. $key is the name of the
// attribute, and then you have the $value.
}
Or you could loop through all the devices:
foreach ($array->device as $device) {
foreach ($device->attributes() as $key => $value) {
// Do something
}
}
It's simple ... try below...
print $array['device'][0]['#attributes']['id'];
or
print $array['device']['0']['#attributes']['id'];

dynamically create arrays from multidimensional array

So if having a multidimensional array like:
Got it from here (as a demo): PHP. Loop through an array and get items with attributes in common
$data
Array
(
[0] => stdClass Object
(
[term_id] => 3
[name] => Comercial
)
[1] => stdClass Object
(
[term_id] => 4
[name] => Escolar
)
[2] => stdClass Object
(
[term_id] => 5
[name] => Kinder
)
[3] => stdClass Object
(
[term_id] => 6
[name] => Primaria
)
[4] => stdClass Object
(
[term_id] => 7
[name] => Secundaria
)
[5] => stdClass Object
(
[term_id] => 1
[name] => Uncategorized
)
)
Having 0,1,2,3,4,5 stdClass Objects, how can I create individual arrays for each std Object dynamically.
By that I mean that the function should be able to create $varX array, where X is the array number of the stdObject, automatically...
$var0 = $data[0];
$var1 = $data[1];
and so on, determined by $data first level count of arrays.
Edit:
I got carried away and forgot to mention the most important question:
Having $var0, $var1... is very important because for a later use of all or each one individually.
So
Needs to create X variables according to the count of first level of the multidimensional array
each $varX needs to be accessible in common with the rest of $varX or individually.
$count = count($data); //6
foreach ($data as $key => $value)
{
$var.$key = $value;
}
Ok, that function works partially because from there I don't know how to make it automatically add $val1,$val2... to (ex:) array_intersect($val1,$val2,$val3...
The easiest way would be to use extract.
extract($data, EXTR_PREFIX_ALL, 'var');
With that said, it will add an underscore (_) after the prefix (e.g. var_0).
Update:
Regarding your edit, you could simply call array_intersect using call_user_func_array. There's no need for variables.
call_user_func_array('array_intersect', $data);
foreach ($data as $key => $obj)
{
$key = 'var'.$key;
$$key = $obj;
}
You can just cast each object to an array.
foreach ($data AS $key => $datum)
{
$data[$key] = (array) $datum;
}
For your update:
foreach ($data AS $key => $datum)
{
$newkey = 'var' . $key; // we want the variable to be called "var" plus the number
$$newkey = (array) $datum; // Make it an array
$data[$key] = $$newkey; // And update the $data array to contain the array
}
You now have $var0, $var1, etc and can also access them as a collection in $data, and they're in both as an array.

Categories