I was trying to make a function con convert an array in a string value, the code is:
function makestring($array)
{
$outval = '';
foreach($array as $key=>$value)
{
if(is_array($value))
{
$outval .= makestring($value);
}
else
{
$outval .= $value;
}
}
return $outval;
}
But I get this error: Warning: Invalid argument supplied for foreach(). Can anybody please help me?
function makestring($array)
{
if(is_array( $array )) {
$outval = '';
foreach($array as $key=>$value)
{
if(is_array($value))
{
$outval .= makestring($value);
}
else
{
$outval .= $value;
}
}
return $outval;
} else {
die("Supplied argument is not an array");
}
}
OR
function makestring( array $array)
{
// you code goes here
}
Try this. You need to check passing argument is array or not before you use foreach
To prevent an error in foreach, it's better to set type to variable:
foreach((array)$someArray as $data) {
Even if $someArray is not an array, you'll not get any error.
→ Try this:
function makestring($array)
{
$outval = '';
$keys = array_keys( $array );
for( $x = 0; $x < count( $array ); $x++ )
{
if( is_array( $array[ $keys[ $x ] ] ) )
{
$outval .= makestring( $array[ $keys[ $x ] ] );
}
else
{
$outval .= $array[ $keys[ $x ] ];
}
}
return $outval;
}
Maybe the variables you use $key and $value has old data in them , normally a unset($key,$value) in the end of a foreach will cure that
$array = array(
'1',
'2',
'3',
array(
'4',
'5',
array(
'6',
'7'
)
)
);
$output = null;
if(!is_array($array)){
$array = array($array);
}
// use a reference to the $output in the callback
array_walk_recursive($array, function($array_element) use (&$output){
if(is_object($array_element) and method_exists($array_element, '__toString')){
$array_element = strval($array_element);
}
if(is_scalar($array_element)){
$output .= $array_element;
}else{
// found a non scalar... handle it! :)
}
});
echo $output;
Check this out.
array_walk_recursive((array) $array, function($val, $key) {
global $result;
$result .= $val;
});
You check via
if(is_array($value))
{
$outval .= makestring($value);
}
whether to call makestring() recursively. However, in the code that enters makestring() in the first place, you do no such check. That's ok, but then you have to check in makestring(), whether you actually got an array from the caller. That's called defensive programming.
Another option is to do the check on the calling side. That's part of Design By Contractâ„¢:
$var = i_usually_give_you_an_array_but_i_might_also_fail();
if (is_array($var)) {
echo makestring($value);
}
recursive implode() this convert multidimensional array to string
function mYimplode($array, $delimeter) {
$result='';
foreach ($array as $key) {
if (is_array($key)) {
$result .= mYimplode($key, $delimeter) . $delimeter;
} else {
$result .= $key . $delimeter;
}
}
$result = substr($result, 0, 0-strlen($delimeter));
return $result;
}
Related
I need to recursively cast a PHP SimpleXMLObject to an array. The problem is that each sub element is also a PHP SimpleXMLElement.
Is this possible?
json_decode(json_encode((array) simplexml_load_string($obj)), 1);
Didn't test this one, but this seems to get it done:
function convertXmlObjToArr($obj, &$arr)
{
$children = $obj->children();
foreach ($children as $elementName => $node)
{
$nextIdx = count($arr);
$arr[$nextIdx] = array();
$arr[$nextIdx]['#name'] = strtolower((string)$elementName);
$arr[$nextIdx]['#attributes'] = array();
$attributes = $node->attributes();
foreach ($attributes as $attributeName => $attributeValue)
{
$attribName = strtolower(trim((string)$attributeName));
$attribVal = trim((string)$attributeValue);
$arr[$nextIdx]['#attributes'][$attribName] = $attribVal;
}
$text = (string)$node;
$text = trim($text);
if (strlen($text) > 0)
{
$arr[$nextIdx]['#text'] = $text;
}
$arr[$nextIdx]['#children'] = array();
convertXmlObjToArr($node, $arr[$nextIdx]['#children']);
}
return;
}
Taken from http://www.codingforums.com/showthread.php?t=87283
It is possible. This is a recursive function which prints out the tags of parent elements and the tags + contents of elements that have no more children. You can alter it to build an array:
foreach( $simpleXmlObject as $element )
{
recurse( $element );
}
function recurse( $parent )
{
echo '<' . $parent->getName() . '>' . "\n";
foreach( $parent->children() as $child )
{
if( count( $child->children() ) > 0 )
{
recurse( $child );
}
else
{
echo'<' . $child->getName() . '>';
echo iconv( 'UTF-8', 'ISO-8859-1', $child );
echo '</' . $child->getName() . '>' . "\n";
}
}
echo'</' . $parent->getName() . '>' . "\n";
}
I don't see the point since SimpleXMLObject can be threated just like arrays anyway...
But if you really need that, just check chassagnette's answer of in this thread or this post in a forum.
Depending on some troubles with CDATA, arrays etc.
(see: SimpleXMLElement to PHP Array)
I think, this would be the best solution:
public function simpleXml2ArrayWithCDATASupport($xml)
{
$array = (array)$xml;
if (count($array) === 0) {
return (string)$xml;
}
foreach ($array as $key => $value) {
if (is_object($value) && strpos(get_class($value), 'SimpleXML') > -1) {
$array[$key] = $this->simpleXml2ArrayWithCDATASupport($value);
} else if (is_array($value)) {
$array[$key] = $this->simpleXml2ArrayWithCDATASupport($value);
} else {
continue;
}
}
return $array;
}
Here my iterative (even if I don't think you will get a stack explosion by parsing data with a recursive one) implementation of a recursive cast to array. This is a more direct manner of doing it than passing through json_**decode functions:
function xml2Array(SimpleXMLElement $el): stdClass {
$ret = $el;
$stack = [&$ret];
while (count($stack) > 0) {
$cur = &$stack[count($stack) - 1];
array_splice($stack, -1);
$cur = (object) (array) $cur;
foreach ($cur as $key => $child) {
$childRef = &$cur->{$key};
if ($child instanceof SimpleXMLElement)
$stack[count($stack) - 1] = &$childRef;
elseif(is_array($child))
foreach ($childRef as $ckey => $cell) {
if ($cell instanceof SimpleXMLElement)
$stack[count($stack) - 1] = &$childRef[$ckey];
}
}
}
return $ret;
}
For those of you who have concerns about the CDATA case,
combining #ajayi-oluwaseun-emmanuel's answer with this answer worked for me:
$xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$arr = json_decode($json,TRUE);
$printArr = recursive($newArray); //calls recursive function
$data = [];
var_dump($data);
var_dump($printArr);
function recursive($array, $level = 0)
{
$searchingValue = 'tableName';
foreach($array as $key => $value)
{
//If $value is an array.
if(is_array($value))
{
recursive($value, $level + 1);
}
else
{
//It is not an array, so print it out.
if($key == $searchingValue)
{
echo "[".$key . "] => " . $value, '<br>';
$data[] = $value;
}
}
}
}
So I have this function and I am trying to save $value value into $data[] array. But it always returns it empty and I don't know why I can't get $value saved outside the function.
If i echo $value I get what i need but like I've mentioned the variables doesn't get saved in this case - table names.
You need to pass the $data to your recursive function. Also you need to return the $data.
Try this code :
function recursive($array, $level = 0, $data =[])
{
$searchingValue = 'tableName';
foreach($array as $key => $value)
{
//If $value is an array.
if(is_array($value))
{
recursive($value, $level + 1 , $data);
}
else
{
//It is not an array, so print it out.
if($key == $searchingValue)
{
echo "[".$key . "] => " . $value, '<br>';
$data[] = $value;
}
}
}
return $data;
}
You can't access variable $data, which is outside the function, from the function. You need to pass it by reference or return it. Small example
<?php
$a = 1;
// Your case
function b() {
$a = 4;
return true;
}
// Passing by reference
function c(&$d) {
$d = 5;
return true;
}
// Using return
function d($d) {
$d = 6;
return $d;
}
b();
var_dump($a);
c($a);
var_dump($a);
$a = d($a);
var_dump($a);
https://3v4l.org/UXFdR
I ran into this question on a different site, after attempting to work on it for an hour (could be my Sunday brains) I gave up. The question is: If there is a function foo:
function foo(){}
The function can be called as (arguments can be >= 2, where the last is always the value and the previous are part of the array).
So calling the function as:
foo('arg1', 'value');
Should result in:
$array['arg1'] = 'value';
The same if it has more than 1 argument:
foo('arg1', 'argx', 'argz', 'value');
Should produce:
$array['arg1']['argx']['argz'] = 'value';
This was my sad attempt:
function foo()
{
$items = func_get_args();
$value = array_pop($items);
$array = array_shift($items);
// Construct first element
$array = array($array => array());
foreach ($items as $el) {
insert_last($array, $value);
}
return $array;
}
function insert_last(&$array, $value)
{
$copy = $array;
while (true) {
$keys = array_keys($copy);
$last = $copy[$keys[count($copy)-1]];
var_dump($last);
if (empty($last)) {
$last = $value;
break;
}
$copy = $last;
}
var_dump($array, $copy);
}
Pretty sure there is probably an easier solution that I just can't think of at the moment. Thanks!
function foo()
{
$items = func_get_args();
$value = array_pop($items);
$array = [];
$arrayPtr = &$array;
foreach ($items as $element) {
$arrayPtr[$element] = null;
$arrayPtr = &$arrayPtr[$element];
}
$arrayPtr[$element] = $value;
return $array;
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
Demo
Via recursion using call_user_func_array()
<?php
function foo() {
$items = func_get_args();
if ( 1==count($items) ) {
return array_shift($items);
}
else {
$key = array_shift($items);
return array( $key=>call_user_func_array('foo', $items) );
}
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
edit: same thing without func_get_args() but using a variadic function
<?php
function foo(...$items) {
if ( 1==count($items) ) {
return array_shift($items);
}
else {
$key = array_shift($items);
return array( $key=>call_user_func_array('foo', $items) );
}
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
What about something like
function foo()
{
$args = func_get_args();
$items = array_pop($args);
foreach (array_reverse($args) as $item) {
$items = array($item => $items);
}
return $items;
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
Demo
I have a array of val which has dynamic strings with underscores. Plus I have a variable $key which contains an integer. I need to match $key with each $val (values before underscore).
I did the following way:
<?php
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
if(in_array($key, $val)) {
echo 'Yes';
}
else
{
echo 'No';
}
?>
Though this code works fine, I want to know if its a correct way or suggest some better alternative.
use this function for regex match from php.net
function in_array_match($regex, $array) {
if (!is_array($array))
trigger_error('Argument 2 must be array');
foreach ($array as $v) {
$match = preg_match($regex, $v);
if ($match === 1) {
return true;
}
}
return false;
}
and then change your code to use this function like this:
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
if(in_array_match($key."_*", $val)) {
echo 'Yes';
}
else
{
echo 'No';
}
This should work :
foreach( $val as $v )
{
if( strpos( $v , $key .'_' ) === true )
{
echo 'yes';
}
else {
echo 'no';
}
}
you can use this
function arraySearch($find_me,$array){
$array2 =array();
foreach ($array as $value) {
$val = explode('_',$value);
$array2[] =$val[0];
}
$Key = array_search($find_me, $array2);
$Zero = in_array($find_me, $array2);
if($Key == NULL && !$Zero){
return false;
}
return $Key;
}
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
$inarray = false;
foreach($val as $v){
$arr = explode("_", $val);
$inarray = $inarray || $arr[0] == $key
}
echo $inarray?"Yes":"No";
The given format is quite unpractically.
$array2 = array_reduce ($array, function (array $result, $item) {
list($key, $value) = explode('_', $item);
$result[$key] = $value;
return $result;
}, array());
Now you can the existence of your key just with isset($array2[$myKey]);. I assume you will find this format later in your execution useful too.
I currently have coded a way to turn a multidimensional array to comma separated values (I'm using pipes instead of commas for ease of debugging). The problem is, I know that the code I use to do this is bloody awful. It works how I want it to, but it's not nice at all.
What I need
Currently the arr_to_csv() function works for five levels of nested data within the multidimensional array. I need a recursive function to perform the same for one or an unlimited number of nested arrays, or a good nudge in the right direction. Recursion is not my strong point at all, but I know it's the way forward.
Data input
A multi-dimensional array is passed to the function.
array
'name' =>
array
'singular' => null
'plural' => null
'fields' =>
array
'price' =>
array
'label' => string 'Preis' (length=5)
'company_id' =>
array
'label' => null
'placeholder' => null
//...the array could go on...
The function returns the following...
This is exactly what I want...
0 => string 'name||singular||null' (length=20)
1 => string 'name||plural||null' (length=18)
2 => string 'fields||price||label||Preis' (length=27)
3 => string 'fields||company_id||label||null' (length=31)
4 => string 'fields||company_id||placeholder||null' (length=37)
5 => string 'fields||name||label||null' (length=25)
6 => string 'fields||name||placeholder||null' (length=31)
My horrible constructed function
I'm no good with recursion, so here's my awful list of foreachs. As you can see from the below code, this is terrible (no need to read the whole thing, it just copies itself). Please help me sort out my horrible code!
function arr_to_csv($data,$csv = '||') {
$array = array();
/* Epic amount of for each's. This could be done with recursion */
foreach($data as $key => &$value) {
if (!is_array($value)) {
$array[] = $key . $csv .(is_null($value)?'null':$value);
} else {
foreach ($value as $k => &$v) {
if (!is_array($v)) {
$array[] = $key . $csv . $k . $csv . (is_null($v) ? 'null' : $v);
} else {
foreach ($v as $kk => &$vv) {
if (!is_array($vv)) {
$array[] = $key . $csv . $k . $csv . $kk . $csv . (is_null($vv) ? 'null' : $vv);
} else {
foreach ($vv as $x => &$y) {
if (!is_array($y)) {
$array[] = $key . $csv . $k . $csv . $kk . $csv. $x . $csv . (is_null($y) ? 'null' : $y);
} else {
foreach ($y as $too => $long) {
if(!is_array($long)) {
$array[] = $key . $csv . $k . $csv . $kk . $csv. $x . $csv . $too . $csv. (is_null($long)?'null':$long);
} else {
foreach ($long as $omg => $why) {
if(!is_array($why)) {
$array[] = $key . $csv . $k . $csv . $kk . $csv. $x . $csv . $too . $csv . $omg . $csv . (is_null($why) ? 'null' : $why);
}
}
}
}
}
}
}
}
}
}
}
}
return $array;
}
This is some pseudocode, but it is a start:
$strings = [];
$flattenArray = function($arr, $level) use (&$strings, &$flattenArray) {
foreach($arr as $key=>$value){
$s = &$strings[$level];
if(!isset($s)) { $s = array(); }
$s[] = $key;
if(is_array($value)) {
$flattenArray($value, $level);
}
else {
$s[] = $value;
}
$level ++;
}
};
$flattenArray($myArray, 0);
foreach($strings as &$arr) {
$arr = implode("||", $arr);
}
Small demo with your array: http://codepad.viper-7.com/CR2SPY <-- It does not work fully, but it is a start
Update:
Here is a demo that I think works the way you want: http://codepad.viper-7.com/shN4pH
Code:
$strings = [];
$flattenArray = function($arr, $level, $k = null) use (&$strings, &$flattenArray) {
foreach($arr as $key=>$value){
if($k === null) {
$s = &$strings[$key];
}
else {
$s = &$strings[$k];
}
if(!isset($s)) {
$s = array();
}
$str = &$s[$level];
if(!isset($str)) {
$str = array();
if($k !== null) { $str[] = $k; }
}
$str[] = $key;
if(is_array($value)) {
$flattenArray($value, $level, ($k === null) ? $key : $k);
}
else {
$str[] = is_null($value) ? "null" : $value;
}
$level ++;
}
};
$flattenArray($myArray, 0);
$all = [];
foreach($strings as $k => $arr){
$new = array();
foreach($arr as $ky => $ar) {
$all[] = implode("||", $ar);
}
}
print_r($all);
I didn't check it, so in case it doesn't work it should be corrected.
function readarray($from_array, $addr = array()) {
global $output;
foreach ($from_array as $key => $value) {
if (is_Array($value) && count($value) > 0) {
$addr[] = $key;
readarray($value, $addr);
} else {
$output[] = implode('||', $addr) . $value;
}
}
}
$output = array();
foreach ($my_array as $key=>$value){
readarray($value);
}
// improved to get separate arrays of the root of initial array
Not sure if this will help you, but would it not be easier to flatten the array first and then format in in the way you want? To flatten the array try this:
$array = "YOUR ARRAY";
$FlatArray = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $k=>$v)
{
$FlatArray[$k] = $v;
}
Been trying all morning to come up with a recursive function for this. This is as close as I got, Maybe you can improve upon this.
$data = array('name' =>array('singular' => NULL,'plural' => NULL,'fields' =>array('price' =>array('label' =>'Preis','company_id' =>array('label' => NULL,'placeholder' => NULL)))));
function arr_to_csv($data,$csv = '||')
{
$list = "";
foreach($data as $key => &$value)
{
$list .= $key . $csv .((!is_array($value))?(is_null($value)?'null':$value): arr_to_csv($value))."<br>";
}
return $list;
}
print_r(arr_to_csv($data));
Returns This:
name||singular||null
plural||null
fields||price||label||Preis
company_id||label||null
placeholder||null