My problem.. I have a PHP array that looks like this:
[1013] => [1154]
[1013] => [1322]
[1154] => [1525]
[1525] => [1526]
How can I take that and move it to something like this:
[1013] => [1154] => [1525] => [1526]
[1013] => [1322]
So it sort of makes a tree that associates to the top level array item. I do not have control over how the data comes to me, it's generated through a third-party API and given to me just like that.
Logic: Client 1013 is the main account. Client 1154 is a client of 1013. Client 1322 is a client of 1013. Client 1525 is a client of 1154. I want to get it to a multi-dimension array so I can show this in a tree format.
Here you go!:
<?php
// dataset
$clientset = array(
array(1013, 1154),
array(1013, 1322),
array(1154, 1525),
array(1525, 1526)
);
$children = array();
// make an array with children to see which nodes have none
foreach($clientset as $set) {
if(!isset($children[$set[0]])) $children[$set[0]] = array($set[1]);
else $children[$set[0]][] = $set[1];
}
// array with parents
$parents = array();
foreach($clientset as $set) {
$parents[$set[1]] = $set[0];
}
// for each node with no children, begin the search!
foreach($clientset as $set) {
if(!isset($children[$set[1]])) {
echo getPath($set[1]).'</br>';
}
}
// recursively search to parents and print them
function getPath($child) {
global $parents;
if($parents[$child]) {
return (getPath($parents[$child]).' => '.$child);
} else return $child;
}
?>
This outputs:
1013 => 1322
1013 => 1154 => 1525 => 1526
The idea is to see which nodes have no children. Then, iterate through their parents. You probably don't need the output like it is right now, but I'm sure you can work it out this way. Enjoy!
You can use array_walk php function to apply callback to each element of source array. Callback should create a new array according to your requirements. Callback function will take 2 parameters: value of current array element and it's key. Using that it's simple to build array you need.
Chris, you should have just emailed me first. :-p
$test_array = array('1','2','3','4','5','6');
$output_string = '';
for ($i = 0; $i < sizeof($test_array) -1; $i++)
{
$output_string .= '{"'.$test_array[$i].'":';
}
$output_string .= $test_array[$i];
for ($i = 0; $i < sizeof($test_array)-1; $i++) { $output_string .= '}'; }
$new_array = json_decode($output_string, true);
var_dump($new_array);
Related
I am writing a method which takes an array of $topicNames and an array of $app and concatenates each $app to $topicNames like the following
public function getNotificationTopicByAppNames(array $topicNames, array $apps)
{
$topics = [];
foreach ($topicNames as $topicName) {
foreach ($apps as $app) {
$topic = $app . '_' . $topicName;
$topics[] = $topic;
}
}
return $topics;
}
}
The input and result are like the following...
$topicNames = [
'one_noti',
'two_noti',
'three_noti'
];
$apps = [
'one_app',
'two_app'
];
// The return result of the method will be like the following
[
'one_app_one_noti',
'two_app_one_noti',
'one_app_two_noti',
'two_app_two_noti',
'one_app_three_noti',
'two_app_three_noti'
]
My question is instead of doing nested loops, is there any other way I can do? Why do I want to avoid nested loops? Because currently, I have $topic. Later, I might want to add languages, locations etc...
I know I can use map, reduce, array_walks, each those are basically going through one by one. Instead of that which another alternative way I can use? I am okay changing different data types instead of the array as well.
If you dont care about the order you can use this
function getNotificationTopicByAppNames(array $topicNames, array $apps)
{
$topics = [];
foreach($apps as $app){
$topics = array_merge($topics, preg_filter('/^/', $app.'_', $topicNames));
}
return $topics;
}
print_r(getNotificationTopicByAppNames($topicNames,$apps));
Output
Array
(
[0] => one_app_one_noti
[1] => one_app_two_noti
[2] => one_app_three_noti
[3] => two_app_one_noti
[4] => two_app_two_noti
[5] => two_app_three_noti
)
Sandbox
You can also switch loops and use the $ instead to postfix instead of prefix. Which turns out to be in the same order you had. I thought of prefixing as a way to remove the loop. Then i thought why not flip it.
function getNotificationTopicByAppNames(array $topicNames, array $apps)
{
$topics = [];
foreach($topicNames as $topic){
$topics = array_merge($topics, preg_filter('/$/', '_'.$topic, $apps));
}
return $topics;
}
print_r(getNotificationTopicByAppNames($topicNames,$apps));
Output
Array
(
[0] => one_app_one_noti
[1] => two_app_one_noti
[2] => one_app_two_noti
[3] => two_app_two_noti
[4] => one_app_three_noti
[5] => two_app_three_noti
)
Sandbox
The trick here is using preg_filter.
http://php.net/manual/en/function.preg-filter.php
preg_filter — Perform a regular expression search and replace
So we search with ^ start or $ end which doesn't capture anything to replace and then we just add on what we want. I've used this before when I wanted to prefix a whole array with something, etc.
I couldn't test it in a class, so I made it a regular function, so adjust as needed.
Cheers!
You can use :
<?php
public function mergeStacks(...$stacks)
{
$allStacks = call_user_func_array('array_merge', $stacks);
return $this->concatString($allStacks);
}
private function concatString(&$stack, $index = 0, &$result = [])
{
if(count($stack) == 0){
return '';
}
if($index == count($stack)){
return $result;
}
array_walk($stack, function($value, $key) use($index, &$result, $stack){
if($key > $index){
array_push($result, $stack[$index] . '_' . $value);
}
});
$index = $index + 1;
return $this->concatString($stack, $index, $result);
}
And then when you want to get the array, no matter if you have languages or topics etc, you can just do :
$this->mergeStacks($languages, $topics, $locations, .....);
Where $languages, $topics, $locations are simple arrays.
Instead of accepting only topics name parameter try something like this:
function getNotificationTopicByAppNames(array $apps, array ...$names)
{
$topics = [];
foreach ($names as $nameArray) {
foreach ($nameArray as $topicName) {
foreach ($apps as $app) {
$topic = $app . '_' . $topicName;
$topics[] = $topic;
}
}
}
return $topics;
}
$topicNames = [
'one_noti',
'two_noti',
'three_noti'
];
$languagesNames = [
'test_en',
'test_other',
'test_other2'
];
$apps = [
'one_app',
'two_app'
];
print_r(getNotificationTopicByAppNames($apps,$topicNames,$languagesNames));
you can pass any number of arrays to array.
Up until now, I've been using the snippet below to convert an XML tree to an array:
$a = json_decode(json_encode((array) simplexml_load_string($xml)),1);
..however, I'm now working with an XML that has duplicate key values, so the array is breaking when it loops through the XML. For example:
<users>
<user>x</user>
<user>y</user>
<user>z</user>
</users>
Is there a better method to do this that allows for duplicate Keys, or perhaps a way to add an incremented value to each key when it spits out the array, like this:
$array = array(
users => array(
user_1 => x,
user_2 => y,
user_3 => z
)
)
I'm stumped, so any help would be very appreciated.
Here is a complete universal recursive solution.
This class will parse any XML under any structure, with or without tags, from the simplest to the most complex ones.
It retains all proper values and convert them (bool, txt or int), generates adequate array keys for all elements groups including tags, keep duplicates elements etc etc...
Please forgive the statics, it s part of a large XML tools set I used, before rewriting them all for HHVM or pthreads, I havent got time to properly construct this one, but it will work like a charm for straightforward PHP.
For tags, the declared value is '#attr' in this case but can be whatever your needs are.
$xml = "<body>
<users id='group 1'>
<user>x</user>
<user>y</user>
<user>z</user>
</users>
<users id='group 2'>
<user>x</user>
<user>y</user>
<user>z</user>
</users>
</body>";
$result = xml_utils::xml_to_array($xml);
result:
Array ( [users] => Array ( [0] => Array ( [user] => Array ( [0] => x [1] => y [2] => z ) [#attr] => Array ( [id] => group 1 ) ) [1] => Array ( [user] => Array ( [0] => x [1] => y [2] => z ) [#attr] => Array ( [id] => group 2 ) ) ) )
Class:
class xml_utils {
/*object to array mapper */
public static function objectToArray($object) {
if (!is_object($object) && !is_array($object)) {
return $object;
}
if (is_object($object)) {
$object = get_object_vars($object);
}
return array_map('objectToArray', $object);
}
/* xml DOM loader*/
public static function xml_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return xml_utils::dom_to_array($doc->documentElement);
}
/* recursive XMl to array parser */
public static function dom_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
$child = $node->childNodes->item($i);
$v = xml_utils::dom_to_array($child);
if (isset($child->tagName)) {
$t = xml_utils::ConvertTypes($child->tagName);
if (!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
} elseif ($v) {
$output = (string) $v;
}
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = array();
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = xml_utils::ConvertTypes($attrNode->value);
}
$output['#attr'] = $a;
}
foreach ($output as $t => $v) {
if (is_array($v) && count($v) == 1 && $t != '#attr') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
/* elements converter */
public static function ConvertTypes($org) {
if (is_numeric($org)) {
$val = floatval($org);
} else {
if ($org === 'true') {
$val = true;
} else if ($org === 'false') {
$val = false;
} else {
if ($org === '') {
$val = null;
} else {
$val = $org;
}
}
}
return $val;
}
}
You can loop through each key in your result and if the value is an array (as it is for user that has 3 elements in your example) then you can add each individual value in that array to the parent array and unset the value:
foreach($a as $user_key => $user_values) {
if(!is_array($user_values))
continue; //not an array nothing to do
unset($a[$user_key]); //it's an array so remove it from parent array
$i = 1; //counter for new key
//add each value to the parent array with numbered keys
foreach($user_values as $user_value) {
$new_key = $user_key . '_' . $i++; //create new key i.e 'user_1'
$a[$new_key] = $user_value; //add it to the parent array
}
}
var_dump($a);
First of all this line of code contains a superfluous cast to array:
$a = json_decode(json_encode((array) simplexml_load_string($xml)),1);
^^^^^^^
When you JSON-encode a SimpleXMLElement (which is returned by simplexml_load_string when the parameter could be parsed as XML) this already behaves as-if there would have been an array cast. So it's better to remove it:
$sxml = simplexml_load_string($xml);
$array = json_decode(json_encode($sxml), 1);
Even the result is still the same, this now allows you to create a subtype of SimpleXMLElement implementing the JsonSerialize interface changing the array creation to your needs.
The overall method (as well as the default behaviour) is outlined in a blog-series of mine, on Stackoverflow I have left some more examples already as well:
PHP convert XML to JSON group when there is one child (Jun 2013)
Resolve namespaces with SimpleXML regardless of structure or namespace (Oct 2014)
XML to JSON conversion in PHP SimpleXML (Dec 2014)
Your case I think is similar to what has been asked in the first of those three links.
Language: PHP 5
Framework: Joomla 3
I have a single dimension array that I get from get_file_array(). I need to break down the names of the files and eventually query the database for more information. For ease of iteration, I was hoping to add a new dimension to my array rather than create parallel arrays. I thought of iterating through the existing array and adding it one node at a time to a new, specifically multidimensional, perhaps associative array, but this seems inelegant. Is there a way to add a dimension to an existing array in PHP? Here's what I tried last, which obviously doesn't work but conveys the spirit of what I want to accomplish:
require_once "../components/com_esms/models/officelookup.php";
class filearray extends JApplicationCli
{
var $dir = null;
//var_dump($dir);
//error_log("filecopy CWD: ".getcwd());
//error_log("filecopy files: ".print_r($dir, true));
function __construct()
{
$this->dir = scandir("../esms_reports/", 0);
parent::__construct();
}
public function get_file_array()
{
//$this->out('Hello World');
unset($this->dir[0]);
unset($this->dir[1]);
$fa = array_values($this->dir);
return $fa;
}
}
$arr_filearray_obj = new filearray();
$dir = $arr_filearray_obj->get_file_array();
//error_log("filecopy files: ".print_r($dir, true));
/*
foreach($dir as $filename)
{
$fa_underscore = explode("_", $filename);
error_log("filecopy lotid: ".$fa_underscore[1]);
}
*/
//$officeid = array();
for($i = 0; $i < count($dir); $i++)
{
$fa_underscore = explode("_", $dir[$i]);
//error_log("filecopy lotid: ".$fa_underscore[1]);
//error_log("filecopy number of lots: ".$i);
$fa_dash = explode("-", $fa_underscore[1]);
$dir[i][1] = $fa_dash[1];
}
error_log("filecopy officeids: ".print_r($dir, true));
//$result = file_get_contents("http://192.168.1.250/systemname/index.php/option=com_esms&view=officelookup&format=json&officeID=".$officeid[0]);
$officelookup = new EsmsModelofficelookup();
for($o = 0; $o < count($dir); $o++)
{
$result = $officelookup->get_offices($dir[$o][1]);
}
error_log("filecopy JSON: ".$result);
echo("DONE!");
EDIT: Here's an example of the file names I am manipulating. The point is to get the client id, query it, check if it has a parent client id and make a folder structure based on that, using the client names, not IDs.
DDR_1426112931-429_031215.pdf or typeofreport_lotid-clientid_date.pdf
What I would want to add to the array would be the results of my database query, which is a JSON encoded structure containing the client's information and the parent client information, if it exists. It looks something like this:
Array
(
[id] => 123
[name] => Dummy
[parent] => 321
)
Array
(
[id] => 321
[name] => DummyParent
[parent] =>
)
Ok I'm seriously stuck, I've been working on a nav menu that I just cannot get to function how I want it to so i've changed tack but am now stuck again so any help would be much appreciated and desperately needed!
I have the code below and am trying to do the following - I have an array that holds info for all the pages of a site and then another array that holds the ids of the pages that are child pages. What I want to do is use a foreach loop to loop through all the pages of the first array and check whether their ids are in the array of child ids or not. If they are not then they are top level nav pages and I want to output some code and then set up another foreach loop which will check whether any subpages have a parent id of the current page and so on.
I can't seem to work out how to compare $b with the ids in the $childpages array no matter what I try! Is this even possible and if so how please?
This is the first section of what im trying at present
<?php function buildMenu4 ($allpages, $childpages) {
foreach ($allpages as $pages){
$a = $pages['parentid'];
$b = $pages['id'];
$c = $childpages;
echo "<ul>\n";
if (!in_array($b, $c)) {
DO SOMETHING..........
Array contents of $c:
Array
(
[0] => Array ( [id] => 6 )
[1] => Array ( [id] => 15 )
[2] => Array ( [id] => 100 )
[3] => Array ( [id] => 101 )
[4] => Array ( [id] => 103 )
[5] => Array ( [id] => 104 )
[6] => Array ( [id] => 105 )
)
edit ---------------------------------
I have reworked my code and am back to a variation of where I was a couple of days ago!! Anyway the code below works as intended until I try to loop it and then it just echoes the results of the first foreach loop e.g. foreach ($allpages as $pages){..... but fails to do anything else.
I am trying to make a function called loopMenu and then run this recursively until there are no more pages to be displayed. I have tried to write the function as shown with the pusedo code in the code block below but I just can't get it to work. I may have muddled up some of the arguments or parameters or perhaps I have just made a big mistake somewhere but I can't see it - any help would be hugely appreciated and desperately needed!
<?php function buildMenu6 ($allpages, $childpageids, $childpages, $subchildpages) {
foreach ($childpageids as $childid){
$c[] = $childid['id'];
};
echo "<ul>\n";
foreach ($allpages as $pages){
$a = $pages['parentid'];
$b = $pages['id'];
if (!in_array($b, $c)){
echo "<li>" . $pages['linklabel'] . "";
WHERE I WANT THE FUNCTION TO START E.G. function loopMenu($childpages, $subchildpages){...the code that follows....
echo"<ul>\n";
foreach ($childpages as $childparent) {
$d = $childparent['parentid'];
$e = $childparent['id'];
if (($d == $b) or ($d == $g)) {
echo "<li>" . $childparent['linklabel'] . "";
echo "<ul>\n";
foreach ($subchildpages as $subchild){
$g = $subchild['id'];
$f = $subchild['parentid'];
if ($f == $e){
echo "<li>" . $subchild['linklabel'] . "";
WHERE I TRY TO RERUN THE FUNCTION USING loopMenu($childparent, $subchild);
echo "<li/>";
};
};
echo"</ul>\n";
echo "</li>";
};
};
echo "</ul>\n";
WHERE I WANT MY FUNCTION TO END E.G. };
echo "</li>";
};
};
echo "</ul>\n";
}; ?>
Then I call the main buildMenu6 function like so:
<?php buildMenu6($pageids, $childPageIds, $childPageArray, $childPageArray); ?>
You need a nested foreach (I've changed your var names or used the original ones for readability):
foreach($allpages as $page) {
foreach($childpages as $child) {
if($page['id'] == $child['id']) {
//do something
break;
}
}
}
Or PHP >= 5.5.0 use array_column:
$childids = array_column($childpages, 'id');
foreach($allpages as $page) {
if(in_array($page['id'], $childids)) {
//do something
}
}
As a kind of hybrid:
foreach($childpages as $child) {
$childids[] = $child['id'];
}
foreach($allpages as $page) {
if(in_array($page['id'], $childids)) {
//do something
}
}
foreach ($allpages as $pages){
$a = $pages['parentid'];
$b = $pages['id'];
$c = $childpages;
echo "<ul>\n";
// In array isn't aware of the 'id' keys
$found = false;
foreach ($c as $id => $value) {
if ($value == $b) {
$found = true;
}
}
if ($found) {
// DO SOMETHING
}
according to THIS SO ANSWER, array_key_exists is (marginally) the fastest array lookup for php.
since, from your description, it seems reasonable to suppose that [id] is a PRIMARY key and, therefore, unique, i would change the [id] dimension and put values directly in its lieu:
$c[105] = NULL; // or include some value, link page URL
... // some code
$needle = 105;
... // some more code
if (array_key_exists($needle,$c)) {
instead of
$c[5] = 105; // $c is the haystack
... // some code
$needle = 105;
... // some more code
foreach ($c as $tempValue) {
if ($tempValue == $needle) {
in case you want to put values in $c, then you could also use isset (it will return FALSE if array value is NULL for said key).
Having this array:
Array
(
[_block1] => Array
(
[list] => Array
(
[sub-list] => Array
(
)
)
[links] => Number
[total] => Number
...
)
[_block2] => Array
(
[#attributes] => Array
(
)
[title] => ...
[data] => Array ()
...
)
[_block3] => Array
(
..
)
)
Those blocks contain data returned by api. Knowing that each api returns data in a different way/structure I need to measure/calculate the data/size inside of each and one of them and then do if data > X or < do something.
Is it possible? I have searched google but I only found count() and that isn't what I need to make this work.
Edit:
Each and of the those blocks contain many other sub blocks, and I was thinking of calculating the data size in bytes, because count wont do the job here.
echo mb_strlen(serialize((array)$arr), '8bit');
If I understood well your question, you need the size of each "block" subarray inside the main array.
You can do something like this:
$sizes = array();
foreach($returnedArray as $key => $content) {
$sizes[$key] = count($content);
}
The $sizes array will be an associative array which the various "block"s as keys and the size of the data as values.
Edit:
after the edit of the question, if the data inside the innermost arrays are strings or integers you can use a function like this:
function getSize($arr) {
$tot = 0;
foreach($arr as $a) {
if (is_array($a)) {
$tot += getSize($a);
}
if (is_string($a)) {
$tot += strlen($a);
}
if (is_int($a)) {
$tot += PHP_INT_SIZE;
}
}
return $tot;
}
assuming to have only ASCII-encoded strings.
To get the size in bytes you can use the below code.
$serialized = serialize($foo);
if (function_exists('mb_strlen')) {
$size = mb_strlen($serialized, '8bit');
} else {
$size = strlen($serialized);
}
I hope it will be helpful.
Do you mean something like this?
$x = 32;
foreach($blocks as $key => $block)
{
if(getArraySize($block) < $x)
{
//Do Something
}else
{
//Do another thing
}
}
//Recursive function
function getArraySize($array)
{
$size = 0;
foreach($array as $element)
{
if(is_array($element))
$size += getArraySize($element);
else
$size += strlen($element);
}
return $size;
}