I'm having following array.
Array
(
[a] => Array
(
[d] => Array
(
[f] => Array
(
)
[g] => Array
(
[h] => Array
(
)
[i] => Array
(
)
)
[j] => Array
(
)
)
[e] => Array
(
)
)
[b] => Array
(
)
[c] => Array
(
)
)
I have tried with below code but not getting required output.
str($treeArr);
function str($arr){
foreach($arr as $key=>$value){
if(!empty($value)){
echo $key.">";
str($value);
}
else{
echo $key."<br>";
}
}
}
I need following output.
a>d>f
a>d>g>h
a>d>g>i
a>d>j
a>e
b
c
Hi guys,is this what you want?
$a=array('a'=>array('d'=>array('f'=>array(),
'g'=>array('h'=>array(),
'i'=>array()),
'j'=>array()),
'e'=>array()),
'b'=>array(),
'c'=>array()
);
str($a);
function str($arr){
static $temp=array();
foreach($arr as $k=>$v){
$temp[]=$k;
if(!empty($v)){
str($v);
}else{
$str=implode(">",$temp);
echo $str."\n";
}
array_pop($temp);
}
}
output:
a>d>f
a>d>g>h
a>d>g>i
a>d>j
a>e
b
c
Related
i have an array which have two values which are separated by the semicolon out of that i only want the the value which is after the semicolon.
Array ( [0] => animals;1 [1] => animals;2 [2] => animals;3 [3] => birds;1 [4] => birds;2 )
i am getting the output
Array ( [animals] => 1 )
Array ( [animals] => 2 )
Array ( [animals] => 3 )
Array ( [animals] => 3 [birds] => 1 )
Array ( [animals] => 3 [birds] => 2 )
i want the output
Array ( [animals] => 1 )
Array ( [animals] => 2 )
Array ( [animals] => 3 )
Array ( [birds] => 1 )
Array ( [birds] => 2 )
i have tried
$filter_param = array();
foreach ($animals as $options)
{
$exp_data = explode(";",$options);
//echo "<br>";
//print_r($exp_data);
$filter_string = '';
foreach ($exp_data as $dta)
{
$filter_string .= $dta[].',';
}
$filter_string = $exp_data[1];
$filter_string = rtrim($filter_string,",");
$filter_param[$exp_data[0]] = $filter_string;
echo "<br>";
print_r($filter_param);
}
$arr = Array ( 0 => "animals;1", 1 => "animals;2", 2 => "animals;3", 3 => "birds;1", 4 => "birds;2" );
foreach($arr as $key=>$row)
{
$tmparr = explode(";", $row);
$newArr[][$tmparr[0]] = $tmparr[1];
}
foreach($newArr as $new)
{
print_r($new);
echo "<br>";
}
Output
Array ( [animals] => 1 )
Array ( [animals] => 2 )
Array ( [animals] => 3 )
Array ( [birds] => 1 )
Array ( [birds] => 2 )
Demo: Click Here
I have below array
<?php
echo "<pre>";
print_r($row);
?>
Array
(
[0] => Array
(
[contact] => ugin
)
[1] => Array
(
[contact] => gsGR
)
[2] => Array
(
[addtnlcontact] => ghjey
)
)
Array
(
[0] => Array
(
[relation] => uncle
)
[1] => Array
(
[relation] => mrsyjsrky
)
[2] => Array
(
[relation] => cghjrtdj
)
)
If i print echo "<pre>"; print_r($row[0]);
i have below array
Array
(
[contact] => ugin
)
Array
(
[relation] => uncle
)
i need above should be in $row[0] => contact => ugin
relation => uncle
tried below code:
$addcont[] = $this->input->post(sheepItForm1_addtnlcontact);
$this->conditionloop($addcont[0],'contact');
$relation[] = $this->input->post(sheepItForm1_relationship);
$this->conditionloop($relation[0],'relation');
public function conditionloop($val,$name)
{
foreach($val as $key => $res)
{
if(!empty($res)) {
$row[$key][$name] = $res;
//echo "<pre>"; print_r($row[$key]);
}
}
echo "<pre>"; print_r($row[0]);
}
please help to solve this
I have this array
$array = Array
(
[a] => Array
(
[0] => b
[1] => h
)
[b] => c
[c] => d
[h] => m
)
And I need to convert the array to like below
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
I already asked this question for One Dimensional array.
I tried with [Creating nested parent child array from one dimensional array in php and I got the below array
Array
(
[a] => Array
(
[b] => Array
(
[a] => Array
(
[h] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
[h] => Array
(
[m] => Array
(
)
)
)
)
)
)
)
)
)
How to Check wheather the key is present in multi dimensional array and if present add the child to the existing key. Help to resolve the Problem. Thanks in Advance
<?php
$array = array(
'a' => array(0=>'b',1=>'h'),
'b' => 'c',
'c' => 'd',
'h' => 'm',
);
$newArray = array();
$secondarray = array();
$part = &$newArray;
$i=1;
foreach($array as $first => $second)
{
if($i==1)
{
$firstone=$first;
}
else
{
if($i==count($array))
{
$newArray[$first] = array($second => array());
$secondarray[$firstone]=$newArray;
}
else
{
$part = &$part[$first];
$part[$second] = array();
}
}
$i++;
}
echo '<pre>';print_r($secondarray);
output
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
[h] => Array
(
[m] => Array
(
)
)
)
)
The following code is converting XML to array. I want to get a sting by the value of [name]. So for example: $..[offerid].. should give 8b5f7fd3806a42ccb0ade9f8309c5587
Who can help me?? :)
$xmldata = file_get_contents($XMLURL);
$arr= xml2ary($xmldata);
foreach($arr['m4n']['_c']['data']['_c']['record'] as $result){
echo "<pre>"; print_r($result);
}
The echo result is:
Array
(
[recordHash] => Array
(
[_v] => -652572603
)
[column] => Array
(
[0] => Array
(
[_a] => Array
(
[name] => url
)
[_v] => http://ad.dd.com/ppc/?20910868C187884459&zpar6=DF_1&ULP=[[http%3A%2F%2F]]
)
[1] => Array
(
[_a] => Array
(
[name] => title
)
[_v] => This is the title
)
[2] => Array
(
[_a] => Array
(
[name] => description
)
[_v] => Aanbod
)
[3] => Array
(
[_a] => Array
(
[name] => offerid
)
[_v] => 8b5f7fd3806a42ccb0ade9f8309c5587
)
)
)
Try looping through all of the items using a php foreach loop.
$name_to_find = 'offerid';
$value = FALSE;
foreach ($result['column'] as $item) {
if ($item['_a']['name'] == $name_to_find) {
$value = $item['_v'];
break;
}
}
// Now $value has the value of the entry with the name $name_to_find
If you end up finding an entry with the given $name_to_find, then $value will not be FALSE (granted that, you don't have any FALSE values :D).
Is there any common way to edit associative array while retaining reference to original array and making the array be prepared for JSON encode?
An easy way is to rearrange array to new one, but I am thinking that better practice is to go recursively and edit it live.
Here is my function for live editing JSON array with PHP:
function editJSON(&$target, &$aimer){
$found = false;
$result = 0;
foreach($aimer as $ak => $av){
foreach($target as $tk => $tv){
if ($tk == $ak){
$found = true;
$result = 1;
if (is_array($tv) && is_array($av)) editJSON($target[$tk], $aimer[$ak]);
}
if (!$found && is_array($tv)) $result += editJSON($target[$tk], $aimer);
}
if ($found && !is_array($av)) $target[$ak] = $av;
}
return $result;
}
Its fast and tested:
$target = array( 'z'=>array('v'=>1, 'c'=>2, 'g'=>3),
'b'=>array(
'a'=>array(
'b'=>array('g'=>array('c'=>11, 'd'=>13), 'b'=>array('x'=>91, 'y'=>92)),
'e'=>array('m'=>2)
)
),
'd'=>array(
'a'=>array(
'b'=>array('g'=>array('c'=>11, 'd'=>13))
)
)
);
$aimer = array(
'a'=>array(
'b'=>array('g'=>array('c'=>998, 'hh'=>999))
)
);
echo 'Target array:';
echo '<br />';
print_r($target);
echo '<br />';
echo 'Aiming array:';
echo '<br />';
print_r($aimer);
echo '<br />';
$result = editJSON($target, $aimer);
echo 'Resulting array:';
echo '<br />';
print_r($target);
echo '<br />';
echo 'JSON has been modified ' . $result . ' times.';
Will output:
Target array:
Array ( [z] => Array ( [v] => 1 [c] => 2 [g] => 3 ) [b] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 11 [d] => 13 ) [b] => Array ( [x] => 91 [y] => 92 ) ) [e] => Array ( [m] => 2 ) ) ) [d] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 11 [d] => 13 ) ) ) ) )
Aiming array:
Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 998 [hh] => 999 ) ) ) )
Resulting array:
Array ( [z] => Array ( [v] => 1 [c] => 2 [g] => 3 ) [b] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 998 [d] => 13 [hh] => 999 ) [b] => Array ( [x] => 91 [y] => 92 ) ) [e] => Array ( [m] => 2 ) ) ) [d] => Array ( [a] => Array ( [b] => Array ( [g] => Array ( [c] => 998 [d] => 13 [hh] => 999 ) ) ) ) )
JSON has been modified 2 times.
EDIT: Added functionality for adding values to array and counting edits.