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

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;
}

Related

Accessing data from DB

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 */;

Problems changing values in an array/object nested combination array

I don't know what to do to get this done what would like to do. I tried multiple approaches, e.g. I used array_map, array_walk, nested foreach loops with get_object_vars and I worked with json_decode/encode and so on. I always come a little bit further but never reach my goal and I would like to get some guidance from you
Basically when you see the array below, how would you proceed when you want to change some value in the path array for multiple values in the array itself?
My questions:
1) Is it right that I must convert both nested objects to an array first or is this not nesessary to do this? I mean I always get some type conversion error which tells me that I either have everything as an object or array. Is this right?
2) If this mistery is solved, which php array function is the appropriate one to change values in an array(/object)? As I have written above, I tried so many and I don't see the trees in the woods anymore. Which one do you suggest to me to use in a foreach loop?
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pictures
[2] => food
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pictures
[2] => vacations
[3] => rome
)
)
)
)
I would suggest that,
you create an array with keys as new path and value as old path (
path to be replaced).
Loop you path array and check if it is available in above defined array.
If available replace it with key of above defined array.
For example
// array defined as point 1
$change_path_array= array('pics'=>'pictures','meal'=>'food');
// $array is your array.
foreach ($array as $value) {
// loop you path array
for($i=0;$i<count($value->doc->path);$i++){
// check if the value is in defined array
if(in_array($value->doc->path[$i],$change_path_array)){
// get the key and replace it.
$value->doc->path[$i] = array_search($value->doc->path[$i], $change_path_array);
}
}
}
Out Put: picture is replaced with pics and food with meal
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pics
[2] => meal
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pics
[2] => vacations
[3] => rome
)
)
)
)
You can modify the code to check casesensitive.
Example of changing all pictures to photos:
$doc1 = new \stdClass;
$doc1->doc = new \stdClass;
$doc1->doc->path = array('Bob', 'pictures', 'food');
$doc2 = new \stdClass;
$doc2->doc = new \stdClass;
$doc2->doc->path = array('Alice', 'pictures', 'vacations', 'rome');
$documents = array($doc1, $doc2);
/* change all 'pictures' to 'photos' */
foreach ($documents as &$doc) {
foreach ($doc->doc->path as &$element) {
if ($element == 'pictures') {
$element = 'photos';
}
unset($element);
}
unset($doc);
}
print_r($documents);
You can do it like this:
for($i = 0; $i < count($arr); $i++){
$path_array = $arr[$i]->doc->path;
// do your modifications for [i]th path element
// in your case replace all 'Bob's with 'Joe's
$path_array = array_map(function($paths){
if($paths == 'Bob') return 'Joe';
return $paths;
}, $paths_array);
$arr[$i]->doc->path = $path_array;
}

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.

PHP Accessing named array elements issue

I have an array which is generated from an XML file and it shows me like below when printed with print_r
Array
(
[cxname] => Global CX 87 123
[ipaddress] => 66.240.55.87
[slots] => Array
(
[slot] => Array
(
[0] => Array
(
[slotno] => 1
[cardtype] => 0x24
[modelno] => OP3524J
[label1] => OP
[label2] => Module
[severity] => Minor
)
[1] => Array
(
[slotno] => 2
[cardtype] => 0x25
[modelno] => OP3524K
[label1] => OP
[label2] => Module
[severity] => Major
)
)
)
)
When I print like this, it shows nothing
echo $dataArray->cxname;
But following code works and prints "Global CX 87 123 "
echo $dataArray["cxname"];
How can I make it work as above example.
Just do this:
$dataArray = (object)$dataArray;
It'll convert the array in an stdClass object and allow you to use it that way. Please note that this will only convert the first level for the array. You'll have to create a function to recurse through the array if you want to access all levels that way. For example:
<?php
function arrayToObject($array) {
if(!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = arrayToObject($value);
}
}
return $object;
}
else {
return FALSE;
}
}
For more information, have a look at http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass. To find out about typecasting, you can also read http://www.php.net/manual/en/language.types.object.php#language.types.object.casting.
This:
echo $dataArray["cxname"];
is the way you access array elements. But this:
echo $dataArray->cxname;
is the way, you access class members.
If you want to access the data as class members, you have to use a xml parser which returns classes (or objects), not arrays.
If you have got the XML string, you can parse it into an object by using simplexml_load_string().

Categories